var strError;
var ajaxReturn;

//Signup Validation
function signup_validation(){
  var objUSer = getObject('email');
  strError = "";

  if ( objUSer.value.length == 0  ) {
    strError = strError + "Please enter valid email."
    objUSer.focus();

  }else if ( ! isEmail( objUSer.value ) ) {
    strError = strError + " Please enter valid email."
    objUSer.focus();
  }

  var objPass = getObject('password');

  if ( objPass.value.length <= 2  ) {
    strError = strError + "\n Password should not be less then 3."
    objPass.focus();
  }


  var objRePass = getObject('repassword');

  if ( objRePass.value.length <= 2  ) {
    strError = strError + "\n Retype Password should not be less then 3."
    objRePass.focus();
  }

  if ( objRePass.value != objPass.value ){

    strError = strError + "\n Password and Retype Password should be match."
    objPass.focus();

  }

  var obj = $('phone');

  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter valid phone number."
    obj.focus();

  }/*else if ( ! isPhone( obj.value ) ){

    strError = strError + "\n Please enter valid phone number."
    obj.focus();

  }*/

  var obj = $('fax');

  /*if ( obj.value.length > 0  ) {

    if ( ! isPhone( obj.value ) ){

      strError = strError + "\n Please enter valid fax number."
      obj.focus();

    }
  
  }*/
  
  var obj = $('firstName');

  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter valid first name."
    obj.focus();

  }else if ( ! isAlpha( obj.value ) ){

    strError = strError + "\n Please enter valid first name."
    obj.focus();

  }
  
  var obj = $('lastName');

  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter valid last name."
    obj.focus();

  }else if ( ! isAlpha( obj.value ) ){

    strError = strError + "\n Please enter valid last name."
    obj.focus();

  }
  

  if ( ! validateSelect( $('gender') ) ){

    strError = strError + "\n Please select gender."

  }

  /*if ( ! validateSelect( $('profession') ) ){

    strError = strError + "\n Please select profession."

  }*/


  if ( ! validateSelect( $('state') ) ){

    strError = strError + "\n Please enter state."

  }
  
  var obj = $('city');  
  if ( ! isAlpha( obj.value ) ){

    strError = strError + "\n Please enter valid city."
    obj.focus();

  }
  
  var obj = $('zip');

  if ( obj.value.length > 0  ) {

    if ( ! isZip( obj.value ) ){

      strError = strError + "\n Please enter valid pin."
      obj.focus();

    }
  
  }

  if ( strError.length ){

    alert( strError );
    return false;
  }
   
  checkDuplicateEmail( objUSer.value );
  
  return false;
}

function enableSubmit(){
	if(document.signup.agreeterms.checked)
		document.signup.Submit.disabled = false;	
	else
		document.signup.Submit.disabled = true;		
}

// Duplicate Email Ajax Call
function checkDuplicateEmail( email ){

  var url = 'index.php?page=signup';
  var pars = 'email='+ email;

  var myAjax = new Ajax.Request(
    url, 
	{
	  method: 'get', 
	  parameters: pars, 
	  onComplete: showResponse
   });
		
}

// Call back function
function showResponse(originalRequest) {
   ajaxReturn = originalRequest.responseText;
   if ( ajaxReturn == 'true') {
    strError = "Email id is already exsits. Please try again."
    alert( strError );
  //  objUSer.focus();
    return false;
  }else{
  	 var obj = $('ajax_submit');  
     obj.value =1;
  	 document.signup.submit(); 
    //return true;
  }
   
}

// login page validation
function forgot_validation(){

  var obj = $('email');;
  strError = "";

  if ( obj.value.length == 0  ) {
    strError = strError + "Please enter valid email."
    obj.focus();

  }else if ( ! isEmail( obj.value ) ) {
    strError = strError + "Please enter valid email."
    obj.focus();
  }

 if ( strError.length ){
    alert( strError );
    return false;
  }
  return true;
}

	
// login page validation
function login_validation(){

  var obj = $('email');;
  strError = "";

  if ( obj.value.length == 0  ) {
    strError = strError + "Please enter valid email."
    obj.focus();

  }else if ( ! isEmail( obj.value ) ) {
    strError = strError + "Please enter valid email."
    obj.focus();
  }

   var obj = $('pass');;

  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter passowrd."
    obj.focus();
  }
  
  if ( strError.length ){
    alert( strError );
    return false;
  }
  return true;
}
// Package Detail Vvalidation
function editPurchaseDetail_validation(id){
  strError = "";
  $('gfile').value="";
   var obj = $('advName');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter name."
    obj.focus();
  }

  var obj = $('advDesc');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter description."
    obj.focus();
  }
    
  var obj = $('advAdd1');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter address."
    obj.focus();
  }

  var obj = $('advCity');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter city."
    obj.focus();
  }

  var obj = $('advZip');
  if ( ! isZip( obj.value ) ){

    strError = strError + "\n Please enter valid zip code."
    obj.focus();
  }
  
  var obj = $('advState');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter state."
    obj.focus();
  }

  var obj = $('advPhone');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter phone."
    obj.focus();
  }/*else if ( ! isPhone( obj.value ) ){

    strError = strError + "\n Please enter valid phone number."
    obj.focus();
  }*/

  var obj = $('advMobile');
  /*if ( ! isPhone( obj.value ) ){

    strError = strError + "\n Please enter valid mobile number."
    obj.focus();
  } */ 

  var obj = $('advEmail');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter email."
    obj.focus();
  }else if ( ! isEmail( obj.value ) ){

    strError = strError + "\n Please enter valid email."
    obj.focus();
  }

  var objCheck = $('advVoucher').checked;

  if ( objCheck ) {

    var obj = $('advVoucherCompany');

    if ( obj.value.length == 0  ) {
      strError = strError + "\n Please enter company."
      obj.focus();
    }

    var obj = $('advVoucherTitle');  

    if ( obj.value.length == 0  ) {
      strError = strError + "\n Please enter title."
      obj.focus();
    }  

  }
  if ( strError.length ){
    alert( strError );
    return false;
  }
  $('submitInfo').value = 'submit';
  document.purchase.action="index.php?page=editPurchaseDetails&editid="+id;
  document.purchase.submit();
  return false;
}


// Package Detail Vvalidation
function packageDetail_validation(){
  strError = "";
   var obj = $('advName');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter name."
    obj.focus();
  }

  var obj = $('advDesc');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter description."
    obj.focus();
  }
    
  var obj = $('advAdd1');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter address."
    obj.focus();
  }

  var obj = $('advCity');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter city."
    obj.focus();
  }
  var obj = $('advZip');
  if ( ! isZip( obj.value ) ){

    strError = strError + "\n Please enter valid zip code."
    obj.focus();
  }
  var obj = $('advState');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter state."
    obj.focus();
  }

  var obj = $('advPhone');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter phone."
    obj.focus();
  }/*else if ( ! isPhone( obj.value ) ){

    strError = strError + "\n Please enter valid phone number."
    obj.focus();
  }*/

  var obj = $('advMobile');
  /*if ( ! isPhone( obj.value ) ){

    strError = strError + "\n Please enter valid mobile number."
    obj.focus();
  } */ 

  var obj = $('advEmail');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter email."
    obj.focus();
  }else if ( ! isEmail( obj.value ) ){

    strError = strError + "\n Please enter valid email."
    obj.focus();
  }

  var objCheck = $('advVoucher').checked;

  if ( objCheck ) {

    var obj = $('advVoucherCompany');

    if ( obj.value.length == 0  ) {
      strError = strError + "\n Please enter company."
      obj.focus();
    }

    var obj = $('advVoucherTitle');  

    if ( obj.value.length == 0  ) {
      strError = strError + "\n Please enter title."
      obj.focus();
    }  

  }
  if ( strError.length ){
    alert( strError );
    return false;
  }
  $('submitInfo').value = 'submit';
  document.purchase.action="index.php?page=paymentOption";
  document.purchase.submit();
  return false;
}

//EDIT PROFILE
//Signup Validation
function editProfile_validation(){
  strError = "";
  var obj = $('phone');

  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter valid phone number."
    obj.focus();

  }/*else if ( ! isPhone( obj.value ) ){

    strError = strError + "\n Please enter valid phone number."
    obj.focus();

  }*/

  var obj = $('fax');

  /*if ( obj.value.length > 0  ) {

    if ( ! isPhone( obj.value ) ){

      strError = strError + "\n Please enter valid fax number."
      obj.focus();

    }
  
  }*/
  
  var obj = $('firstName');
  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter valid first name."
    obj.focus();

  }else if ( ! isAlpha( obj.value ) ){

    strError = strError + "\n Please enter valid first name."
    obj.focus();

  }
  
  var obj = $('lastName');

  if ( obj.value.length == 0  ) {
    strError = strError + "\n Please enter valid last name."
    obj.focus();

  }else if ( ! isAlpha( obj.value ) ){

    strError = strError + "\n Please enter valid last name."
    obj.focus();

  }
  

  if ( ! validateSelect( $('gender') ) ){

    strError = strError + "\n Please select gender."

  }

  if ( ! validateSelect( $('state') ) ){

    strError = strError + "\n Please enter state."

  }
  
  var obj = $('city');  
  if ( ! isAlpha( obj.value ) ){

    strError = strError + "\n Please enter valid city."
    obj.focus();

  }
  
  var obj = $('zip');

  if ( obj.value.length > 0  ) {

    if ( ! isZip( obj.value ) ){

      strError = strError + "\n Please enter valid pin."
      obj.focus();

    }
  
  }
  if ( strError.length ){

    alert( strError );
    return false;
  }

  return true;
}

//CPASSWORD VALIDATINON

function cpassword_validation(){
  strError="";
  var objPass = getObject('pass');

  if ( objPass.value.length <= 2  ) {
    strError =  "\n Password should not be less then 3."
    objPass.focus();
  }

  var objNewPass = getObject('newpass');
  if ( objNewPass.value.length <= 2  ) {
    strError = strError + "\n New Password should not be less then 3."
    objNewPass.focus();
  }
  
  var objRePass = getObject('repass');
  if ( objRePass.value != objNewPass.value ){
    strError = strError + "\n Password and Retype Password should be match."
    objRePass.focus();
  }

  if ( strError.length ){
    alert( strError );
    return false;
  }

  return true;
}

function contactAgent_validation(){
	f = document.form2;
	if(f.name.value==''){
		alert("Please enter name.");
		f.name.focus();
		return false;
	}
	if(f.email.value==''){
		alert("Please enter email.");
		f.email.focus();
		return false;
	}
	
	if(!echeck(f.email.value)){
			//alert("Please enter correct email.");
			f.email.focus();
			return false;
	}
	
	if(f.tele.value==''){
		alert("Please enter telephone.");
		f.tele.focus();
		return false;
	}
	return true;
	
}


function echeck(str) {
			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			if (str.indexOf(at)==-1){
			   alert("Invalid E-mail ID")
			   return false
			}
	
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			   alert("Invalid E-mail ID")
			   return false
			}
	
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				alert("Invalid E-mail ID")
				return false
			}
	
			 if (str.indexOf(at,(lat+1))!=-1){
				alert("Invalid E-mail ID")
				return false
			 }
	
			 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
				alert("Invalid E-mail ID")
				return false
			 }
	
			 if (str.indexOf(dot,(lat+2))==-1){
				alert("Invalid E-mail ID")
				return false
			 }
			
			 if (str.indexOf(" ")!=-1){
				alert("Invalid E-mail ID")
				return false
			 }
	
			 return true					
	}
 
 function validateMediaform(){
	f= document.frmMedia;
	if(f.mp_name.value==''){
		alert("Please enter name.");
		f.mp_name.focus();
		return false;
	}
	if(f.mp_company.value==''){
		alert("Please enter company name.");
		f.mp_company.focus();
		return false;
	}
	if(f.mp_email.value==''){
		alert("Please enter email.");
		f.mp_email.focus();
		return false;
	}
	if(!echeck(f.mp_email.value)){
		//alert("Please enter email.");
		f.mp_email.focus();
		return false;
	}
	
	return true;
 }
 
 function validatefrmreferbusiness(){
	f= document.frmreferbusiness;
	
	if(f.rf_bname.value==''){
		alert("Please enter company name.");
		f.rf_company.focus();
		return false;
	}
	if(f.rf_email1.value==''){
		alert("Please enter company contact/email.");
		f.rf_email1.focus();
		return false;
	}
	if(!echeck(f.rf_email1.value)){
		//alert("Please enter email.");
		f.mp_email1.focus();
		return false;
	}
	
	if(f.rf_fullname.value==''){
		alert("Please enter your name.");
		f.rf_name.focus();
		return false;
	}
	if(f.rf_email2.value==''){
		alert("Please enter your contact/email.");
		f.rf_email2.focus();
		return false;
	}
	if(!echeck(f.rf_email2.value)){
		//alert("Please enter email.");
		f.mp_email2.focus();
		return false;
	}
	return true;
 }
 
 function validatefrmnewsletter(){
	f= document.frmnewsletter;
	if(f.nl_email.value==''){
		alert("Please enter email.");
		f.nl_email.focus();
		return false;
	}
	if(!echeck(f.nl_email.value)){
		//alert("Please enter email.");
		f.nl_email.focus();
		return false;
	}
	return true;
 }
