/*######################################################################################################################
Title			: Paydayloan :: Main Section
Project Name	: Paydayloan
File Name		: js-script.js
Description		: This script contains client side validation methods for the forms available in all html files

Modified By		: Modified Date
---------------------------------
Ayyappan          10-Dec-2009
---------------------------------

########################################################################################################################*/

url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
var  http_req;

//newcust script file
var strdevelopmentMode="live";
var strNewCustUrl="",strRetCustUrl="",strPdlUrl="";
if(strdevelopmentMode=="demo")
{
  strPdlUrl="http://dev.paydayloan.com/";
  strNewCustUrl="http://dev.paydayloan.com/newcust/";	 
  strRetCustUrl="http://dev.paydayloan.com/retcust/";	 
}
else if(strdevelopmentMode=="live")
{
 strPdlUrl="https://www.paydayloan.com/";
 strNewCustUrl="https://www.paydayloan.com/newcust/";	 
 strRetCustUrl="https://www.paydayloan.com/retcust/";	 
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function funValidateNewCustomersForm(formname)
{
  with(document.forms[formname])
  {     
   var strState = drpdwn_newcus_State.value;
   strState= strState.toUpperCase();   
   var strFrstName = txt_newcus_FirstName.value;
   var strLstName = txt_newcus_LastName.value;
   var strEmail = txt_newcus_Email.value;
   if(strFrstName=="")
   {
		alert("Please enter first name");
		txt_newcus_FirstName.focus();
		return false;
   }
   else if(strLstName=="")
   {
		alert("Please enter last name");
		txt_newcus_LastName.focus();
		return false;
   }
   else if(strEmail=="")
   {
		alert("Please enter email");
		txt_newcus_Email.focus();
		return false;		
   }
   else
   {
		if(strState=="")
		{
		   action=strNewCustUrl+"dsp_zipcode.aspx";
		   submit();
		}
		else if(strState=="CA" || strState=="ID")
		{
		  action=strNewCustUrl+"LoanApplication.aspx";
		  submit();
		}
		else if(strState=="CO")
		{
		  document.location.href=strPdlUrl+"CanNotServe.html";		  
		}
		else
		{
		  //function used to get and store the New Customer box values into Xml file
		  funStoreUserDetails(formname);
		}
	}

  }//with ends
}

//function used to get and store the New Customer box values into Xml file
function funStoreUserDetails(formname)
{
  with(document.forms[formname])
  {     
	   var strFrstName = txt_newcus_FirstName.value;
	   var strLstName = txt_newcus_LastName.value;
	   var strEmail = txt_newcus_Email.value;
	   var strState = drpdwn_newcus_State.value;
	   var strPaymentType = drpdwn_newcus_PaymentType.value;
	   var strPaymentPeriod = drpdwn_newcus_PaymentPeriod.value;
	   var StringToSend = "FirstName="+strFrstName+"&LastName="+strLstName+"&Email="+strEmail+"&PaymentType="+strPaymentType+"&PaymentMethod="+strPaymentPeriod+"&State="+strState;
		
	   if(strPaymentType=="Direct Deposit")
		strPaymentType=1;
	   else
		strPaymentType=0;

	   switch(strPaymentPeriod)
	   {
		 case "Weekly":strPaymentPeriod=52;break;
		 case "Monthly":strPaymentPeriod=12;break;
		 case "Twice per Month":strPaymentPeriod=24;break;
		 case "Every Two Weeks":strPaymentPeriod=26;break;
	   }
	
	  var Qrystring="first_name="+strFrstName+"&last_name="+strLstName+"&email="+strEmail+"&state="+strState+"&direct_deposit="+strPaymentType+"&pay_frequency="+strPaymentPeriod;

	  var strthirdPartyUrl="http://www.assurancepayday.com/LFpost.aspx?LF=c9b56bac-d7db-dc11-9bac-00145e7aa2f0&aid=LF563&"+Qrystring;
	  if(strFrstName!="" || strLstName!="" || strEmail!="")
	  {
			url = document.location.href;
			xend = url.lastIndexOf("/") + 1;
			var base_url = url.substring(0, xend);
			
	        if(strdevelopmentMode=="local")
			 url="store_users.aspx?"+StringToSend;
			else
			 url="newcust/store_users.aspx?"+StringToSend;

			//Does URL begin with http?
			if(url.substring(0, 4) != 'http')
			{
			  url = base_url + url;		 
			}
			
			http_req = null;
			if(window.XMLHttpRequest) http_req = new XMLHttpRequest();
			else if(window.ActiveXObject) http_req = new ActiveXObject("Microsoft.XMLHTTP");

			if(http_req)
			{
				http_req.onreadystatechange = function()
				{ 
					if(http_req.readyState==4)
					{			
					  strthirdPartyUrl=http_req.responseText;		
					  if(strthirdPartyUrl!="" && strthirdPartyUrl!="error")
					  {
						  //added by ayyappan on dec-10-2009
						if(strthirdPartyUrl.indexOf("click.linkstattrack.com/zoneId")>-1)
						{						     	 Qrystring="name_first="+strFrstName+"&name_last="+strLstName+"&email_primary="+strEmail+"&home_state="+strState;
						}

                        if(strthirdPartyUrl.indexOf("http://")<0 && strthirdPartyUrl.indexOf("https://")<0)
						 strthirdPartyUrl="http://"+strthirdPartyUrl;
                        
						if(strthirdPartyUrl.indexOf("?")<0)
						 strthirdPartyUrl=strthirdPartyUrl+"?"+Qrystring;
						else
						 strthirdPartyUrl=strthirdPartyUrl+"&"+Qrystring;
					    			
						 document.location=strthirdPartyUrl;
					  }
					  
					}//http_req.readyState== 4 check ends
				};

				http_req.open("GET", url, true);
				http_req.send("");

		  }//http_req check ends
	  }
	  else
	  {
	    document.location=strthirdPartyUrl;
	  }

   }//with ends
}

//function used to valiate thedsp_zipcode form
function funValidateDsp_ZipcodeForm(formname)
{
  with(document.forms[formname])
  {
	var strState = trimAll(drpdwn_dsp_State.value);
	strState= strState.toUpperCase();
   	if(strState=="")
	{
	  alert("Please select state");
	  drpdwn_dsp_State.focus();
	  return false;
	}
	else if(strState=="CO")
	{
	  document.location.href=strPdlUrl+"CanNotServe.html";		  
	}
	else 
	{		 
	  return true;
	}
  }//with ends
}

//function used to perform the array search for strings
function funChrArraySearch(arrInput,strElmnttoSrch)
{
   var BoolElmntExist= false;
   for(elmnt=0;elmnt<arrInput.length;elmnt++)
   {	
     if(arrInput[elmnt]==strElmnttoSrch)
	  {
        BoolElmntExist= true;
		break;
	  }
   }
  return BoolElmntExist;
}

function funValidateReturnCustomerLogin(formname)
{
  with(document.forms[formname])
  {
	   var strEmail = txt_retcus_email.value;
	   var strPassword = txt_retcus_Password.value;
	   
		if(strEmail=="")
		{
		 alert("Please enter your email");
		 txt_retcus_email.focus();
		 return false;
		}
		else if(strPassword=="")
		{
		 alert("Please enter your password");
		 txt_retcus_Password.focus();
		 return false;
		}
		else
		{	  
		  action=strRetCustUrl+"Login.aspx";
		  submit();
		  return true;
		}
  }
}

//Loan Application form scripts starts

function funValidateCoveredBorrowerInformation()
{
  with(document.forms["frmLoanApplication"])
  {	  
	if(funCheckPhoneNumber("tdcApp_Customer_txtCustHomePhone")==false)
	{
      alert("Customer Information: Please enter a valid Home Phone Number");
	  document.getElementById("tdcApp_Customer_txtCustHomePhone").focus();
	  return false;
	}
	else if(funCheckPhoneNumber("tdcApp_Employer_txtEmpPhone")==false)
	{
	  alert("Employer Information: Please enter a valid Employer Work Phone Number");
	  document.getElementById("tdcApp_Employer_txtEmpPhone").focus();
	  return false;
	}

	 //for last and next pay date
	var BolLastPaydateRetVal = funCheckLastPayDate();
	if(BolLastPaydateRetVal==true)
	{
	  var BolNextPaydateRetVal = funCheckNextPayDate();
      if(BolNextPaydateRetVal==false)
       return false;
	}
	else
	 return false;

	if(document.getElementById("tdcApp_Reference_txtRefHomePhone").value!="" && funCheckPhoneNumber("tdcApp_Reference_txtRefHomePhone")==false)
	{
	  alert("Relative Information:  Please enter a valid Phone Number for your first relative listed.  This must be 10 digits.");
	  document.getElementById("tdcApp_Reference_txtRefHomePhone").focus();
	  return false;
	}	
	else if(document.getElementById("tdcApp_Reference_txtRefHomePhone2").value!="" && funCheckPhoneNumber("tdcApp_Reference_txtRefHomePhone2")==false)
	{
	  alert("Relative Information:  Please enter a valid Phone Number for your 2nd relative listed.  This must be 10 digits.");
	  document.getElementById("tdcApp_Reference_txtRefHomePhone2").focus();
	  return false;
	}	
	else
	{
  	  return true;
	}	
  }
}

function funCheckLastPayDate()
{
  var BolOutput=true;
  with(document.forms["frmLoanApplication"])
  {	
	   var strServerDate = hdn_server_date.value;
	   var strLastPayDate = document.getElementById("tdcApp_Payroll_txtPayrollLastPayDate").value;
       var BolIsHoliday = funDateIsHolidaysCheck(strLastPayDate);
       
	   if(strLastPayDate=="" || funIsValidDate(strLastPayDate)==false || BolIsHoliday==true)
	   {
		  alert("Please enter a business day in the format of mm/dd/yyyy");
		  document.getElementById("tdcApp_Payroll_txtPayrollLastPayDate").focus();
		  return false;
	   }
	   else 
	   {
          return true;
	   }
  }
}

function funCheckLastPayDate_01()
{
  var BolOutput=true;
  with(document.forms["frmLoanApplication"])
  {	
	   var strServerDate = hdn_server_date.value;
	   var strLastPayDate = document.getElementById("tdcApp_Payroll_txtPayrollLastPayDate").value;
       var BolIsHoliday = funDateIsHolidaysCheck(strLastPayDate);
       
	   if(strLastPayDate=="" || funIsValidDate(strLastPayDate)==false || BolIsHoliday==true)
	   {
		  alert("Please enter a business day in the format of mm/dd/yyyy");
		  document.getElementById("tdcApp_Payroll_txtPayrollLastPayDate").focus();
		  return false;
	   }
	   else 
	   {
		 //0 => Weekly --- 1 => Bi-Weekly --- 2 => Twice Monthly --- 3 => Monthly
		 var ArrDifToCheck =new Array(7,14,17,32);
		 var intDayDifToCheck=0;
		 for(typ=0;typ<ArrDifToCheck.length;typ++)
		 {
			 if(document.getElementById("tdcApp_Payroll_optPeriodicity_"+typ).checked)
			 {
			   intDayDifToCheck=ArrDifToCheck[typ];
			   break;
			 }
		 }
          
	     //find day dif between current server date and entered Next pay date
		 intDayDifToCheck = parseInt(intDayDifToCheck);
		 if(intDayDifToCheck>0)
		 {
		   var intActualDaydif=Fundatedifference(strLastPayDate,strServerDate);
		   intActualDaydif=parseInt(intActualDaydif);
		   
		   if(intActualDaydif<0)
		   {
		      alert("The last pay date shouldn't be future date");
			  document.getElementById("tdcApp_Payroll_txtPayrollLastPayDate").focus();
			  BolOutput= false;
		   }
		   else if(intActualDaydif>intDayDifToCheck)
		   {
		      alert("The last pay date cannot be more than "+intDayDifToCheck+" days in the past");
			  document.getElementById("tdcApp_Payroll_txtPayrollLastPayDate").focus();
			  return false;
		   }
		 }
		 
	   }//else part ends
 
    return BolOutput;

  } //with ends
}

function funCheckNextPayDate()
{
  var BolOutput=true;
  with(document.forms["frmLoanApplication"])
  {	
	   var strServerDate = hdn_server_date.value;
	   var strNextPayDate = document.getElementById("tdcApp_Payroll_txtPayrollNextPayDate").value;
       var BolIsHoliday = funDateIsHolidaysCheck(strNextPayDate);
	   if(funIsValidDate(strNextPayDate)==false || BolIsHoliday==true)
	   {
		 alert("Please enter a business day in the format of mm/dd/yyyy");
		 document.getElementById("tdcApp_Payroll_txtPayrollNextPayDate").focus();
		 BolOutput= false;
	   }
	  
     return BolOutput;
  }
}

function funCheckNextPayDate_01()
{
  var BolOutput=true;
  with(document.forms["frmLoanApplication"])
  {	
	   var strServerDate = hdn_server_date.value;
	   var strNextPayDate = document.getElementById("tdcApp_Payroll_txtPayrollNextPayDate").value;
       var BolIsHoliday = funDateIsHolidaysCheck(strNextPayDate);

	   if(funIsValidDate(strNextPayDate)==false || BolIsHoliday==true)
	   {
		  alert("Please enter a business day in the format of mm/dd/yyyy");
		  document.getElementById("tdcApp_Payroll_txtPayrollNextPayDate").focus();
		  BolOutput= false;
	   }
	   else 
	   {
		 //0 => Weekly --- 1 => Bi-Weekly --- 2 => Twice Monthly --- 3 => Monthly
		 var ArrDifToCheck =new Array(7,14,17,32);
		 var intDayDifToCheck=0;
		 for(typ=0;typ<ArrDifToCheck.length;typ++)
		 {
			 if(document.getElementById("tdcApp_Payroll_optPeriodicity_"+typ).checked)
			 {
			   intDayDifToCheck=ArrDifToCheck[typ];
			   break;
			 }
		 }
          
	     //find day dif between current server date and entered Next pay date
		 intDayDifToCheck = parseInt(intDayDifToCheck);
		 if(intDayDifToCheck>0)
		 {
		   var intActualDaydif = parseInt(Fundatedifference(strServerDate,strNextPayDate));
		   if(intActualDaydif<0)
		   {
		      alert("The next pay date shouldn't be past date");
			  document.getElementById("tdcApp_Payroll_txtPayrollNextPayDate").focus();
			  BolOutput= false;
		   }
		   else if(intActualDaydif>intDayDifToCheck)
		   {
		      alert("The next pay date cannot be more than "+intDayDifToCheck+" days in the future");
			  document.getElementById("tdcApp_Payroll_txtPayrollNextPayDate").focus();
			  BolOutput= false;
		   }
		 }
		 
	  }//else part ends
 
    return BolOutput;

  } //with ends
}

//This function returns true if the passed date is available in list holiays or sunday/saturday
function funDateIsHolidaysCheck(Inputdate)
{
   var BolOutput=false;
   if(Inputdate!="")
	{
		var Caldate = new Date(Inputdate); 
		DayNameIndx= parseInt(Caldate.getDay());

		if(DayNameIndx==0 || DayNameIndx==6)//Check saturday or sunday
		{
		  BolOutput=true;
		}
		else
		{
		  var CalYr = Caldate.getFullYear();
		  var CalMon = Caldate.getMonth();     // integer, 0-11
		  var CalDay = Caldate.getDate();

			 var arrHolidays=new Array("05/26/2008","07/04/2008","09/01/2008","10/13/2008","11/11/2008","11/27/2008","12/25/2008","01/01/2009","01/19/2009","02/16/2009","05/25/2009","07/04/2009","09/07/2009","10/12/2009","11/11/2009","11/26/2009","12/25/2009");
			 for(hlDy=0;hlDy<arrHolidays.length;hlDy++)
			 {
				var HoliDate = new Date(arrHolidays[hlDy]); 
				var HolYr = HoliDate.getFullYear();
				var HolMon = HoliDate.getMonth();     // integer, 0-11
				var HolDay = HoliDate.getDate();
                
				if(CalYr==HolYr && CalMon==HolMon && CalDay==HolDay)
				{			 
				  BolOutput=true;
				  break;
				}
			 }
		}

	}//Inputdate!="" check ends 

	return BolOutput;
}

function removeCharacters( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Removes characters from a source string
  based upon matches of the supplied pattern.

PARAMETERS: 
  strValue - source string containing number.
  
RETURNS: String modified with characters
  matching search pattern removed
  
USAGE:  strNoSpaces = removeCharacters( ' sfdf  dfd', 
                                '\s*')
*************************************************/
 var objRegExp =  new RegExp( strMatchPattern, 'gi' );
 
 //replace passed pattern matches with blanks
  return strValue.replace(objRegExp,'');
}

function funCheckPhoneNumber(strCtrlName)
{   	
    var strValue = document.getElementById(strCtrlName).value;
	strValue=  removeCharacters(strValue,'[^0-9]');
    
    if(strValue=="" || isNaN(strValue) || strValue.length!=10)
    {
      return false;
    }
  
  document.getElementById(strCtrlName).value=strValue;
  return true;   
}
//Loan Application form scripts ends