/* #################     HELPFUL FUNCTIONS    ########################################################################### */
function clearField(id){
	// clear the value of the form field
	$("#"+id).val('');
}

function clearFields(obj){
	$(obj).val("");
}

function repopulate(obj,newval){
	var val = $(obj).val();
	if(val == ""){
		$(obj).val(newval);
	}
}

/* RE	MOVE A TRAILING CHARACTER FROM A STRING */
function removeTrailing(fld) {
	var ln = fld.length;
	var end = ln - 1;
	return fld.substr(0,end);
}

/* ------------------------------------------------------------------------------------
	CHECKS IF A VALUE IS NUMERIC
	EXAMPLE:
		if(!IsNumeric(value)){
			var youSuck = 'true';
		}
--------------------------------------------------------------------------------------- */
function IsNumeric(sText){
	var ValidChars = "0123456789.-";
	var IsNumber=true;
	var Char;
	var usedPeriod = 0;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
		
		if(Char == "."){
			usedPeriod++;
		}
		
		if(usedPeriod > 1){
			IsNumber = false;
		}
	}
	return IsNumber;
}

/* ---------------------------------------------------------------------------- */
function formatPhone(obj,del,parens){
	/* This function formats a phone number as the user types
		This function takes three values
			obj = the identifier of the calling field
			* = the delimiter to be used
			yes/no = group area code in parenthesis or not 
		EXAMPLE: onkeyup="formatPhone(this,'-','no')" */
	var phone = $(obj).val();
	if(parens == 'yes'){
		phone = phone.substring(0,13);
	}else{
		phone = phone.substring(0,12);
	}
	var phoneLen = phone.length;
	var num = phoneLen - 1;
	var lastChar = phone.charAt(num);
	
	if(lastChar != del){
		if(!IsAllowed(lastChar)){
			phone = phone.replace(lastChar, "");
			$(obj).val(phone);
		}
	}
	
	switch(phoneLen)
	{
		case 1:
			if(parens == 'yes'){
				var C = phone.charAt(num);
				if(C != "("){
					newSTRING = "(" + C;
					$(obj).val(newSTRING);
				}
			}
		break;
		case 4:
			if(parens == 'no'){
				var C = phone.charAt(num);
				if(C != del){
					var newSTRING = phone.substring(0,num);
					newSTRING = newSTRING + del + C;
					$(obj).val(newSTRING);
				}
			}
		break;
		case 5:
			if(parens == 'yes'){
				var C = phone.charAt(num);
				if(C != ")"){
					var newSTRING = phone.substring(0,num);
					newSTRING = newSTRING + ")" + C;
					$(obj).val(newSTRING);
				}
			}
		break;
		case 8:
			if(parens == 'no'){
				var C = phone.charAt(num);
				if(C != del){
					var newSTRING = phone.substring(0,num);
					newSTRING = newSTRING + del + C;
					$(obj).val(newSTRING);
				}
			}
		break;
		case 9:
			if(parens == 'yes'){
				var C = phone.charAt(num);
				if(C != del){
					var newSTRING = phone.substring(0,num);
					newSTRING = newSTRING + del + C;
					$(obj).val(newSTRING);
				}
			}
		break;
		default:
			$(obj).val(phone);
	}

}
/* ---------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------
	CHECKS IF A STRING CONTAINS AN ALLOWABLE CHARACTER
	EXAMPLE:
		if(!IsAllowed(value)){
			var youSuck = 'true';
		}
--------------------------------------------------------------------------------------- */
function IsAllowed(sText){
	// A LIST OF ALLOWABLE CHARACTERS
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
/* ---------------------------------------------------------------------------- */

/* TRIM A STRING */
function trim(a){
	var result = a.replace(/^\s+|\s+$/g,"");
	return result;
}


/* ################      FORM SPECIFIC FUNCTIONS      ####################################################################### */
function checkZip(flow){ // CONTACT PAGE FORM
	if(flow == 'zipcheck'){
		var zip = $("#zipcode").val();
		if(zip.length < 5){
			alert('Please enter your zip code');
		}else if(zip == "FIND YOUR OFFICE BY ZIP CODE"){
			alert('Please enter your zip code');
		}else{
			return true;
		}
		
	}else{
		var ccmid = $("#ccmid").val();
		if(ccmid.length < 4){
			alert('Please enter your Broker Number');
		}else if (ccmid == "FIND YOUR OFFICE BY BROKER #"){
			alert('Please enter your Broker Number');
		}else{
			return true;
		}

	}
	
	return false;
}

/* ----------------------------------------------------------------------------- */

/* ----------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------- */
function checkZipCode(act){
	var err = "no";
	var msg = "";

	switch(act){
		case "zipcheck":
			var check = $("#zipcode").val();
				if(check.length < 5){
					err = "yes";
					msg = "Please enter a valid zip code";
				}
			break;
		case "knownknown":
			var check = $("#ccmid").val();
				if(check.length < 1){
					err = "yes";
					msg = "Please enter a member broker number";
				}
			break;
		case "unknownknown":
			var check = $("#reservationccmid").val();
				if(check.length < 1){
					err = "yes";
					msg = "Please enter a reservation number";
				}
			break;
		case "zipcheck2":
			var check = $("#zipcode2").val();
				if(check.length < 5){
					err = "yes";
					msg = "Please enter a valid zip code";
				}
			break;
		default:
			//"code to be executed if no case is met";
	}
	
	if(err == "yes"){
		alert("Form Failure:\n" + msg);
		return false;
	}else{
		return true;
	}
}

/* ---------------------------------------------------------------------------- */
// These two function remove default name and put it back if no name is given
function removeName(){
	var count = document.getElementById("nameCount").value;
	//alert(count);
	if(count == 0){
		document.getElementById("agencyBroker").value = "";
		document.getElementById("nameCount").value = 1;
	}
}

function putNameBack(){
	var name = document.getElementById("agencyBroker");
	var value = name.value;
	var size = value.length;
	if(size < 1){
		document.getElementById("agencyBroker").value = "First and Last Name";
		document.getElementById("nameCount").value = 0;
	}
}
/* ---------------------------------------------------------------------------- */
function zipcheckForm(){
	var zip = $("#zipcode").val();
	//alert(zip.length);
	if(zip.length < 5){
		alert("Please provide a 5 diget zip code.");
		return false;
	}else{
		return true;
	}
	
}

function hideOvers(){
	$("#zipList").hide();
	$("#inList").hide();
	$("#formListR").hide();
	$("#formListL").hide();
	$("#outList2").show();
	$("#outList").show();
}

function getContactInfo(state, sectionType){
	$("#contactInfo").html("<div align='left'>Loading</div>");
	$.post("plugins/contact2.lasso", { state: state, sectionType: sectionType },
   		function(html){
     			$("#contactInfo").show();
     			$("#contactInfo").html(html);
   		});
}

function getBrochureInfo(state, sectionType){
	$("#contactInfo").html("<div align='left'>Loading</div>");
	$.post("plugins/contact2.lasso", { state: state, sectionType: sectionType },
   		function(html){
     			$("#contactInfo").show();
     			$("#contactInfo").html(html);
   		});
}

