/** This function is a modification of "ValidateForm()" method 
 * Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function isEmail(emailID) {
	if ((emailID.value=="")||(emailID.value==null)||(echeck(emailID.value)==false)){
		//alert("Please Enter a valid Email ID");
		//emailID.focus();
		return false;
	}
	return true
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * This function is used by the function isEmail(emailID)  
 */
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

/**Zip Code Validation courtesy of: http://javascript.internet.com/forms/val-zip-code.html
 * Code validetes the zip is either in the form 12345 or 12345-6789
 
 <!-- Original:  Brian Swalwell -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
*/
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.value.length!=5 && field.value.length!=10) {
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		field.focus();
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			alert("Invalid characters in your zip code.  Please try again.");
			field.focus();
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
		alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
		field.focus();
		return false;
	    }
	}
	return true;
}

/**Validates the required info in list_your_properties.php is correct
 */
function checkData_listProperty()
{
  	//seller info
	if(document.listForm.sellerid == null || document.listForm.sellerid.value == ""){
		if(document.listForm.sci_name.value == ""){
			alert("Please enter a valid Name");
			document.listForm.sci_name.focus();
			return false;
		}
		if (!isEmail(document.listForm.sci_email)) {
			alert("Please enter a valid Email Address. ");
			document.listForm.sci_email.focus();
			return false;
		}
		if(document.listForm.sci_zip.value != ""){
			if(!validateZIP(document.listForm.sci_zip)){
				return false;
			}
		}
	}
	//property info
	if(document.listForm.pi_county.value == ""){
		alert("Please enter a valid County");
		document.listForm.pi_county.focus();
		return false;
	}
	if(document.listForm.pi_city.value == ""){
		alert("Please enter a valid City");
		document.listForm.pi_city.focus();
		return false;
	}
	if(document.listForm.pi_state.value == ""){
		alert("Please enter a valid State");
		document.listForm.pi_state.focus();
		return false;
	}
	if(!validateZIP(document.listForm.pi_zip)){
		return false;
	}
	if(document.listForm.pi_status.value == ""){
		alert("Please select a valid Current Status");
		document.listForm.pi_status.focus();
		return false;
	}
	if(document.listForm.pi_mls_type.value == ""){
		alert("Please select a valid MLS/Property Type");
		document.listForm.pi_mls_type.focus();
		return false;
	}
	if(!extensionOK(document.listForm.frontPhoto.value)){
		alert("Please choose a filetype of: .png, .jpg or .gif for the Front View");
		document.listForm.frontPhoto.focus();
		return false;
	}
	if(!extensionOK(document.listForm.rearPhoto.value)){
		alert("Please choose a filetype of: .png, .jpg or .gif for the Rear View");
		document.listForm.rearPhoto.focus();
		return false;
	}
	if(!extensionOK(document.listForm.livingPhoto.value)){
		alert("Please choose a filetype of: .png, .jpg or .gif for the Living Room View");
		document.listForm.livingPhoto.focus();
		return false;
	}
	if(!extensionOK(document.listForm.kitchenPhoto.value)){
		alert("Please choose a filetype of: .png, .jpg or .gif for the Kitchen View");
		document.listForm.kitchenPhoto.focus();
		return false;
	}
	if(!extensionOK(document.listForm.streetPhoto.value)){
		alert("Please choose a filetype of: .png, .jpg or .gif for the Street View");
		document.listForm.streetPhoto.focus();
		return false;
	}
	return true;
}

/**Validates the required info in register.php is correct
 */
function checkData_register()
{
  	if(document.registerForm.username.value == ""){
		alert("Please enter a Username");
		document.registerForm.username.focus();
		return false;
	}
	if(document.registerForm.password.value == ""){
		alert("Please enter a Password");
		document.registerForm.password.focus();
		return false;
	}
	if(!(document.registerForm.password.value == document.registerForm.password_r.value)){
		alert("Your passwords do not match");
		document.registerForm.password.focus();
		return false;
	}
	if (!isEmail(document.registerForm.email)) {
		alert("Please enter a valid Email Address. ");
		document.registerForm.email.focus();
		return false;
	 }
	 return true;
}
/**Validates the required info in referral.php is correct
 */
function checkData_referral() 
{
	if(!extensionOK(document.form_referral.fileToUpload.value)){
		alert("Please choose a filetype of: .png, .jpg or .gif");
		document.form_referral.fileToUpload.focus();
		return false;
	}
	if (document.form_referral.listing.value == "") {
		alert("Please select a Listing. ");
		document.form_referral.listing.focus();
		return false;
	};
	document.getElementById('loading').style.visibility ="visible";
	return true;
}
/** Validates the required info for updateReferral.php
 */
function checkData_referral_update(theForm, formNum){
	if(theForm.phone.length > 11){
		alert("The Phone Number can only be 11 digits.  Please reduce your entry to less than 11 digits");
		theForm.phone.focus();
		return false;
	}
	if(theForm.company.length > 50){
		alert("Company can only be 50 character.  Please reduce your entry to less than 50 characters");
		theForm.company.focus();
		return false;
	}
	if(theForm.website.length > 255){
		alert("The Website can only be 50 characters.  Please reduce your entry to less than 255 characters");
		theForm.website.focus();
		return false;
	}
	if(theForm.services.value.length > 255){
		alert("The Service Description can only be 255 characters.  Please reduce your entry to less than 255 characters");
		theForm.services.focus();
		return false;
	}
	document.getElementById('loading'+formNum).style.visibility ="visible";
	return true;

}
/**Validates the required info in updateProfile.php is correct
 */
function checkData_profile()
{
	if(document.profileForm.password.value == ""){
		alert("Please enter a Password");
		document.profileForm.password.focus();
		return false;
	}
	if(!(document.profileForm.password.value == document.profileForm.password_r.value)){
		alert("Your passwords do not match");
		document.profileForm.password.focus();
		return false;
	}
	if (!isEmail(document.profileForm.email)) {
		alert("Please enter a valid Email Address. ");
		document.profileForm.email.focus();
		return false;
	 }
	 return true;
}
/** Creates a popup window with the map location (i.e. maquest url) of the property.
 */
function showMap(location)
{
	var load=
	window.open(location,'',menubar=no,resizeable=yes,toolbar=no,location=no,status=no,height=600,width=800);
}

/**retrieves all the parameters from the referral form (referrals.php) 
 * and places them in the format needed for ajax call
 */
function getReferralParams(form)
{
	params = form.phone.name + "=" + form.phone.value + "&" +
			form.company.name + "=" + form.company.value + "&" +
			form.website.name + "=" + form.website.value + "&" +
			form.userid.name + "=" + form.userid.value + "&" +
			form.listing.name + "=" + form.listing.value + "&" +
			form.services.name + "=" + form.services.value + "&" +
			form.uploadedlogo.name + "=" + form.uploadedlogo.value;
	return params;
}

function autoTab(input,len) {
if(input.value.length >= len)
input.form[(getIndex(input)+1)].focus();
}

function getIndex(input) {
var index = -1, i = 0;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}

/** Checks to ensure the file extension chosen for the file is valid
*/
function extensionOK(fileName){
	if(fileName != ""){
		var extensions = new Array();
		extensions[0] = ".png";
		extensions[1] = ".jpg";
		extensions[2] = ".gif";
		var ext = fileName.substr(fileName.lastIndexOf("."));
		for(var i=0; i < extensions.length; i++){
			if(ext.toLowerCase() == extensions[i]) return true;
		}
		return false;
	}
	return true;
}

/** Callback from the submission of referrals.php */
function stopUpload(success, message){
      var result = '';
      if (success == 1){
         alert('Successfully Submitted Request');
      }
      else {
         alert('There was an error submitting your referral.');
         result = '<span class="emsg">There was an error submitting your referral:</span>';
      }
	  document.getElementById('result').innerHTML = result;
	  document.getElementById('message').innerHTML = message;
	  document.getElementById('loading').style.visibility ="hidden";
      return true;
}

/** Callback from the submission of updateReferral.php */
function updateComplete(success, message, formNum){
      var result = '';
      if (success == 1){
		//alert('Successfully Updated Referral');	
      }
      else {
         alert('There was an error updating your referral.');
         result = '<span class="emsg">There was an error updating your referral:</span>';
      }
	  document.getElementById('message'+formNum).innerHTML = message;
	  document.getElementById('loading'+formNum).style.visibility ="hidden";
      return true;
}

/** Function used to load calendars in list_your_properties.php */
function loadCalendars(){
	new JsDatePick({
		useMode:2,
		target:"pi_appraisal_date",
		dateFormat:"%Y-%m-%d"
	});
	
	new JsDatePick({
		useMode:2,
		target:"pi_listing_date",
		dateFormat:"%Y-%m-%d"
	});
};

/**bulk listing "href function" so we can put any test in the button class and do javascript onclick
*/
function bulkPropertyCheck(finished){
	if(!finished){
		document.getElementById('finished').value = "no";
	} else {
		document.getElementById('finished').value = "yes";
	}
	alert(document.getElementById('finished').value);
	return true;
}

function adminApproveReferral(refid, theStatusType){
	MakeRequest("ajax/approveReferral.php?refid=" + refid +'&statusType='+ theStatusType, "refDiv" + refid, "GET", null);
	return true;
}

/*Limit the amount of text entered into the textbox "services" in referrals.php and updateReferrals.php
  source adapted from http://www.mediacollege.com/internet/javascript/form/limit-characters.html
 */
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

