//------------------------------------------------------------------------------------//

function AjaxFormSubmit(sFormId,sElementId,sUrl) {
	new Ajax.Request(sUrl,{
		method: 'post',
		parameters: $(sFormId).serialize(true),
		onSuccess: function(transport) {
			$(sElementId).update(transport.responseText);
			new Effect.Highlight(sElementId);
		}
	});
}

//------------------------------------------------------------------------------------//

function AjaxUpdate(sElementId,sUrl) {
	new Ajax.Updater(sElementId,sUrl, {
		method: 'post', 
		evalScripts: true,
		onSuccess: function() {
			new Effect.Highlight(sElementId);
		}
	});
}

//------------------------------------------------------------------------------------//

function CloseElement(sElementId) {
	UpdateContent(sElementId,'');
}

//------------------------------------------------------------------------------------//

function CloseMainDetail() {
	CloseElement('Main_Detail');
}

//------------------------------------------------------------------------------------//

function ConfirmDelete($sMsg) {
	// Example usage:
	// <a href="SomeProgram.php" onClick="return ConfirmDelete()">Delete</a>
	if ($sMsg == null) {
		$sMsg = "Click 'OK' to confirm you want to delete this item.";
	}
	return (confirm($sMsg));
}

//------------------------------------------------------------------------------------//

function DeleteApptAuxItem(sDatabox,iApptId) {
	if (ConfirmDelete()) {
		var sAuxElementId = sDatabox + '_' + iApptId;
		var sAuxOuter = sDatabox + 'List';
		var sUrl = 'DeleteApptAux.php?t='+sDatabox+'&aid='+iApptId;
		new Ajax.Request(sUrl);
		$(sAuxElementId).update("");
		$(sAuxOuter).hide();
	}
}

//------------------------------------------------------------------------------------//

function DeleteAuxItem(sDatabox,iItemId,iContactId) {
	if (ConfirmDelete()) {
		var sAuxElementId = sDatabox + '_' + iItemId;
		var sUrl = 'DeleteContactAux.php?t='+sDatabox+'&id='+iItemId+'&cid='+iContactId;
		new Ajax.Request(sUrl);
		$(sAuxElementId).replace("");
		CloseMainDetail();
	}
}

//------------------------------------------------------------------------------------//

function DeleteNote(sElementId,iNoteId) {
	if (ConfirmDelete()) {
		var sUrl = 'DeleteNote.php?nid='+iNoteId;
		new Ajax.Request(sUrl);
		$(sElementId).replace("");
	}		
}

//------------------------------------------------------------------------------------//

function LoadPage(sUrl) {
	location.replace(sUrl);
}

//------------------------------------------------------------------------------------//

function MenuExpand(s) {
  var td = s;
  var d = td.getElementsByTagName("div").item(0);

  td.className = "menuHover";
  d.className = "menuHover";
}

//------------------------------------------------------------------------------------//

function MenuCollapse(s) {
  var td = s;
  var d = td.getElementsByTagName("div").item(0);

  td.className = "menuNormal";
  d.className = "menuNormal";
}

//------------------------------------------------------------------------------------//

function ModalSearchAction(SelectList,sUrl) {
	// Replaces _CID_ with selected ContactId in sUrl
	// "_CID_" MUST BE last 5 chars of sUrl!!!
	var iContactId = SelectList.options[SelectList.selectedIndex].value;
	var iLen = sUrl.length;
	var sNewUrl = sUrl.substr(0,iLen-5);
	sNewUrl += iContactId;
	if (iContactId != '') {
		parent.location = sNewUrl;
	}
}
//------------------------------------------------------------------------------------//

function OptCodeDrop(sDivId,sUrl,oObj) {
	var sDropValue = $F(oObj);
	var sParms = "dv=" + sDropValue;
	var sNewUrl = sUrl + "&" + sParms;
	new Ajax.Updater(sDivId,sNewUrl,{asynchronous:true});
}


//------------------------------------------------------------------------------------//

function PopCalendar(sFormName,sFormField) {
	var calStartDate = new calendar_MYSQL(document.forms[sFormName].elements[sFormField]);
	calStartDate.year_scroll = false;
	calStartDate.time_comp = false;
	calStartDate.popup();
}

//------------------------------------------------------------------------------------//

function PopUpCalendar(oElement) {
	// Example: <a href="javascript:PopUpCalendar($('tt_date_due'));"><img src="js_calendar_pop/cal.gif" width="16" height="16" border="0"></a>
	var calPopUp = new calendar_MYSQL(oElement);
	calPopUp.year_scroll = false;
	calPopUp.time_comp = false;
	calPopUp.popup();
}
//------------------------------------------------------------------------------------//

function RegionDrop(sDivId,sUrl,oObj) {
	var sDropValue = $F(oObj);
	var sParms = "dv=" + sDropValue;
	var sNewUrl = sUrl + "&" + sParms;
	new Ajax.Updater(sDivId,sNewUrl,{asynchronous:true});
}

//------------------------------------------------------------------------------------//

function RemoveApptContact(iContactId,iApptId) {
	var sElementId = 'contact_' + iContactId;
	var sUrl = 'ApptRemoveContact.php?aid='+iApptId+'&cid='+iContactId;
	new Ajax.Request(sUrl);
	$(sElementId).replace("");
}

//------------------------------------------------------------------------------------//

function StampTime(sHrElement,sMinElement,sMeridianElement) {
	oTimeStamp = new Date();

	var iHours;
	var iMins;
	var sMeridian;
	iHours = oTimeStamp.getHours();
	if (iHours >= 12) {
		sMeridian = "PM";
	} else {
		sMeridian = "AM";
	}
	if (iHours > 12) {
		iHours -= 12;
	}
	if (iHours == 0) {
		iHours = 12;
	}
	// Round down to nearest 5 minutes (because dropdown is in 5 min intervals)
	iMins = oTimeStamp.getMinutes();
	var sMins = iMins.toString();
	var sOnes;
	var sTens;
	if (iMins > 10) {
		sOnes = sMins.substr(1,1);
		sTens = sMins.substr(0,1);
	} else {
		sOnes = sMins;
		sTens = '0';
	}
	if (parseInt(sOnes) < 5) {
		sOnes = '0';
	} else {
		sOnes = '5';
	}
	var sHours = iHours.toString();
	if (parseInt(iHours) < 10) {
		sHours = '0' + iHours.toString();
	}
	sHrElement.value 		= sHours;
	sMinElement.value 		= (sTens + sOnes);
	sMeridianElement.value 	= sMeridian;
}
//------------------------------------------------------------------------------------//

function ToggleDatabox(sElementId) {
	$(sElementId).toggle();
	new Effect.Highlight(sElementId);
}

//------------------------------------------------------------------------------------//

function UpdateAuxListing(sAddChange,sAuxListingLine,DATABOX,REC_ID) {
	var sElement = DATABOX + '_' + REC_ID;
	sAuxListingLine = unescape(sAuxListingLine);
	if (sAddChange == 'CHANGE') {
		$(sElement).update(sAuxListingLine);
	} else {
		var NewLine = "<div id='"+sElement+"' class='SortItem'>"+sAuxListingLine+"</div>";
		var sDragDrop = DATABOX + '_DragDrop';
		var sContainer = DATABOX + 'List';
		$(sDragDrop).insert({top: NewLine});
		CloseMainDetail();
		new Effect.Highlight(sElement);
		// Need to Re-create the sortable (drag/drop) division because it needs to re-read the added element.
		switch (DATABOX) {
		case 'addr':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_addr});
			break;
		case 'aka':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_aka});
			break;
		case 'board':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_board});
			break;
		case 'contacttype':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_contacttype});
			break;
		case 'creditcard':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_creditcard});
			break;
		case 'email':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_email});
			break;
		case 'ethnic':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_ethnic});
			break;
		case 'phone':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_phone});
			break;
		case 'region':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_region});
			break;
		case 'website':
			Sortable.create(sDragDrop,{tag:'div',onUpdate:Update_website});
			break;
		}
		$(sContainer).show();
	}
}

//------------------------------------------------------------------------------------//

function UpdateContent(sId,sContent) {
	$(sId).update(sContent);
	new Effect.Highlight(sId);
}

//------------------------------------------------------------------------------------//

function UpdateMainDetail(sUrl) {
	AjaxUpdate('Main_Detail',sUrl);
}

//------------------------------------------------------------------------------------//

function ValidateCopyMoveForm(current_form) {
	var iFilledFields = 0;
	for (counter=0; counter < current_form.length; counter++) {
		if (current_form[counter].type == 'select-one') {
			if (current_form[counter].value != '') {
				iFilledFields++;
			}
		}
	}
	if (iFilledFields == 1) {
		if (confirm('Click OK to confirm Copy/Move')) {
			current_form.submit();
		}
	} else {
		alert('One and only one field must be filled out.');
	}
}

//------------------------------------------------------------------------------------//

function ViewContact(SelectList) {
	var iContactId = SelectList.options[SelectList.selectedIndex].value;
	if (iContactId != '') {
		self.location = 'ViewContact.php?cid=' + iContactId;
	}
}

//------------------------------------------------------------------------------------//

