
// Add Trim function to ALL strings
//String.prototype.trim = function(){
 //return( (ar=/^s*([sS]*S+)s*$/.exec(this)) ? ar[1] : "" );
//}
// mystring = mystring.trim(); 
function setFocus(){
}
function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}
function sumFields(myForm,subTractee,subTractor,answer){
	myForm[answer].value = myForm[subTractee].value - myForm[subTractor].value;
}

function checkForDelete(myForm,myControl) {
	if (myControl.length == undefined) {
		if (myControl.checked == true) {
			var isChecked = true;
		}
	}
	else {
		for (var i = 0; i < myControl.length; i++) {
			if (myControl[i].checked == true) {
				var isChecked = true;
			}
		}
	}
	if (isChecked) {
		myForm.submit();
	}
	else {
		alert("You need to check items to delete");
		return;
	}
}

function helpWindow ( url )
{
	return window.open ( url, "myhelp", "dependent=yes,height=320,resizable=yes,scrollbars=yes,width=440" );
}

  ////////////////////////////////////////////////////
  // This function fills the SELECT list with myContact
  function initControls() {
    with (document.updateContact) {
      // Clear any current OPTIONS from the SELECT
      myDataSelect.options.length = 0;

      // For each contact record...
      for (var i = 0; i < myContact.getRowCount(); i++) {

        // Create a new OPTION object
        NewOpt = new Option;
        NewOpt.value = myContact.gccontactid[i];
        NewOpt.text = myContact.gcfirstname[i] + ' ' + myContact.gclastname[i];       

        // Add the new object to the SELECT list
        myDataSelect.options[myDataSelect.options.length] = NewOpt;        
      }
    }
  }
 
  ////////////////////////////////////////////////
  // This function populates other INPUT elements
  // when an option in the SELECT box is clicked
function fillControls() {
	var myC = 0;
	var aC = myAddress.getRowCount();
	var pC = myPhones.getRowCount();
	var myFound = 0;
  with (document.updateContact) {
    // Add one to the OPTION number to get the data row number
    var rowNum = myDataSelect.selectedIndex;
		// clear the checkbox
		gcRegistered.checked = false; 
    // Populate textboxes with data in that row from contact
		for (i = 0;i < gcType.options.length; i++) {
			if (gcType.options[i].text == myContact.gctype[rowNum]) {
	    	gcType.options[i].selected = true;
			}
		}
		for (i = 0;i < gcStatus.options.length; i++) {
			if (gcStatus.options[i].text == myContact.gcstatus[rowNum]) {
	    	gcStatus.options[i].selected = true;
			}
		}
		for (i = 0;i < gcResponsibility.options.length; i++) {
			if (gcResponsibility.options[i].text == myContact.gcresponsibility[rowNum]) {
	    	gcResponsibility.options[i].selected = true;
			}
		}		
    gcFirstName.value = myContact.gcfirstname[rowNum];
    gcLastName.value = myContact.gclastname[rowNum];
    gcGhin.value = myContact.gcghin[rowNum];
		for (i = 0;i < gcPrefix.options.length; i++) {
			if (gcPrefix.options[i].text == myContact.gcprefix[rowNum]) {
	    	gcPrefix.options[i].selected = true;
			}
		}
		gcEmail.value = myContact.gcemail[rowNum];
		if(myContact.gcregistered[rowNum] == 1){
			gcRegistered.checked = true;
		}
		
//		Clear the address fields
		gcAddress1.value = "";
		gcAddress2.value = "";
		gcCity.value = "";
		gcState.value = "";
		gcZipCode.value = "";
		while (aC != 0 && aC > myC) {
			if (myAddress.gcobjectid[myC] == myContact.gccontactid[rowNum]){
			    // Populate textboxes with data in that row from address
				gcAddress1.value = myAddress.gcaddress1[myC];
				gcAddress2.value = myAddress.gcaddress2[myC];
				gcCity.value = myAddress.gccity[myC];
				gcState.value = myAddress.gcstate[myC];
				gcZipCode.value = myAddress.gczipcode[myC];
				myFound = 1;
			}
			myC++;
		}
//	clear the phone fields
		gcPhoneType1.value = "";
		gcArea1.value = "";
		gcPhone1.value = "";
		gcPhoneType2.value = "";
		gcArea2.value = "";
		gcPhone2.value = "";
    myC = 0;
		myFound = 1;
		// Populate textboxes with data in that row from phones		
		//gcphone1 = myPhones.gcphone
		while (pC != 0 && pC > myC && myFound < 3) {
			if (myPhones.gcobjectid[myC] == myContact.gccontactid[rowNum]){
			    // Populate textboxes with data in that row from address
				for (i = 0;i < eval("gcPhoneType" + myFound).options.length; i++) {
					if (eval("gcPhoneType" + myFound).options[i].text == myPhones.gctype[myC]) {
	    			eval("gcPhoneType" + myFound).options[i].selected = true;
					}
				}
				eval("gcArea" + myFound).value = myPhones.gcarea[myC];
				eval("gcPhone" + myFound).value = myPhones.gcphone[myC];
				myFound++; 
			}
			myC++;
		} 
	}	
}

 ////////////////////////////////////////////////

 // This function "saves" data from the various

 // text boxes into the wddxRecordset object

 function keepChanges() {
  var myString = "";
	var myC = 0;
	var aC = myAddress.getRowCount();
	var pC = myPhones.getRowCount();
	var isAddress = 0;
  with (document.updateContact) {
  	// Get the data row number
    var selectedContact = myDataSelect.selectedIndex;
    var rowNum = selectedContact;
    // Populate javascript data array with info from textboxes
    myContact.gcghin[rowNum] = gcGhin.value;
  	myContact.gcemail[rowNum] = gcEmail.value;
    myContact.gcfirstname[rowNum] = gcFirstName.value;
    myContact.gclastname[rowNum] = gcLastName.value;
	  myContact.gctype[rowNum] = gcType.options[gcType.selectedIndex].text;
		myContact.gcstatus[rowNum] = gcStatus.options[gcStatus.selectedIndex].text;
		myContact.gcresponsibility[rowNum] = gcResponsibility.options[gcResponsibility.selectedIndex].text;
		myContact.gcprefix[rowNum] = gcPrefix.options[gcPrefix.selectedIndex].text;
		myContact.wasedited[rowNum] = 'Yes';
		if (gcRegistered.checked == true) {
			myContact.gcregistered[rowNum] = 1;
		}
		else {
			myContact.gcregistered[rowNum] = 0;
		}
	//	Check if there is a address record in the recordset for this contact
	//	If so then update the record.
		while (aC != 0 && aC > myC) {
			if (myAddress.gcobjectid[myC] == myContact.gccontactid[rowNum]){
				myAddress.gcaddress1[myC] = gcAddress1.value;
				myAddress.gcaddress2[myC] = gcAddress2.value;
				myAddress.gccity[myC] = gcCity.value;
				myAddress.gcstate[myC] = gcState.value;
				myAddress.gczipcode[myC] = gcZipCode.value;
				myAddress.wasedited[myC] = 'Yes';
				isAddress = 1;  //contact has an address
			}
			myC++;
		}
		if (isAddress != 1){ //No address for this record so create one.
		// Check to see if the address field have content.
		// If so then create a new address record
		 	myString = myString.concat(gcAddress1.value,gcAddress2.value,gcCity.value,
											gcState.value,gcZipCode.value);
	    myString = trim(myString);
			if (myString.length != 0) {
				myAddress.addRows(1);
				newRow = myAddress.getRowCount() - 1;
				myAddress.setField(newRow, "gcaddressid", 0);
				myAddress.setField(newRow, "gcaddress1", gcAddress1.value);
				myAddress.setField(newRow, "gcaddress2", gcAddress2.value);
				myAddress.setField(newRow, "gccity", gcCity.value);
				myAddress.setField(newRow, "gcstate", gcState.value);
				myAddress.setField(newRow, "gczipcode", gcZipCode.value);
				myAddress.setField(newRow, "gcobjectid", myContact.gccontactid[rowNum]);
				myAddress.setField(newRow, "wasedited", "New");
			}
		}
  // check to see if there are phone records for this contact
    myC = 0;
		myFound = 1;
		while (pC != 0 && pC > myC && myFound < 3) {
			if (myPhones.gcobjectid[myC] == myContact.gccontactid[rowNum]){
				myPhones.gctype[myC] = eval("gcPhoneType" + myFound).options[eval("gcPhoneType" + myFound).selectedIndex].text;
				myPhones.gcarea[myC] = eval("gcArea" + myFound).value;
				myPhones.gcphone[myC] = eval("gcPhone" + myFound).value;
				myPhones.wasedited[myC] = 'Yes';
				myFound++; 
			}
			myC++;
		} 

		if (myFound == 1) {  //means there are no phone records for this contact
		  myString = "";		// check for data to add
			myString = myString.concat(gcArea1.value,gcPhone1.value);
			if (myString.length != 0){  // if there is data to add, add it.
				myPhones.addRows(1);
				newRow = myPhones.getRowCount() - 1;
				myPhones.setField(newRow, "gcphoneid", 0);
				myPhones.setField(newRow, "gcphone", gcPhone1.value);
				myPhones.setField(newRow, "gcarea", gcArea1.value);
				myPhones.setField(newRow, "gctype", gcPhoneType1[gcPhoneType1.selectedIndex].text);
				myPhones.setField(newRow, "gcobjectid", myContact.gccontactid[rowNum]);
				myPhones.setField(newRow, "wasedited", "New");
			}
		}
		if (myFound < 3) {  // means there is no 2nd phone record
		  myString = "";  // check for data to add and add it
			myString = myString.concat(gcArea2.value,gcPhone2.value);
			
			if (myString.length != 0){
				myPhones.addRows(1);
				newRow = myPhones.getRowCount() - 1;
				myPhones.setField(newRow, "gcphoneid", 0);
				myPhones.setField(newRow, "gcphone", gcPhone2.value);
				myPhones.setField(newRow, "gcarea", gcArea2.value);
				myPhones.setField(newRow, "gctype", gcPhoneType2[gcPhoneType2.selectedIndex].text);
				myPhones.setField(newRow, "gcobjectid", myContact.gccontactid[rowNum]);
				myPhones.setField(newRow, "wasedited", "New");
			}
		}
    // Re-initialize the SELECT list
    initControls();
    // Re-select the book that was selected before
    myDataSelect.selectedIndex = -1;
    myDataSelect.selectedIndex = selectedContact;
   }
 }

  ////////////////////////////////////////////////

  // This function clears all fields

  function clearFields() {
    with (document.updateContact) {
      // Add a new row to the recordset
//		Clear the address fields
		gcAddress1.value = "";
		gcAddress2.value = "";
		gcCity.value = "";
		gcState.value = "";
		gcZipCode.value = "";
//	clear the phone fields
		gcPhoneType1.value = "";
		gcArea1.value = "";
		gcPhone1.value = "";
		gcPhoneType2.value = "";
		gcArea2.value = "";
		gcPhone2.value = "";
// clear contact fields
		gcGhin.value = "";
		gcEmail.value = "";
		gcFirstName.value = "";
		gcLastName.value = "";
		gcRegistered.checked = false;
		gcPrefix.value = "";
		gcStatus.value = "";
		gcType.value = "";
    }
  }

 ////////////////////////////////////////////////

 // This function inserts a new row in the 
 // sends recordsets to the form and submits it
function commitToServer() {
  with (document.updateContact) {
    // Create new WDDX Serializer object (supplied by Allaire)
    mySerializer = new WddxSerializer();
    // Serialize the "Books" recordset into a WDDX packet
    myWDDXContact = mySerializer.serialize(myContact);
		myWDDXPhones = mySerializer.serialize(myPhones);
		myWDDXAddress = mySerializer.serialize(myAddress);
    // Place the packet into the "WDDXContent" hidden field
    WDDXContact.value = myWDDXContact;      
		WDDXPhones.value = myWDDXPhones;
		WDDXAddress.value = myWDDXAddress;
    // Submit the form
    submit();
  }
}

function verifyPassword(myControl) {
	var myForm = myControl.form;
	if (myForm.tendon.value != myControl.value) {
		myForm.tendon.value = '';
		myForm.tendon.focus();
		myControl.value = '';
		alert("The passwords do not match");
	}
}

function setFormAction(myForm,myAction,myControl,myMessage){
	if (arguments.length > 2) {
		if(myControl.value != ''){
			myForm.action = myAction;
			myForm.submit();
		}
		else {
				alert(myMessage);
				return;
		}
	}
	else {
		myForm.action = myAction;
		myForm.submit();
	}
} 

function allSelected(myForm) {
	var s = new Array(1); 
	s[0] = myForm.column1;
	s[1] = myForm.column2;
	s[2] = myForm.column3;
	for (var j = 0; j <= 2; j++) {		
		for (var i=(s[j].options.length-1); i>=0; i--) { 
			s[j].options[i].selected = true;
		}
	}
	//myForm.action = myAction;
	//myForm.submit();
	return;
}	
		
/*		function setFormAction2(myForm,myAction,myControl){
			if(myControl.value != ''){		
				myForm.action = myAction + '&myCounter=' + myControl.name + '&sortData=' + myForm.sortData.value;
			//myForm.submit();
			}
		} */	
//	function setFormAction(myDocument, myFormName, myAction) {
//		document[myFormName].action = myAction;
//		document[myFormName].submit();
//	}
	
	function editContent(fileName){
		document.editForm.action = 'index.cfm?fuseaction=admin.wysiwyg';
		document.editForm.fileName.value = fileName;
		document.editForm.submit();
	}

	function disableMe(control){
		if(document.all){
			controlAgain = control + 'Again';
			document.all(control).disabled=true;
			document.all(controlAgain).disabled=false;
		}
		else{
			control = document.getElementById(control);
			controlAgain = document.getElementById(control + 'Again');
			control.disabled=true;
			controlAgain.disabled=false;
		}
	}
	function enableMe(control){
		if(document.all){
			document.all(control).disabled=false;
		}
		else{
			control = document.getElementById(control);
			control.disabled=false;
		}
	}
function removeSelectedOptions(from) { 
	for (var i=(from.options.length-1); i>=0; i--) { 
		var o=from.options[i]; 
		if (o.selected) { 
			from.options[i] = null; 
			} 
		} 
	from.selectedIndex = -1; 
	} 
	
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			}
		}

	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	countWords(to,from);
	}
function countWords(myColumn1) {
		var total1 = 0;
		var myForm = document.wordCountTotal;
		var myValue = document.wordCount;
		if (arguments.length > 1) {
			var total2 = 0;
			var myColumn2 = arguments[1];
			for (var i=(myColumn2.options.length-1); i>=0; i--) {
				var o = myColumn2.options[i];				
				total2 = parseInt(total2) + parseInt(myValue.elements['a' + o.value].value);
			}
			myForm[myColumn2.name].value = total2;
		}
		for (var i=(myColumn1.options.length-1); i>=0; i--) {
			var o = myColumn1.options[i];
			total1 = parseInt(total1) + parseInt(myValue.elements['a' + o.value].value);
		}
		myForm[myColumn1.name].value = total1;
}
// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}


	// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	// If > 1 option selected, do nothing
	var selectedCount=0;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount!=1) {
		return;
		}
	// If this is the first item in the list, do nothing
	var i = obj.selectedIndex;
	if (i == 0) {
		return;
		}
	swapOptions(obj,i,i-1);
	obj.options[i-1].selected = true;
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	// If > 1 option selected, do nothing
	var selectedCount=0;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount != 1) {
		return;
		}
	// If this is the last item in the list, do nothing
	var i = obj.selectedIndex;
	if (i == (obj.options.length-1)) {
		return;
		}
	swapOptions(obj,i,i+1);
	obj.options[i+1].selected = true;
	}
	// -------------------------------------------------------------------
// autoComplete (text_input, select_input, ["text"|"value"], [true|false])
//   Use this function when you have a SELECT box of values and a text
//   input box with a fill-in value. Often, onChange of the SELECT box
//   will fill in the selected value into the text input (working like
//   a Windows combo box). Using this function, typing into the text
//   box will auto-select the best match in the SELECT box and do
//   auto-complete in supported browsers.
//   Arguments:
//      field = text input field object
//      select = select list object containing valid values
//      property = either "text" or "value". This chooses which of the
//                 SELECT properties gets filled into the text box -
//                 the 'value' or 'text' of the selected option
//      forcematch = true or false. Set to 'true' to not allow any text
//                 in the text box that does not match an option. Only
//                 supported in IE (possible future Netscape).
// -------------------------------------------------------------------
function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i; }
	else { select.selectedIndex = -1; }
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
			}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
	}
	function getRes(myForm,width,height) {
    if (navigator.appName == 'Netscape' && document.layers != null) {
        wid = window.innerWidth;
        hit = window.innerHeight;
    }
    if (document.all != null){
        wid = document.body.clientWidth;
        hit = document.body.clientHeight;
    }
		width.value = wid;
		height.value = hit;
}

