// JavaScript Document
function validate_email(field,alerttxt)
{
with (field)
  {
	  	 var user_emails = document.getElementById('email_alert').value;
	 var user_email = document.getElementById('email1').value;
	 //alert(user_name);
 	var userarray = new Array();
	userarray = user_emails.split(",");
		//alert(userarray);
       //alert(userarray.length); 
	var part_num = 0;
	//alert(part_num);
		 for(i=0; i<userarray.length; i++)
		  {
				 if(userarray[i] == user_email)
				  {
					  document.getElementById('email1').value ='';
					 alert("Emailid Already Exist");
				  return false;
					}//if loop ends
		  }//for ends

  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {alert(alerttxt); 
	//document.getElementById('email1').value ='';
	 return false;}
  else  {return true;}
  }
}
function validate_pass(field,alerttxt)
{
   	var address = document.getElementById('name2').value;
   	var address1 = document.getElementById('name').value;
   	var email = document.getElementById('email1').value;
	var pass = document.getElementById('pass2').value;
	//alert(address+address1+email+pass);
	//alert(pass);
	var mail = new Array();
	mail=email.split(" ");
	//alert(mail.length);
	//alert()
	var password = new Array();
	password=pass.split(" ");
	//alert(password.length);
	var part_num=0;
with (field)
  {
	  var i =0
  if (value=="" || value=="Password:")
    {alert(alerttxt); document.getElementById('name').value =''; return false;}
	else if(part_num < mail.length) {
				for(i; i<=mail.length; i++) {
					//alert(mail[i]+password[i]);
				if((mail[i] != address) || (password[i] != address1)) {
				var error = 0;
				}else {
				return true;
				}
				}
				if(error == 0) {
				alert('Please Enter Valid Email and PassWord');
				return false;
				}
	}
  }
}

function validate(control) {
	 //alert('Value');
	 document.getElementById(control.id).value='';
}
function validate_form2(thisform)
{
with (thisform)
  {
	  	  //var businessname = document.getElementById('businessname').value;
		  
		  var streetno = document.getElementById('streetno').value;
		  var postcode = document.getElementById('postcode').value;
		  var landphone1 = document.getElementById('landphone1').value;
		  var landphone2 = document.getElementById('landphone2').value;
		  var mobileno = document.getElementById('txtmobilephone').value;
		  var fax = document.getElementById('fax').value;

	 //alert(streetno);
	  //var businessaddress = document.getElementById('businessaddress').value;
	  //alert(servicevalue);
	  
	/* if(validate_chooselisting(listing,"Choose Listing!")==false)
    {//alert("ok3");
	listing.focus();return false;}
  	
	 else */if(validate1(businessname,"Enter the Business Name!")==false)
    {//alert("ok3");
	businessname.focus();return false;}
	
	else if(validate1(user_name,"Enter the User Name!")==false)
    {//alert("ok3");
	user_name.focus();return false;}
	
	//else if(validateusername(user_name,"Username already exists!")==false)
    //{//alert("ok3");
	//user_name.focus();return false;}
	
	//else if(validate_username(user_name,"Username not valid!")==false)
    //{//alert("ok3");
	//user_name.focus();return false;}
	
	//else if(username_improper(user_name)==false)
	//{//alert("ok);
	//user_name.focus();return false;}
	
	//else if(username_improper1(user_name)==false)
	//{//alert("ok);
	//user_name.focus();return false;}


	 else  if(validate_pass2(password,"Enter the Password!")==false)
    {password.focus();return false;}
	
	 else if(validate_repass(confirmpassword,"Enter the Confirm  Password!")==false)
    {confirmpassword.focus();return false;}
	
	
	 //else if(validate1(businessowner,"Enter the Business Owner!")==false)
    //{businessowner.focus();return false;}
	
		//else if(validate1(level,"Enter the Level!")==false)
  	//{level.focus();return false;} 
	
	/*else if(validate_streetno(streetno,"Enter the Street No")==false)
	{streetno.focus();return false;}*/
	
	else if(validate1(streetname,"Enter the Street Name")==false)
	{streetname.focus();return false;}
	
	/*else if(validate1(streettype,"Enter the Street Type")==false)
	{streettype.focus();return false;}*/
	
	else if(validate1(suburb1,"Enter the Suburb")==false)
	{suburb1.focus();return false;}
	
	else if(validatestate(state,"Select the State")==false)
	{state.focus();return false;}

	else if(validate_postcode(postcode,"Enter the postal code")==false)
	{postcode.focus();return false;}
	
	else if(validate_phones(landphone1,"Enter the Land phone1","Enter the 10 digit Land phone1")==false)
	{landphone1.focus();return false;}
	
	/*else if(validate_phones(landphone2,"Enter the Land phone2","Enter the 10 digit Land phone2")==false)
	{landphone2.focus();return false;}*/
	
	else if(validate_phones(txtmobilephone,"Enter the Mobile Number","Enter the 10 digit Mobile Number")==false)
	{txtmobilephone.focus();return false;}
	
	/*else if(validate_phones(fax,"Enter the Fax Number","Enter the 10 digit Fax Number")==false)
	{fax.focus();return false;}*/

    else if(validate_email(email1,"Not a valid e-mail address!")==false)
    {email1.focus();return false;}
	
	/*else if(validate_email(email2,"Not a valid e-mail address!")==false)
    {email2.focus();return false;}*/
	
	else if(validatewebsite(website,"Enter the Website")==false)
	{website.focus();return false;}
	
	else if(validateCaptcha(recaptcha_response_field,"Your captcha is incorrect. Please try again")==false)
  	{recaptcha_response_field.focus();return false;}
	
	else if(document.register.check_terms.checked == false)
	{
	alert("Please accept the terms and conditions");
	return false;
	}
	

  }
  
}

function validate_streetno(field,alerttxt)
{
 //alert("Phone");
 //alert(field)
 var x = document.register.streetno.value;
        if(isNaN(x)|| x.indexOf(" ")!=-1){
              alert("Enter numeric value"); return false; }
        if (x == ''){
                alert("Enter Your  Street Number"); return false;
           }
}


function validate_postcode(field,alerttxt)
{
 //alert("Phone");
 var x = document.register.postcode.value;
        if(isNaN(x)|| x.indexOf(" ")!=-1){
              alert("Enter numeric value");return false; }
        if (x == ''){
                alert("Enter Your Postal Code"); return false;
           }
}

function validate_phones(field,alerttxt,alerttxt1)
{
	//alert("Values")
	with (field)
  {
 //alert(value);
	 //var x = value;
	 //var position = value.indexOf(" ");
	 //alert("position :"+position);
	 		if(value == ''){
				alert(alerttxt);return false; }
			if(isNaN(value)|| value.indexOf(" ")!=-1 ){
				  alert("Enter numeric value");return false; }
			if (value.length != 10){
					alert(alerttxt1); return false;
			   }
  }
}

function validate_chooselisting(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value==null||value=="Choose Listing")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}

function validate1(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value==null||value=="")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}
function validateusername(field,alerttxt)
{
	//alert("entered to function");

	with (field)
  {
	 //alert(Value);
	 var user = document.getElementById('user_alert').value;
	 var user_name = document.getElementById('user_name').value;
	 //alert(user_name);
 	var userarray = new Array();
	userarray = user.split(",");
		//alert(userarray);
       //alert(userarray.length); 
	var part_num = 0;
	//alert(part_num);
		 for(i=0; i<userarray.length; i++)
		  {
				 if(userarray[i] == user_name)
				  {
					  document.getElementById('user_name').value ='';
					 alert("Username Already Exist");
					//document.getElementById('error2').innerHTML = 'Username Already Exist';
				  //part_num+=1;
				  
				  return false;
		
					}//if loop ends
		  }//for ends

  }//with ends

}//function ends
function validatecategory(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value==""||value=="Start typing to see suggested categories")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}

function validatewebsite(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value==""||value=="E.g. http://www.yoursite.com.au")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}
function validatelandphone(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value==""||value=="E.g. 02 8114 6111")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}
function validatefax(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value==""||value=="E.g. 02 8114 6208")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}
function validatestate(field,alerttxt)
{
	//alert("zip1");

	with (field)
  {
	 //alert(Value);
  if (value=="Select one")
    {//alert("ok3");
	alert(alerttxt);
	return false;}
  else
    {return true;}
  }

}
function validate_form(thisform)
{
	//alert('Value');
with (thisform)
  {
  if(validate_email1(email3,"Not a Valid Email Address!")==false)
  {email3.focus();return false;}
  
  
  }
}
function validate_email4(field,alerttxt)
{
with (field)
  {
	  var emai = document.getElementById('email').value;
	  alert('Email'+emai);
	var email = emai;
	var mail = new Array();
	mail=email.split(",");
	var part_num=0;
	var i=0;
	//alert(value);
	//alert(mail.length);
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {alert(alerttxt);return false;}
 	 else if(part_num < mail.length) {
			for(i; i<mail.length; i++) {
				//alert (mail[i]);
			if((mail[i] == value)) {
			alert('Email address aleardy Exist');
			return false;
			}else
			 {
				//alert('False');
			var error =1;
			}
			}
			//alert(error);
			if(error == 1) {
			return true;
		}
	}
  }
}


function validate_repass(field,alerttxt)
{
with (field)
  {
  var email = document.getElementById('password').value;
  //alert(email);
  if(value=="")
    {alert(alerttxt);return false;}
  else if(value != email)
  	{alert('Both Password Do Not Match');return false;}
  else  {return true;}
  }
}

function validate_randomvalue(field,alerttxt)
{
with (field)
  {
  var randomvalue = document.getElementById('randomvalue').value;
  //alert(email);
  if(value=="")
    {alert(alerttxt);return false;}
  else if(value != randomvalue)
  	{alert('Code Do Not Match');return false;}
  else  {return true;}
  }
}
function validate_pass2(field,alerttxt)
{
with (field)
  {
  if (value=="")
    {alert(alerttxt);return false;}
  else if(value.length <= 5)
  	{alert('Password length greater then 6 character');return false;}
	else
    {return true;}
  }
}

/*function username_improper(field)
{
with (field)
 {
		alert(value);

	var iChars = "*|,\":<>[]{}`\';()@&$#%.{0,1,2,3,4,5,6,7,8,9}";
	
	var i;
	for (i = 0; i < value.length; i++) {
		alert("ok");

	   		   if (iChars.indexOf(value.charAt(i)) != -1){
				   alert("ok1");

				  alert ("Username contains invalid characters");

				  return false;

  			   }//if loop ends
			   else
			   {return true;}

	}//for loop ends
 }//with ends
}//function ends
*/

function username_improper(username) {
	var user_name = document.getElementById('user_name').value;
	//alert("username "+user_name);
    var error = "";
 
    if ((user_name.length < 5) || (user_name.length > 15)) {
        error = "The username is the wrong length.\n";
		alert(error);
		return false;
    }
	else{
    return true;}
}
function username_improper1(username)
{
	var user_name = document.getElementById('user_name').value;
	//alert("username "+user_name);
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
	if (illegalChars.test(user_name)) {
        error = "The username contains illegal characters.\n";
		alert(error);
		return false;
    } 
	else{
    return true;}
	
}

//NORMAL VALIDATION
function validate_zip5(field,alerttxt)
{
	//alert("ok2");
	//alert(field);
	//alert(alerttxt);
with (field)
  {
  if (value==null||value=="")
    {alert(alerttxt);return false;}
  else
    {return true;}
  }
}

function validate_form4(thisform)
{
	//alert('Value');
with (thisform)
  {
  if(validate_email5(email,"Not a Valid Email Address!")==false)
  {email.focus();return false;}
  
  
  }
}
function validate_email5(field,alerttxt)
{
with (field)
  {
	var emai = document.getElementById('email1').value;
	//alert(emai);
	var mail = new Array();
	mail=emai.split(" ");
	var part_num=0;
	var i=0;
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	if (apos<1||dotpos-apos<2)
	{alert(alerttxt); return false;}
	else if(part_num < mail.length) {
				for(i; i<=mail.length; i++) {
					//alert(mail[i]);
				if((mail[i] != value)) {
				var error = 0;
				}else {
				return true;
				}
				}
				if(error == 0) {
				alert('Please Enter Valid Email');
				return false;
				}
	}
  }
}


function validate_testimonial(thisform)
{
with (thisform)
  {
	 if(validate1(testimonialheadline,"Enter the Testimonial Heading!")==false)
    {//alert("ok3");
	testimonialheadline.focus();return false;}
	
	else if(validate1(testimonialdescription,"Enter the Description!")==false)
    {//alert("ok3");
	testimonialdescription.focus();return false;}
	

  }
  
}
 function validate_newsupdate(thisform)
 {
	 //alert("form");
	with (thisform)
  {
	 if(validate1(newsheadline,"Enter the Tilte!")==false)
    {//alert("ok3");
	newsheadline.focus();return false;}
	
	else if(validate1(newsdescription,"Enter the Striking news update!")==false)
    {//alert("ok3");
	newsdescription.focus();return false;}
	

  }
 
 }
 
 function validate_coupons(thisform)
 {
			with (thisform)
  {
	 if(validate1(couponheadline,"Enter the Voucher Slogan!")==false)
    {//alert("ok3");
	couponheadline.focus();return false;}
	
	else if(validate1(offerdetails,"Enter the Description!")==false)
    {//alert("ok3");
	offerdetails.focus();return false;}
	
	 else if(validate1(restrictions,"Enter the Terms & conditions!")==false)
    {restrictions.focus();return false;}
	else if(validate1(expirydate,"Enter the Expiry date!")==false)
    {expirydate.focus();return false;}
	
		else if(validate1(promotionalcode,"Enter the Redeem Code!")==false)
    {promotionalcode.focus();return false;}

  }
 
 }
/* function validate_addproduct(thisform)
 {
			with (thisform)
  {
	 if(validate1(pdtorsername,"Enter the Product or Service name!")==false)
    {//alert("ok3");
	pdtorsername.focus();return false;}
	
	
	else if(validate1(productdescription,"Enter the Description!")==false)
    {//alert("ok3");
	productdescription.focus();return false;}
	

  }
 
 }
*/

// function checkfiles(thisform)
//{
//	alert("ok");
//	with (thisform)
//  {
//alert("ok1");
//var fup = document.getElementById("imagegal").value;
//alert("filename "+fup);
//var fileName = fup.value;
//var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
//if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "png" || ext == "PNG" )
//{
//return true;
//} 
//else
//{
//alert("Upload Gif,Jpg,Png images only");
//fup.focus();
//return false;
//}
//  }//with ends
//}
  /* Recaptcha Validate */
function validateCaptcha(field,alerttxt)
{
	//alert("Testing");
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
    type: "POST",
    url: "ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;
    //alert(html);
    if(html == "success")
    {
        /*$("#captchaStatus").html("Success. Submitting form.");*/
        return true;
        // Uncomment the following line in your application
        //return true;
    }
    else
    {
        alert(alerttxt);
        //Recaptcha.reload();
        return false;
    }
}

 /* Recaptcha Validate End */
 


 
 /*Rechange End */
 
 function checkNumber(val) {
	 //alert("Value");
  var strPass = val.value;
  //alert(strPass);
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}
