// JavaScript Document
/* ************* COMMON FUNCTION  STARTS **************  */

	var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	//var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	var phone=/(^\d{3}-\d{3}-\d{4}$)/;
	//var priceExp=/(^\d$)/;
	//var phone=/^\d(\d|-){7,20}/;
	var dateExp=/(^\d{2}[//]\d{2}[//]\d{4}$)/
	var numberExp = /^([0123456789.])+$/;
		//var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
//var ext=/^([a-zA-Z0-9_\.\-])+(.+[jpg]|[Jpg]|[JPG]|[gif]|[Gif]|[GIF]|[bmp|[Bmp]|[BMP]$)/;

//date validation
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate_new(dtStr){
	
	var dtCh ="/";
	var daysInMonth = DaysArray(12)
	
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid date.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date.")
		return false
	}
	return true;
}
/////////////

function ltrim(string){
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=0,j=0;i<string.length;i++)
    {
      if(j==0)
      {
        if(string.charAt(i)!=" ")
        {
            string1[j++]=string.charAt(i);
        }
      }
      else
      {
        string1[j++]=string.charAt(i);
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    } 
    return string;
  }

function rtrim(string)
  {
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=string.length;i>=0;i--)
    {
      if(string.charAt(i-1)==" ")
      {
        continue;
      }
      else
      {
        for(j=0;j<i;j++)
        {
          string1[j]=string.charAt(j);
        }
        break;
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    }
    
    
    return string;
  }
  
  function trim(string)
  {
    string=ltrim(string); // // This function is used to trim the left side of a String
    string=rtrim(string);// This function is used to trim the right side of a String
    return string;
  }


/* common function ends */

/************************************************************************************************************/
/* function is used to validate credicard number */
function validateCreditCardType(cardType,cardNum)
{//alert(cardType+cardNum);
   
   var card = /^\d{16}$/;
	var cardAmex=/^\d{15}$/;

	if(cardType=="Amex" && cardAmex.test(cardNum)==false)// 3400 0000 0000 009 
	{
		return false;
	}
	else if(cardType!="Amex" && card.test(cardNum)==false)
	{
		return false;
	}
	else if(cardType=="Visa" && cardNum.charAt(0)!=4)////Visa  4111 1111 1111 1111 
	{	
		return false;
	}	
	else if(cardType=="MasterCard" && cardNum.charAt(0)!=5)//MasterCard  5500 0000 0000 0004 
	{         
		return false;	   
	}
	else if(cardType=="AMEX" && cardNum.charAt(0)!=3)//American Express  3400 0000 0000 009 
	{
		return false;		
	}
	else 
	{
		return true;
	 }
}
/* function is used all popup open*/
function popupView(url)
{
	//alert(url);
	var newwindow;
	newwindow=window.open(url,'Window1','width=600,left=100,top=100,height=500,resizable=yes,scrollbars=yes,menubar=no,status=no');
	//newwindow=window.open(url,'Window1','width=700,left=0,top=0,height=700,resizable=yes,scrollbars=yes,menubar=no,status=no');
	if (window.focus) {newwindow.focus();}
	
}
/* function is used all popup open*/
function popupgmapView(url)
{
	//alert(url);
	var newwindow1;
	newwindow1=window.open(url,'Window2','width=500,left=100,top=100,height=400,resizable=yes,scrollbars=yes,menubar=no,status=no');
	//newwindow=window.open(url,'Window1','width=700,left=0,top=0,height=700,resizable=yes,scrollbars=yes,menubar=no,status=no');
	if (window.focus) {newwindow1.focus();}
	
}

/* function is used to validate admin login form(index.php)*/
function validateLogin()
{
	if(trim(document.frmlogin.username2.value) == ""){
		alert("Please enter username.");
		document.frmlogin.username2.focus();
		return false;
	}
	else if(trim(document.frmlogin.password2.value) == ""){
		alert("Please enter password.");
		document.frmlogin.password2.focus();
		return false;
	}
	else
	{
		return true;
	}
		
}
/* function ends */
/* function is used to validate add client details form(addclients.php)*/
function validateAddClients()
{
	if(trim(document.frmaddclients.company.value) == ""){
		alert("Please enter company name.");
		document.frmaddclients.company.focus();
		return false;
	}
	else if(trim(document.frmaddclients.fullname.value) == ""){
		alert("Please enter fullname.");
		document.frmaddclients.fullname.focus();
		return false;
	}
	else if(trim(document.frmaddclients.address.value) == ""){
		alert("Please enter address.");
		document.frmaddclients.address.focus();
		return false;
	}
	else if(trim(document.frmaddclients.city.value) == ""){
		alert("Please enter city.");
		document.frmaddclients.city.focus();
		return false;
	}
	else if(trim(document.frmaddclients.state.value) == "0"){
		alert("Please select state.");
		document.frmaddclients.state.focus();
		return false;
	}
	else if(trim(document.frmaddclients.zip.value) == ""){
		alert("Please enter zip.");
		document.frmaddclients.zip.focus();
		return false;
	}
	else if(trim(document.frmaddclients.email.value) == ""){
		alert("Please enter email address.");
		document.frmaddclients.email.focus();
		return false;
	}	
	else if(trim(document.frmaddclients.phone.value)!="" && !phone.test(document.frmaddclients.phone.value)){
		alert("Please enter phone number in correct format eg(xxx-xxx-xxxx).");
		document.frmaddclients.phone.focus();
		return false;
	}
	else if(!email.test(document.frmaddclients.email.value))
	{
	  	alert("Invalid email address.");
	  	document.frmaddclients.email.focus();
	  	 return false;
	}
	else if(trim(document.frmaddclients.username.value)==""){
		alert("Please enter username.");
		document.frmaddclients.username.focus();
		return false;
	}
	else if(trim(document.frmaddclients.password.value) == "" ){
		alert("Please enter password.");
		document.frmaddclients.password.focus();
		return false;
	}
	else if(trim(document.frmaddclients.password.value).length<6 ){
		alert("Password should be minimum 6 characters.");
		document.frmaddclients.password.focus();
		return false;
	}	
	else
	{
		return true;
	}
		
}
/* function is used to validate payment information -paymentinfo.php*/
function validatePayment(mode)
{
	if(mode=='order' && document.frmpayment.reporttype.value=="0")
	{
		alert("Please select report type.");
		document.frmpayment.reporttype.focus();
		return false;	
	}
	else if(trim(document.frmpayment.ccname.value) == ""){
		alert("Please enter name on credit card.");
		document.frmpayment.ccname.focus();
		return false;
	}
	else if(trim(document.frmpayment.cctype.value) == "0"){
		alert("Please select  credit card type.");
		document.frmpayment.cctype.focus();
		return false;
	}
	else if(trim(document.frmpayment.ccnumber.value) == ""){
		alert("Please enter credit card number.");
		document.frmpayment.ccnumber.focus();
		return false;
	}
	
	else if(validateCreditCardType(document.frmpayment.cctype.value,trim(document.frmpayment.ccnumber.value))==false){
		alert("Invalid  credit card number.");
		document.frmpayment.ccnumber.focus();
		return false;
	}
	else if(trim(document.frmpayment.ccmonth.value) == "0"){
		alert("Please select expiration month.");
		document.frmpayment.ccmonth.focus();
		return false;
	}
	else if(trim(document.frmpayment.ccmonth.value) == "0"){
		alert("Please select expiration month.");
		document.frmpayment.ccnumber.focus();
		return false;
	}
	else if(trim(document.frmpayment.ccyear.value) == "0"){
		alert("Please select expiration year.");
		document.frmpayment.ccyear.focus();
		return false;
	}
	else if(trim(document.frmpayment.billingaddress.value) == ""){
		alert("Please enter billing address.");
		document.frmpayment.billingaddress.focus();
		return false;
	}
	else if(trim(document.frmpayment.billingcity.value) == ""){
		alert("Please enter billing city.");
		document.frmpayment.billingcity.focus();
		return false;
	}
	else if(trim(document.frmpayment.billingstate.value) == "0"){
		alert("Please enter billing state.");
		document.frmpayment.billingstate.focus();
		return false;
	} 	
	else if(trim(document.frmpayment.billingzipcode.value) == ""){
		alert("Please enter billing zipcode.");
		document.frmpayment.billingzipcode.focus();
		return false;
	} 
	else if(trim(document.frmpayment.propertystreet.value) == ""){
		alert("Please enter property street address.");
		document.frmpayment.propertystreet.focus();
		return false;
	} 
	else if(trim(document.frmpayment.propertystate.value) == "0"){
		alert("Please enter property state.");
		document.frmpayment.propertystate.focus();
		return false;
	} 
	else if(trim(document.frmpayment.propertyzipcode.value) == ""){
		alert("Please enter property zipcode.");
		document.frmpayment.propertyzipcode.focus();
		return false;
	} 
	else
		clacLatLng();
}	
//////////////// function is used to validate properyinfo.php page -type=2 -user created by the admin /////////////////////////////

function validatePropertyinfo(mode)
{
	if(mode=="order" && document.frmpayment.reporttype.value=="0")
	{
		alert("Please select report type.");
		document.frmpayment.reporttype.focus();
		return false;
	}
	else if(trim(document.frmpayment.propertystreet.value) == ""){
		alert("Please enter property street address.");
		document.frmpayment.propertystreet.focus();
		return false;
	} 
	else if(trim(document.frmpayment.propertystate.value) == "0"){
		alert("Please enter property state.");
		document.frmpayment.propertystate.focus();
		return false;
	} 
	else if(trim(document.frmpayment.propertyzipcode.value) == ""){
		alert("Please enter property zipcode.");
		document.frmpayment.propertyzipcode.focus();
		return false;
	}
	else
		clacLatLng();
}

//calculate Latitude and Langitude
function clacLatLng()
{
	
	var geocoder = new GClientGeocoder();	
		var member_address=document.frmpayment.propertystreet.value+","+document.frmpayment.propertycity.value+","+document.frmpayment.propertystate.value+","+document.frmpayment.propertyzipcode.value;
		
		geocoder.getLatLng(member_address,function(point)
		{
			if(!point)
			{
			
				alert("Your details are not correct. Please enter Address,City and Zip Code correctly");
				document.frmpayment.propertystreet.focus();
				return false;
			}
			else
			{
				document.frmpayment.hidPoint.value=point;				
				document.frmpayment.status.value=1;
				if(document.getElementById("themeloading").style.display=="inline")
					document.getElementById("themeloading").style.display='block'; 
				document.frmpayment.submit();
				return true;
			}	
		}); 
	
}
 function googleMapLoad(lat,long) {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lat,long), 10);
      }
    }
/* google map view */	
	
function googleMapLoad1(lat,long)
{
     if (GBrowserIsCompatible())
	 {
	  		
		function createMarker(point)
		{
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("");
			});
			return marker;
		}
		
		// Display the map, with some controls and set the initial location 
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		// Set up marker with info window		
		var point = new GLatLng(lat,long);
		map.setCenter(point, 13);
		var marker = createMarker(point);
		map.addOverlay(marker);
	 }
 }

function changeBilladdress(address,city,state,zip)
{
	
	if(document.frmpayment.billstatus.checked)
	{
		document.frmpayment.billingaddress.value=document.frmpayment.billingaddress1.value;	
		document.frmpayment.billingcity.value=city;	
		document.frmpayment.billingstate.value=state;	
		document.frmpayment.billingzipcode.value=zip;	
	}
	else
	{
		document.frmpayment.billingaddress.value="";	
		document.frmpayment.billingcity.value="";	
		document.frmpayment.billingstate.value="0";	
		document.frmpayment.billingzipcode.value="";		
	}
}

/* ------propertyinfo.php ,paymentinfo.php function ends-------*/
/* function is used to ajax lisitng-reports.php*/
function reportsLinkList(url,page,index, type,so)
{
	var url  = url+"?page="+page+"&index="+index+"&type="+type+"&so="+so;
	if(trim(document.frmreports.id.value)!=="")
		url=url+"&id="+trim(document.frmreports.id.value);
	if(trim(document.frmreports.reportname.value)!=="")
		url=url+"&rpname="+trim(document.frmreports.reportname.value);
		//alert(url);
	passvalue(url,"div_results");
}

/*---------------*/
/* function is used to ajax lisitng-orders.php*/
function ordersLinkList(url,page,index, type,so)
{
	var url  = url+"?page="+page+"&index="+index+"&type="+type+"&so="+so;
	if(trim(document.frmreports.id.value)!=="")
		url=url+"&id="+trim(document.frmreports.id.value);
	
	passvalue(url,"div_results");
}

/*---------------*/
/*----------validate forgotpassword page -forgotpassword.php-------*/
function validateForgotpwd()
{
	if(trim(document.frmforgotpwd.email.value)=="")
	{
		alert("Please enter email address.");
		document.frmforgotpwd.email.focus();
		return false;
	}
	else if(!email.test(document.frmforgotpwd.email.value))
	{
	  	alert("Invalid email address.");
	  	document.frmforgotpwd.email.focus();
	  	 return false;
	}
	else
		return true;
}
/*-------------------------*/
/*---------------contactus.php-----------*/
function validateContact()
{
	if(trim(document.frmcontact.name.value)=="")
	{
		alert("Please enter name.");
		document.frmcontact.name.focus();
		return false;
	}	
	if(trim(document.frmcontact.state.value)=="0")
	{
		alert("Please select state.");
		document.frmcontact.state.focus();
		return false;
	}
	else if(trim(document.frmcontact.email.value)=="")
	{
		alert("Please enter email address.");
		document.frmcontact.email.focus();
		return false;
	}	
	else if(!email.test(document.frmcontact.email.value))
	{
	  	alert("Invalid email address.");
	  	document.frmcontact.email.focus();
	  	 return false;
	}
	else
		return true;
	
}

/*------appraisals.php-----------------------*/
function validateAppraisal()
{
	if(trim(document.frmappraisal.name.value)=="")
	{
		alert("Please enter name.");
		document.frmappraisal.name.focus();
		return false;
	}
	else if(trim(document.frmappraisal.phone.value)=="")
	{
		alert("Please enter phone number.");
		document.frmappraisal.phone.focus();
		return false;
	}
	else if(trim(document.frmappraisal.email.value)=="")
	{
		alert("Please enter email address.");
		document.frmappraisal.email.focus();
		return false;
	}
	else if(!email.test(document.frmappraisal.email.value))
	{
	  	alert("Invalid email address.");
	  	document.frmappraisal.email.focus();
	  	 return false;
	}
	else if(trim(document.frmappraisal.altphone.value)=="")
	{
		alert("Please enter alternative phone number.");
		document.frmappraisal.altphone.focus();
		return false;
	}
	else if(trim(document.frmappraisal.city.value)=="")
	{
		alert("Please enter city.");
		document.frmappraisal.city.focus();
		return false;
	}
	else if(trim(document.frmappraisal.state.value)=="0")
	{
		alert("Please select state.");
		document.frmappraisal.state.focus();
		return false;
	}
	else if(trim(document.frmappraisal.zip.value)=="")
	{
		alert("Please enter zipcode.");
		document.frmappraisal.zip.focus();
		return false;
	}
	else if(trim(document.frmappraisal.address.value)=="")
	{
		alert("Please enter address.");
		document.frmappraisal.address.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propbrname.value)=="")
	{
		alert("Please enter borrowers name.");
		document.frmappraisal.propbrname.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propcontact.value)=="")
	{
		alert("Please enter contact for access.");
		document.frmappraisal.propcontact.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propsphoneh.value)=="")
	{
		alert("Please enter subject phone (Home).");
		document.frmappraisal.propsphoneh.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propaddress.value)=="")
	{
		alert("Please enter subject address.");
		document.frmappraisal.propaddress.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propcity.value)=="")
	{
		alert("Please enter property city.");
		document.frmappraisal.propcity.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propstate.value)=="0")
	{
		alert("Please select property state.");
		document.frmappraisal.propstate.focus();
		return false;
	}
	else if(trim(document.frmappraisal.propzip.value)=="")
	{
		alert("Please enter property zip code.");
		document.frmappraisal.propzip.focus();
		return false;
	}
	else if(document.frmappraisal.proppayment[0].checked==false && document.frmappraisal.proppayment[1].checked==false && document.frmappraisal.proppayment[2].checked==false)
	{
		alert("Please select payment method");
		document.frmappraisal.proppayment[0].focus();
		return false;
	}
	else
	return true;;
	//name phone email altphone address city zipcode propbrname propcontact propsphoneh propaddress propcity propzip
}
/*------------*/
/*---------function is used to vaiidate cuttax form - index.php-----------------------*/
function validateCutuax()
{
	if(trim(document.frmcuttax.cutname.value)=="")
	{
		alert("Please enter name.");
		document.frmcuttax.cutname.focus();
		return false;
	}
	else if(trim(document.frmcuttax.cutemail.value)=="")
	{
		alert("Please enter email.");
		document.frmcuttax.cutemail.focus();
		return false;
	}
	else if(!email.test(document.frmcuttax.cutemail.value))
	{
		alert("Invalid email address.");
		document.frmcuttax.cutemail.focus();
		return false;
	}
	else
		return true;
}
/*------------------------*/
/*---------function is used to vaiidate cuttax form - leftpanel.php-----------------------*/
function validateCutuax1()
{
	if(trim(document.frmcuttax1.cutname.value)=="")
	{
		alert("Please enter name.");
		document.frmcuttax1.cutname.focus();
		return false;
	}
	else if(trim(document.frmcuttax1.cutemail.value)=="")
	{
		alert("Please enter email.");
		document.frmcuttax1.cutemail.focus();
		return false;
	}
	else if(!email.test(document.frmcuttax1.cutemail.value))
	{
		alert("Invalid email address.");
		document.frmcuttax1.cutemail.focus();
		return false;
	}
	else
		return true;
}
/*------------------------*/