// JavaScript Document


//---------------------------------------------------------------------------------------------------
// Trims the input string of leading and trailing spaces and returns the new string

function fnTrim(string)
{
	var cnt;	
	len = string.length;
	str = string;
	begin = -1;
	for(cnt=0;cnt<len;cnt++)
	{
		if (str.charAt(cnt) == " ")
		{	
			begin = cnt;
		}	
		else
		break;
		
	}
	str = str.slice(begin+1,len);
	len = str.length;
	end = len;
	for(cnt=len-1;cnt>=0;cnt--)
	{
		if (str.charAt(cnt) == " ")
		{	
			end = cnt;
		}	
		else
		break;
	}
	str = str.slice(0,end);
	return str;
}

//-------------------------------------
function dropDownMenu(menu, mes) 
   {
   var myindex=menu.selectedIndex;
   if (myindex==0)
      {
      alert(mes);
      menu.focus();
	  return true;
	  }
	else 
		{
		return false;
		}
   }


//---------------------------------------------------------------------------------------------------

// Checks if the input string is null or blanks

function fnIsNull(txtfld, mes)
{
	   var string=txtfld.value;
       if (fnTrim(string) == null || fnTrim(string) == "" )
       {
	   	alert(mes);
		txtfld.focus();
		txtfld.select();
     	return true;
       }
		return false;
}

//To check single and double quotes.

function fnIsQuotes(txtfld, mes){
	var a_strString = fnTrim(txtfld.value)
	if(a_strString.indexOf('"') != -1)
	  {
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	if(a_strString.indexOf("'") != -1)
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	
	return false
}


function emailcheck(txtfld, mes)
{
var str;
var string1=txtfld.value;
 str = "";
if (string1.indexOf("@")==-1){
	str = "NO";}
else if (string1.indexOf("@")==0){
	str = "NO";}
else if (string1.indexOf(".")==-1){
	str = "NO";}
else if (string1.indexOf(".")==0){
	str = "NO";}
else if (string1.indexOf("@") + 1 == string1.indexOf(".")){
	str = "NO";}
else if (string1.indexOf("@") + 1== string1.length){
	str = "NO";}
else if(string1.lastIndexOf(".") + 1== string1.length){
	str = "NO";}
else if(string1.indexOf("@") != string1.lastIndexOf("@")){
	str = "NO";}

else{
	str = "YES";}

if (str=="NO"){	
	alert(mes);
	txtfld.select();
	txtfld.focus();
	return true;
	}
	else{
		return false;}
}



//---------------------------------------------------------------------------------------------------
				// Main validation function
				function validate(){
				frm=document.debtadvice;
				if(dropDownMenu(frm.debtlevel,"Please choose Estimated Debt Level"))
				return false;
				else if(dropDownMenu(frm.oweto,"How many companies do you owe"))
				return false;
				else if(dropDownMenu(frm.disposableincome,"Choose your Disposable income"))
				return false;
				else if(dropDownMenu(frm.homeowner,"Are you a homeowner?"))
				return false;
				if(frm.homeowner)
				{
				if(frm.homeowner.value == "Yes")
				{
				if(fnIsNull(frm.homeworth,"What is your home worth ?"))
				return false;
				if(fnIsNull(frm.buyingprice,"How much did you buy your home for?"))
				return false;
				}
				}
				if(fnIsNull(frm.fname,"Enter your first name"))
				return false;
				else if(fnIsNull(frm.lname,"Enter your last name"))
				return false;
				else if(fnIsNull(frm.address,"Enter your address"))
				return false;
				else if(fnIsNull(frm.postprefix,"Please enter your postcode") || fnIsNull(frm.postsuffix,"Please enter your postcode"))
				return false;
				
				else if(fnTrim(frm.homephoneno.value) == '' && fnTrim(frm.mobilephoneno.value)=='')
				{
				alert("Please enter either Home/Mobile phone number to get in touch with you");
				frm.homephoneno.focus();
				return false;
				}
				
				if ( frm.homephoneno.value!="")
				{
				if ( checkPhone(frm.homephoneno," Enter Valid Home Number"))
				return false;
				}
				if (frm.mobilephoneno.value!="")
				{
				if ( checkPhone(frm.mobilephoneno," Enter Valid Mobile Number"))
				return false;
				}
				
				else if(fnIsNull(frm.email,"Please provide us a valid Email"))
				return false;	
				else if(fnIsQuotes(frm.email,"Please provide us a valid Email"))
				return false;				
				else if(emailcheck(frm.email,"Please provide us a valid Email"))
				return false;
				
				if(dropDownMenu(frm.jointdebt,"Are your Debts in Joint Name?"))
				return false;
				if(dropDownMenu(frm.incometype,"Type of Income"))
				return false;
				if(dropDownMenu(frm.debtawareness,"Is your Partner Aware of Debts?"))
				return false;
				if(dropDownMenu(frm.debtmanagementcomp,"Are you Currently with Debt Management Company "))
				return false;
				
				}
				SetCookie=function(cookieName,cookieValue) {
				var today = new Date();
				var expire = new Date();
				expire.setTime(today.getTime() + 300000);
				document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();

}

ReadCookie=function(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function getParam(url,param)
{
 var i;
  var val;
  if ((i = url.indexOf("?" + param + "=")) > -1 ||(i = url.indexOf("?" + param.toUpperCase() + "=")) > -1 ||(i = url.indexOf("&" + param +"=")) > -1 ||(i = url.indexOf("&" + param.toUpperCase() + "=")) > -1) 
  {
    val = url.substring(i + param.length + 2, url.length);
    if ((i = val.indexOf("&")) > -1) {  val = val.substring(0, i);  }
  }
  if(val)
{
return val;
}
}

function setParams(url)
{
	SetCookie("se",getParam(url,"se"));
	SetCookie("cat",getParam(url,"cat"));
	SetCookie("kw",getParam(url,"kw"));
}

function hidden()
{
	document.writeln("<input type='hidden' name='se' value="+ReadCookie("se")+">");
	document.writeln("<input type='hidden' name='cat' value="+ReadCookie("cat")+">");
	document.writeln("<input type='hidden' name='kw' value="+ReadCookie("kw")+">");
}

function getkey(e){
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}
	function goodchars(e, goods){
	//alert(e.value);
var key, keychar;
key = getkey(e);
if (key == null) return true;
keychar = String.fromCharCode(key);
if (goods.indexOf(keychar) != -1)
	return true;
if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	return true;
return false;
}

function checkPhone(fld,mes)
{
	var x = fld.value;
	var filter  =/^\d{10,11}$/;
	if (!filter.test(x)) 
	{
	alert(mes);
	fld.select();
	}
	else
	return false;
	return true;
	
}

function chech_ho()
	{
		if(document.debtadvice.homeowner.value=="Yes")
			{
				document.getElementById("ho_extra").style.display="block";
				}
		else
			{
				document.getElementById("ho_extra").style.display="none";
				}
		}
		
		
		
	function contactValidate(){
				frm=document.contact;				
				if(fnIsNull(frm.fname,"Enter your first name"))
				return false;
				else if(fnIsNull(frm.lname,"Enter your last name"))
				return false;				
				else if(fnIsNull(frm.homephoneno,"Please enter either Home phone number"))
				return false;				
				if ( frm.homephoneno.value!="")
				{
				if ( checkPhone(frm.homephoneno," Enter Valid Home Number"))
				return false;
				}
				if (frm.mobilephoneno.value!="")
				{
				if ( checkPhone(frm.mobilephoneno," Enter Valid Mobile Number"))
				return false;
				}
				
				if(fnIsNull(frm.email,"Please provide us a valid Email"))
				return false;	
				else if(fnIsQuotes(frm.email,"Please provide us a valid Email"))
				return false;				
				else if(emailcheck(frm.email,"Please provide us a valid Email"))
				return false;		
				else if (fnchekbox(frm.privacy, "Please Check the Privacy Policy "))
				 return false;
				
				}
				
		
function fnchekbox(chekbox, mes)
 {
     frm=document.contact.privacy;
   checked =frm.checked;
      if (!checked)
    {  
	alert(mes);
    return true;
    }
 }