

function toggleBizFields(setdisplay) {
   thediv = document.getElementById("bizformfields");
   if(setdisplay == 'none' ){
       thediv.style.display='none';
   }else{ 
      thediv.style.display='block';
   }
}

 function PasswordValidation()
 {
   var frm = document.forms["createaccount"];
   if(frm.pass1.value != frm.pass2.value)
   {
     alert('The password and verified password don not match.');
     return false;
   }
   else
   {
     return true;
   }
 }
 
 
function validateUserUpdate(theForm) {
   var reason = "";
   clear_div_msg("createaccount_errorloc");
   
   
   if (theForm.pass1.value.length > 0 || theForm.pass2.value.length > 0) { 
   	reason += validatePassword(theForm.pass1,theForm.pass2);
   }
   reason += validateEmpty(theForm.firstname,"First Name");
   reason += validateEmpty(theForm.lastname,"Last Name");
   if (theForm.login.value != theForm.email.value) {
   	reason += validateEmail(theForm.email);
   	reason += $.ajax({
	         url: "/check.php",
	         async: false,
	         type: "GET",
	         data: ({email: theForm.email.value}),
	         dataType: "html"
         }).responseText
   }
   if (theForm.nick.value != theForm.nickname.value) {
      	reason += validateNick(theForm.nickname);
      	reason += $.ajax({
	         url: "/check.php",
	         async: false,
	         type: "GET",
	         data: ({nick: theForm.nickname.value}),
	         dataType: "html"
         }).responseText

   }
   if (theForm.typeid.value == "2") {
   	reason += validateEmpty(theForm.bizname,"Business Name");
   	reason += validateEmpty(theForm.city,"Business City");
   	reason += validateEmpty(theForm.state,"Business State");
   	reason += validateEmpty(theForm.zip,"Business Zip");
   	reason += validatePhone(theForm.phone);
   	reason += validateEmpty(theForm.address,"Business Address");   
   }  
   
   if (reason != "") {
     show_div_msg("createaccount_errorloc","<p  class='error_strings'>Some fields need correction:<br>" + reason+"</p><br>");
     return false;
   }
 
   return true;
}

function validateCreateAccount(theForm) {
   var reason = "";
   
   clear_div_msg("createaccount_errorloc");
   
   reason += validatePassword(theForm.pass1,theForm.pass2);
   reason += validateEmail(theForm.email);
   reason += validateEmpty(theForm.firstname,"First Name");
   reason += validateEmpty(theForm.lastname,"Last Name"); 
   reason += validateNick(theForm.nickname);

   if ($('input:radio[name=type]:checked').val() == 2) {
	reason += validateEmpty(theForm.name,"Business Name");
	reason += validateEmpty(theForm.city,"Business City");
	reason += validateEmpty(theForm.state,"Business State");
	reason += validateEmpty(theForm.zip,"Business Zip");
	reason += validatePhone(theForm.phone);
	reason += validateEmpty(theForm.address,"Business Address");   
   }  

   var checkEmail = $.ajax({
         url: "/check.php",
         async: false,
         type: "GET",
         data: ({email: theForm.email.value}),
         dataType: "html"
         }).responseText
   
   var checkNick = $.ajax({
            url: "/check.php",
            async: false,
            type: "GET",
            data: ({nick: theForm.nickname.value}),
            dataType: "html"
         }).responseText
   
   reason += checkEmail;
   reason += checkNick;
   
   if (checkEmail.length >0) theForm.email.style.background    = 'url(/images/search-field-bg-er.gif) center left no-repeat';
   if (checkNick.length  >0) theForm.nickname.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
   
   if (reason != "") {
        show_div_msg("createaccount_errorloc","<p  class='error_strings'>Some fields need correction:<br>" + reason+"</p><br>");
        return false;
   }
    
   return true;
}


function updateFormCoord(response) {
	
	clear_div_msg('createaccount_errorloc');
	
	if (!response || response.Status.code != 200) {
		show_div_msg("createaccount_errorloc","<p  class='error_strings'>That address is not locatable for Google Maps, please change it.</p><br>");
		
		
	} else {
		place = response.Placemark[0];
		// Retrieve the latitude and longitude & Update Form

		var lat = place.Point.coordinates[1];
		var lng = place.Point.coordinates[0];

		$("#lat").val(lat);
		$("#lng").val(lng);
		
		//  Create Point
		point = new GLatLng(lat,lng);
		// Create a marker
		
		map.removeOverlay(marker);
		marker = new GMarker(point);
		
		// Add the marker to map
		map.addOverlay(marker);
		map.setCenter(point, 14);
		
		$('#submitFormButton').show('slow');
		$('#validateAddress').hide('slow');
	}
	
}

function validateAddress() {
   
   var reason = "";
   
   theForm = $("#createBusiness").get(0); 
   geocoder = new GClientGeocoder();
   geocoder.getLocations(theForm.address.value + ", " + theForm.city.value + ", " + theForm.state.value + "  " + theForm.zip.value, updateFormCoord);
}

function revalidateAddress() {
   
   if ($('#submitFormButton').is(":visible")) {
   	$('#submitFormButton').hide('slow');
	$('#validateAddress').show('slow');
   }
}

 function validateBizAdd() {
   
   var reason = "";
   theForm = $("#createBusiness").get(0);  
   clear_div_msg('createaccount_errorloc');
    
   reason += validateEmpty(theForm.name,"Business Name");
   reason += validateSelect(theForm["category[]"],"Category");
   reason += validateEmpty(theForm.city,"Business City");
   reason += validateEmpty(theForm.state,"Business State");
   reason += validateEmpty(theForm.zip,"Business Zip");
   reason += validatePhone(theForm.phone);
   reason += validateEmpty(theForm.address,"Business Address");
   
   if (reason != "") {
     show_div_msg("createaccount_errorloc","<p  class='error_strings'>Some fields need correction:<br>" + reason+"</p><br>");
     return false;
   }

   $("#createBusiness").submit();
   
   return true;

}


 function validateNewDeal(theForm) {
   var reason = "";
   clear_div_msg("createaccount_errorloc");
     
   reason += validateTextareaEmpty(theForm.shortdescription,"Short Description");   
   reason += validateDateSpread(theForm.sdate, theForm.edate);
   reason += validateEmpty(theForm.stime, "Start Time ");
   reason += check_image(theForm.imagefile1);
 
   if (reason != "") {
     show_div_msg("createaccount_errorloc","<p  class='error_strings'>Some fields need correction:\n<br>" + reason+"</p><br>");
     return false;
   }
   return true;
}


 function validateResetOnSubmit(theForm) {
   var reason = "";
   clear_div_msg("createaccount_errorloc");
   reason += validateEmail(theForm.email);
   
    
   if (reason != "") {
     show_div_msg("createaccount_errorloc","<p  class='error_strings'>Not a valid email address:\n<br>" + reason+"</p><br>");
     return false;
   }
 
   return true;
}

function validateTextareaEmpty(fld, name) {
    var error = "";
 
    if (!fld) {
        error = "Please fill in the "+name+" field.\n<br>"
    }
    else if (fld.value.length == 0) {
        fld.style.background = 'url(/images/textarea-field-bg-er.gif) center left no-repeat'; 
        error = "The "+name+" field has not been filled in.\n<br>"
    } else {
        fld.style.background = 'url(/images/textarea-field-bg.gif) center left no-repeat';
    }
    return error;  
}


function validateEmpty(fld, name) {
    var error = "";
 
    if (!fld) {
        error = "Please fill in the "+name+" field.\n<br>"
    }
    else if (fld.value.length == 0) {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat'; 
        error = "The "+name+" field has not been filled in.\n<br>"
    } else {
        fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
    }
    return error;  
}

function validateSelect(fld, name) {
    var error = "";
 
    if (fld.value == "") {
        error = "Please fill in the "+name+" field.\n<br>"
    } 
    return error;  
}


function validateDateSpread(fld1, fld2) {
    
    var error = validateEmpty(fld1,"Start Date") + validateEmpty(fld2,"End Date");

    if (error == "") {
    
	    var d1 = Date.parse(fld1.value.replace(/-/g,"/"));
	    var d2 = Date.parse(fld2.value.replace(/-/g,"/"));
	    
	    if (d1 > d2) {
		error += "The start date is after the end date, should these be reversed?\n<br>";
	    }
    }
    
    return error;  
}

function validateTimeSpread(fld1, fld2, name) {
    var error = "";
 
   //
    return error;  
}



function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    //else if ((fld.value.length < 5) || (fld.value.length > 15)) {
 //        fld.style.background = 'Yellow'; 
 //        error = "The username is the wrong length.\n";
 //   } 
 
 
    if (fld.value == "") {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat'; 
        error = "You didn't enter an username.\n<br>";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat'; 
        error = "The username can only contain letters & numbers.\n<br>";
    } else {
        fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
    }
    return error;
}


function validatePassword(fld,fld2) {
    var error = "";
    
   // var illegalChars = /[\W_]/; // allow only letters and numbers 
   // else if (illegalChars.test(fld.value)) {
   //        error = "The password contains illegal characters.\n<br>";
   //        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeatw';
  // }
    //else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
       //   error = "The password must contain at least one numeral.\n";
       //    fld.style.background = 'Yellow';
    //} 
  
    if (fld.value == "") {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        fld2.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        error = "You didn't enter a password.\n<br>";
    } else if(fld.value != fld2.value) {
        error = "The passwords do not match.\n<br>";
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        fld2.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        error = "The password has to be between 3 & 20 characters. \n";
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        fld2.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
    }
    else {
        fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
        fld2.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
    }
   return error;
}   


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                       
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;  //"
    
    
    if (fld.value == "") {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        error = "You didn't enter an email address.\n<br>";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        error = "Please enter a valid email address.\n<br>";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        error = "The email address contains illegal characters.\n<br>";
    } else {
        fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
    }
    return error;
}

function validateNick(fld) {
    var error="";
    var tfld = trim(fld.value); // value of field with whitespace trimmed off
    
    if (fld.value == "") {
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
        error = "You didn't enter a Display Name.\n<br>";
    }  else {
        fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
    }
    return error;
}


function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n<br>";
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n<br>";
        fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
    }
    else {
           fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
    }
    return error;
}

function validatePhone(fld) {
 /* 
 * 7181238748 to (718)123-8748 
 */  
 
 var num=fld.value;
 
 var returnval = "";   
 var re = /[^0123456789]/gi; 
 var numCleaned=num.replace(re,''); 
 
 if(numCleaned.length == 10) {  
	  fld.value = '('+numCleaned.substring(0,3)+')'+numCleaned.substring(3,6)+'-'+numCleaned.substring(6,10);
	  fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
 } else {
	  returnval = "You must enter a valid US phone number with area code"; 
	  fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
 }
 return returnval; 
}  


function show_div_msg(divname,msgstring)
{
	if(divname.length<=0) return false;

	if(document.layers)
	{	
	        msgstring = removeHTMLTags(msgstring);
		alert(msgstring);
		return false;   
	}
	else
	if(document.all)
	{
		divlayer = document.all[divname];
		if(!divlayer){
			msgstring = removeHTMLTags(msgstring);
			alert(msgstring);
			return false;
		}
		divlayer.innerHTML= divlayer.innerHTML + msgstring;
	}
	else
	if(document.getElementById)
	{
		divlayer = document.getElementById(divname);
		if(!divlayer){
		   msgstring = removeHTMLTags(msgstring);
		   alert(msgstring);
		   return false;
		}
		divlayer.innerHTML = divlayer.innerHTML + msgstring;
	}
	divlayer.style.visibility="visible";	
	return false;
}

function clear_div_msg(divname)
{
	if(divname.length<=0) return false;

	if(document.layers)
	{
		divlayer = document.layers[divname];
               	divlayer.document.open();
		divlayer.document.write("");
		divlayer.document.close();
	}
	else
	if(document.all)
	{
		divlayer = document.all[divname];
                divlayer.innerHTML= "";
	}
	else
	if(document.getElementById)
	{
		divlayer = document.getElementById(divname);
                divlayer.innerHTML = "";
	}
	divlayer.style.visibility="hidden";	
	return false;
}


function removeHTMLTags(strInputCode){
	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;	
}

function check_image(fld) {

	var imgpath;
	var result = "";
	
	imgpath = fld.value;

	if(imgpath != "") {

		// code to get File Extension..
		var arr1 = new Array;
		arr1 = imgpath.split("\\");
		var len = arr1.length;
		var img1 = arr1[len-1];
		var filext = img1.substring(img1.lastIndexOf(".")+1);

		// Checking Extension

		if(filext != "jpg" && filext != "jpeg" && filext != "gif" && filext != "png" && filext != "jpg" && filext != "JPG" && filext != "GIF" && filext != "PNG")  {
			result += "Image is not a valid type, must be a jpeg, jpg, png, or gif file.\n";
			fld.style.background = 'url(/images/search-field-bg-er.gif) center left no-repeat';
		} else {
			fld.style.background = 'url(/images/search-field-bg.gif) center left no-repeat';
		}

	}

	
	return result;

}