function is_number(TestObject){
//alert(TestObject);
	if(isNaN(parseFloat(TestObject.value))){
	return false;
	}
	
return true;
}

function is_email(TestObject){
var RegStr = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/
	if(RegStr.test(TestObject.value)){
		return true;
	}
return false;
}

function is_not_empty(TestObject){
	if(TestObject.value == ''){
	return false;
	}
return true;
	
}

function is_checked(TestObject){
	if(TestObject.checked == false){
	return false;
	}
return true;
	
}


var	alertstring  ='';

function validate_step1(){

var	alertstring  ='';

obj1 = document.getElementById("Total_Unsecured_Debt");
	if(obj1.options[obj1.selectedIndex].value == ''){
	alertstring = alertstring + obj1.title + " : Please select a total unsecured credit amount\n";
	}

obj2 = document.getElementById("Num_of_Creditors");
	if(!is_number(obj2)){
	alertstring = alertstring + obj2.title + " : Please type a valid number amount\n";
	}

obj3 = document.getElementById("Monthly_Income");
	if(!is_number(obj3)){
	alertstring = alertstring + obj3.title + " : Please type a valid money or number amount\n";
	}
obj4 = document.getElementById("incometype");
	if(obj4.value ==''){
	alertstring = alertstring + obj4.title + " : Please type a valid Income Type\n";
	}

obj5 = document.getElementById("Homeownerbox");
	
	if(is_checked(obj5)){ 
			
obj6 = document.getElementById("homevalue");
		if(!is_number(obj6)){
		alertstring = alertstring + obj6.title + " : Please type a valid money or number amount\n";
		}	
obj7 = document.getElementById("outstandingmortgage");
		if(!is_number(obj7)){
		alertstring = alertstring + obj7.title + " : Please type a valid money or number amount\n";
		}
	
	}
	
	/*obj6 = document.getElementById("Data_Protection");
	
	if(!is_checked(obj6)){
		alertstring = alertstring + obj6.title + " : You must accept the Data Protection Policy\n";	
		
	
	}*/
	
	
	if(alertstring != ""){
		alert(alertstring);
		return false;
	}
	else
	{
		
		document.getElementById("step1").style.visibility='hidden';
		document.getElementById("step1").style.display='none';
		document.getElementById("step2").style.visibility='visible';
		document.getElementById("step2").style.display='block';
		var url="datasave.php";
		url =  url + "?Total_Unsecured_Debt="+ document.getElementById("Total_Unsecured_Debt").value;
 		url+="&Num_of_Creditors="+ document.getElementById("Num_of_Creditors").value;		
		url+="&Monthly_Income="+ document.getElementById("Monthly_Income").value;
		url+="&incometype="+ document.getElementById("incometype").value;
		url+="&Are_You_A_Homeowner="+ document.getElementById("Homeownerbox").value;		
		url+="&Current_Value_of_Home="+ document.getElementById("homevalue").value;		
		url+="&Mortgage_Outstanding="+ document.getElementById("outstandingmortgage").value;		
	//	alert(url);
		ajax(url);	 
	
	}
}

function validate_step2(){
	var	alertstring  ='';

obj1 = document.getElementById("First_Name");
	if(!is_not_empty(obj1)){
	alertstring = alertstring + obj1.title + " : Please type your First name\n";
	}

obj11 = document.getElementById("Surname");
	if(!is_not_empty(obj11)){
	alertstring = alertstring + obj11.title + " : Please type your Surname\n";
	}
	
obj2 = document.getElementById("Email_Address");
	if(!is_email(obj2)){
	alertstring = alertstring + obj2.title + " : Please type a valid email address\n";
	}

obj3 = document.getElementById("Home_Number");
thephone = obj3.value;
if(thephone.match(/[^0-9]/) || thephone.length < 11){ //find non integers, number must be minimum 11 digits
	alertstring = alertstring + obj3.title + " : Please type your phone number with 11 digits\n";
	}		
obj6 = document.getElementById("monthly_bill");

if(!is_number(obj6)){
	alertstring = alertstring + obj6.title + " : Please type your monthly bill, only numerics\n";
	}
	
	if(!is_number(obj3)){
	alertstring = alertstring + obj3.title + " : Please type your phone number, only numerics\n";
	}
	
obj4 = document.getElementById("Property Number");
	if(!is_not_empty(obj4)){
	alertstring = alertstring + obj4.title + " : Please type your house number\n";
	}
	
obj5 = document.getElementById("Postcode");
	if(!is_not_empty(obj5)){
	alertstring = alertstring + obj5.title + " : Please type your post code\n";
	}
	
if(alertstring != ""){
		alert(alertstring);
		return false;
	}
/*else
	{
		
		document.getElementById("step2").style.visibility='hidden';
		document.getElementById("step2").style.display='none';
		document.getElementById("step3").style.visibility='visible';
		document.getElementById("step3").style.display='block';
		var url="datasave.php";
		url =  url + "?First_Name="+ document.getElementById("First_Name").value;
 		url+="&Surname="+ document.getElementById("Surname").value;		
		url+="&Email_Address="+ document.getElementById("Email_Address").value;
		url+="&Home_Number="+ document.getElementById("Home_Number").value;		
		url+="&Mobile_Number="+ document.getElementById("Mobile_Number").value;		
		url+="&Best_Contact_time="+ document.getElementById("Best_Contact_time").value;	
		
		url+="&monthlybill="+ document.getElementById("monthlybill").value;	
		url+="&HouseNumber="+ document.getElementById("Property Number").value;	
		url+="&Postcode="+ document.getElementById("Postcode").value;	
		url+="&uid="+ document.getElementById("uid").value;	
			
		//alert(url);
		ajax(url);	 
		return false;	
				
		
	}*/

if(qualifyamount < 10000){
remindermessage = "Your total unsecured debt is less than &pound;10 000. To qualify for an IVA your unsecured debt needs to be more than &pound;10 000\n\n\n- Is there any one else you owe money to?\n- Is this all your unsecured debt: credit cards, store card, catalogues, personal loans, overdrafts?\n- Do you have any unpaid household bills, utilities & council tax?\n- Do you have any arrears?\n- And what about your partner?\n\nClick Cancel to alter the debt amount or Ok to proceed.";

	if(!confirm(remindermessage)){
			history.back();
		return false;
	}
}
	showaddresslookup();
	return false;
}
//-------------------------CONVERT TO CURRENCY--------------------------------
function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

function moneyFormat(textfieldo){
	tmpmoneyval = replaceCharacters(textfieldo.value);
	//tmpmoneyval = replaceCharacters(tmpmoneyval,'.','');
	//alert(tmpmoneyval);
	
	//textfieldo.value = FormatNumberBy3(tmpmoneyval);
	/*if(isNaN(textfieldo.value)){
			textfieldo.value = "0.00";
			return false;
 }*/
}

function replaceCharacters(conversionString){
//return conversionString;
var regEx = new RegExp (',', 'gi') ;
  return conversionString.replace(regEx,'');

}
//-------------------------CONVERT TO CURRENCY--------------------------------





function checkform()
{
	var validchars='0123456789';
	
    var totalowed=document.getElementById("totalowed").value;
	if(totalowed == '')
	{
		alert("Please Select Approximately, how much money do you owe?");
		document.getElementById("totalowed").focus();
		return false;
	}
	else
	{
	  for(var k=0;k<totalowed.length;k++)
        {
	       var ch=totalowed.charAt(k);
	       if(validchars.indexOf(ch)==-1) 
	       {
	         alert('Total Owed should contain only Digits. No other Character is allowed.');
			 document.getElementById("totalowed").focus();
	         return false;	 
	       }
        }
	
	}

    var totowed=parseInt(totalowed);
	if(totowed < 2000)
	{
		alert('Total Owed must be greater or equal 2000.');
		document.getElementById("totalowed").focus();
	    return false;	 
	}
   
    var debtor_count=document.getElementById("debtor_count").value;
	if(debtor_count == '')
	{
		alert("Please select How many companies do you owe money to?");
		document.getElementById("debtor_count").focus();
		return false;
	}
		
	var income=document.getElementById("income").value;
	
 	if(income == '')
	{
		alert("Please Enter What is your monthly income?");
		document.getElementById("income").focus();
		return false;
	}
	else
	{
		for(var k=0;k<income.length;k++)
        {
	       var ch=income.charAt(k);
	       if(validchars.indexOf(ch)==-1) 
	       {
	         alert('Monthly Income should contain only Digits. No other Character is allowed.');
			 document.getElementById("income").focus();
	         return false;	 
	       }
        }		
	}
	
	var totincome=parseInt(income);
	if(totincome < 200)
	{
		alert('Monthly Income must be greater or equal 200.');
		document.getElementById("income").focus();
	    return false;	 
	}
	
	var outgoings=document.getElementById("outgoings").value;
	
 	if(outgoings == '')
	{
		alert("Please Enter Approximately, what do you spend on food and household bills each month?");
		document.getElementById("outgoings").focus();
		return false;
	}
	else
	{
		for(var k=0;k<outgoings.length;k++)
        {
	       var ch=outgoings.charAt(k);
	       if(validchars.indexOf(ch)==-1) 
	       {
	         alert('Total Expenses should contain only Digits. No other Character is allowed.');
			 document.getElementById("outgoings").focus();
	         return false;	 
	       }
        }			
	}
	
	
	if (document.getElementById("intype").value=='')
	{
		alert("Please Enter What is your income type");
		document.getElementById("intype").focus();
		return false;
	}
		
	if (document.getElementById("fname").value=="")
	{
		alert("Please Enter Your First Name.");
		document.getElementById("fname").focus();
		return false;
	}
	
	if (document.getElementById("lname").value=="")
	{
		alert("Please Enter Your Last Name.");
		document.getElementById("lname").focus();
		return false;
	}	
	
	if (document.getElementById("housenameno").value=="")
	{
		alert("Please Enter Your House Number/Name.");
		document.getElementById("housenameno").focus();
		return false;
	}
	
	if (document.getElementById("town").value=="")
	{
		alert("Please Enter Your Town.");
		document.getElementById("town").focus();
		return false;
	}
	
	if (document.getElementById("country").value=="")
	{
		alert("Please Enter Your Country.");
		document.getElementById("country").focus();
		return false;
	}
	
	if (document.getElementById("postcode").value=="")
	{
	    alert("Please Enter Postal Code");
	    document.getElementById("postcode").focus();
	    return false;
	}
	
	var email=document.getElementById("email").value;
	if (email == "")
	{
	  alert("Please Enter Email Address");
	  document.getElementById("email").focus();
	  return false;
	}
	else
	{
	   if(!validateemail(email))
	   {
		  //alert("Please Enter Email Address");
	      document.getElementById("email").focus();
	      return false;
	   }
	}
	
    var hphone=document.getElementById("hphone").value;
	if (hphone == "")
	{
		alert("Please Enter Home Telephone Number");
		document.getElementById("hphone").focus();
		return false;
	}		
	else
	{
		  if(hphone.length != '10' && hphone.length != '11')
		  {
			 alert('Telephone numbers should contain 10 or 11 digits,');  
			 document.getElementById("hphone").focus();
			 return false;
		  }
		  if(hphone.charAt(0) != '0')
		  {
			 alert('Telephone number should start with 0');  
			 document.getElementById("hphone").focus();
			 return false;
		  }
		  //var validchars='0123456789';		  
		  for(var k=0;k<hphone.length;k++)
		  {
			 var ch=hphone.charAt(k);
			 if(validchars.indexOf(ch)==-1) 
			 {
			   alert('Telephone number should contain only Digits. No other Character is allowed. ');
			   document.getElementById("hphone").focus();
			   return false;	 
			 }
		  }
	}
	
	/*else if (document.getElementById("a_phone").value=="")
	{
		alert("Please Enter Alternative Contact Number");
		document.getElementById("a_phone").focus();
		return false;
	} */
	var mobileno=document.getElementById("mobileno").value;
	if (mobileno == "")
	{
		alert("Please Enter Home Mobile Number");
		document.getElementById("mobileno").focus();
		return false;
	}		
	else
	{
		  if(mobileno.length != '10' && mobileno.length != '11')
		  {
			 alert('Mobile number should contain 10 or 11 digits,');  
			 document.getElementById("mobileno").focus();
			 return false;
		  }
		  if(mobileno.charAt(0) != '0')
		  {
			 alert('Mobile number should start with 0');  
			 document.getElementById("mobileno").focus();
			 return false;
		  }
		  //var validchars='0123456789';		  
		  for(var k=0;k<mobileno.length;k++)
		  {
			 var ch=mobileno.charAt(k);
			 if(validchars.indexOf(ch)==-1) 
			 {
			   alert('Mobile number should contain only Digits. No other Character is allowed. ');
			   document.getElementById("mobileno").focus();
			   return false;	 
			 }
		  }
	}

	/*else if(!isNumber(document.getElementById("a_phone").value))
	{
		alert("Please Enter Numeric Value only");
		document.getElementById("a_phone").focus();
		return false;
		
	} */
	
	if (document.getElementById("contactTime").value=="")
	{
	   alert("Please Enter Contact Time");
	   document.getElementById("contactTime").focus();
	   return false;
	}
	
	if(document.getElementById("resitype").value=="")
	{
	   alert("Please Select Type of Residence.");
	   document.getElementById("resitype").focus();
	   return false;
	}
	
	/*else
	{
		return true;
	}*/

}

function isNumber(val)
{

   if (val.match(/^[0-9.\s]+$/))
   {
      return true;
   }
   else
   {
      return false;
   }   
}


function validateemail(email)
{
	var strError;
	//email=o.value;
			   if(!checkmail(email)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError ="Enter valid Email address "; 
                 }//if                                               
                 alert(strError); 
                 return false; 
               }
			   return true;
}



function checkmail(email)
{
// a very simple email validation checking. 
/* you can add more complex email checking if it helps */
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false; 
}


function isValidEmail (emailIn){
	var isEmailOk = false;
//	var filter = /^[a-zA-Z0-9][a-zA-Z0-9._-]*\@[a-zA-Z0-9-]+(\.[a-zA-Z][a-zA-Z-]+)+$/
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;


	if(emailIn.search(filter) != -1)
		isEmailOk = true;
	if(emailIn.indexOf("..") != -1)
		isEmailOk = false;
	if(emailIn.indexOf(".@") != -1)
		isEmailOk = false;
	if(emailIn.indexOf("'") != -1 || emailIn.indexOf("''") != -1 || emailIn.indexOf("\"") != -1 || emailIn.indexOf("\"\"") != -1 || emailIn.indexOf("(") != -1  || emailIn.indexOf(")") != -1 || emailIn.indexOf("[") != -1 || emailIn.indexOf("]") != -1 || emailIn.indexOf("{") != -1  || emailIn.indexOf("}") != -1 || emailIn.indexOf(",") != -1 || emailIn.indexOf(",,") != -1 || emailIn.indexOf(":") != -1 || emailIn.indexOf(";") != -1 || emailIn.indexOf("#") != -1 || emailIn.indexOf("mailto:") != -1)
		isEmailOk = false;

	return isEmailOk;
} // Ends 