/*Validation for Quick Registration control */

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

   
    function CallDoc()
    {
    
        var txtName = document.getElementById("Ctrl_Quick_Registration1_txtName");
        var txtEmailid = document.getElementById("Ctrl_Quick_Registration1_txtEmail");
        var txtReEmailid = document.getElementById("Ctrl_Quick_Registration1_txtReEmail");
        var txtPassword = document.getElementById("Ctrl_Quick_Registration1_txtPassword");
        var txtConfirmpass = document.getElementById("Ctrl_Quick_Registration1_txtRePassword");
        var txtMobile = document.getElementById("Ctrl_Quick_Registration1_txtMobile");
  
        

        if (txtName.value=='')
        {
            alert('Enter the Name');
            txtName.focus();
            return false;
            
        }
        if (txtName.value.length > 50)
	    {
		    alert('Login Id should not be more than 50 characters');
		     txtName.focus();
		    return false;
	    }

  if(!ValidateFields(txtPassword.value,4,15))
        {
        alert('Invalid Password');
        txtPassword.focus();
        return false;
        }
        
        
        if(!ValidateFields(txtConfirmpass.value,4,15))
        {
        alert('Please Re-Enter Same Password');
         txtConfirmpass.focus();
        return false;
        }
        
           
        if(txtPassword.value != txtConfirmpass.value)
        {
        alert('Password Mismatch');
         txtConfirmpass.focus();
        return false;
        }


        if (txtEmailid.value=='')
        {
            alert('Enter Email id');
            txtEmailid.focus();
            return false;
            
        }
        if(!val_email(txtEmailid.value))
        {
        alert('Please Enter valid Email id');
         txtEmailid.focus();
        return false;
        }
        
        if(!val_email(txtReEmailid.value))
        {
        alert('Please Re-Enter valid Email id');
         txtReEmailid.focus();
        return false;
        }

        if(txtEmailid.value != txtReEmailid.value)
        {
        alert('Email Address Mismatch');
        txtReEmailid.focus();
        return false;
        }

      
   
       /* if (checkUKTelephone (txtMobile.value)!=true)
        {
            alert (telNumberErrors[telNumberErrorNo]);
             txtMobile.focus();
            return false;
        }*/

	if (isInteger(txtMobile.value)==false)
                    {
                        alert("Please enter valid mobile number.");
                        txtMobile.focus();
                        return false;          
                     }


		 



        if(!document.getElementById("Ctrl_Quick_Registration1_chkterms").checked)
        { 
            alert('You need to agree terms & conditions before proceeding ahead.'); 
            return false;
        }
       
       // document.getElementById("imgSubmit").style.visibility = "hidden" 11-Jun-10
  document.getElementById("imgSubmit").src= "../images/please-wait.jpg" ;
     document.getElementById("imgSubmit").width= "85" ;
     document.getElementById("imgSubmit").height= "20" ;
        
              
        var sessionObj = new sack();
        sessionObj.requestFile ='frm_quickreg.aspx?qname=' + document.getElementById("Ctrl_Quick_Registration1_txtName").value + '&qemail=' +  document.getElementById("Ctrl_Quick_Registration1_txtEmail").value + '&qpassword=' + document.getElementById("Ctrl_Quick_Registration1_txtPassword").value  + '&qmobile=' + document.getElementById("Ctrl_Quick_Registration1_txtMobile").value ;
        
        sessionObj.onCompletion = function(){ 
        ////close registration table and open download table
        var status=sessionObj.response;
        if(status=='Success')
        {

        document.getElementById('divOne').style.display='none';
        document.getElementById('Ctrl_Quick_Registration1_divTwo').style.display='block';
        document.getElementById("Ctrl_Quick_Registration1_divThree").style.display='block';
//        document.getElementById("tblQuickReg").style.display='none';
        }
        else if(status=='Failed')
        {

        alert('Registration failed, Please try again');
        }
        else if(status=='LoginExist')
        {
        alert('Login Id exists');
        }
        else if(status=='EmailExist')
        {
        alert('Email Id already exists');
        }
        else
        {
        alert('Some error has occured with server,Please try again');
        }
          document.getElementById("imgSubmit").style.visibility = "visible";
 document.getElementById("imgSubmit").src= "../images/button_submit.gif" ;
     document.getElementById("imgSubmit").width= "65" ;
     document.getElementById("imgSubmit").height= "20" ;
        


        }; 

        sessionObj.runAJAX(); 
        
    }

 
      function CallTimer()
    {
   
    
        var sessionObj = new sack();
        sessionObj.requestFile ='frm_getKeys.aspx?id=' + document.getElementById('hdnId').innerText;
       
       sessionObj.onCompletion = function(){ 
        ////close registration table and open download table
        var status=sessionObj.response;
         
       
        if(status=='Failed')
        {
       //  alert('Some error has occured with server,Please try again');
lbl_message.innerText="Note: ";
lbl_message.innerText+="Sometimes in rare cases, in busy hours because of the web traffic, it may take few minutes to update your activation key after successful payment processing." ;
lbl_message.innerText+="In that case, please contact us on support@pass-test.co.uk. ";
   var img_progress;
  img_progress= document.getElementById('img_progress');
  img_progress.style.display="none";  

lbl_product.innerText="";
   





        }      
        else
        {
         hdnId.innerText="Following are you details:";
        lbl_message.innerText="";
        
         var fetchValue;
         fetchValue=status.toString().split('$$');                    
         lbl_product.innerText="Product id: "+ fetchValue[0]    ;
         lbl_activation_keys.innerText="Activation key: " + fetchValue[1]    ;     
         
         var img_progress;
           img_progress= document.getElementById('img_progress');
          
           img_progress.style.display="none";  
        }
        
        
         // document.getElementById("imgSubmit").style.visibility = "visible"
        }; 

        sessionObj.runAJAX(); 
        
    }

    
    function validateChars(str)
    {
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";	
        for (var i = 0; i < str.length; i++)
        {
            if (iChars.indexOf(str.charAt(i)) != -1) 
            {
  	            return false;
            }
        }
        return true;
    }

function validateUsername(str)
{
//This will just allow characters and numbers. (._)
     var matchArray =  /[^a-z|_|.\d]/i;
     if(str.match(matchArray) == null)  
     {
        return true;             
     }
     	
     return false;


} 

    function checkUKTelephone (telephoneNumber) {

    // Convert into a string and check that we were provided with something
    var telnum = telephoneNumber + " ";
    if (telnum.length == 1)  {
    telNumberErrorNo = 1;
    return false;
    }
    telnum.length = telnum.length - 1;

    // Don't allow country codes to be included (assumes a leading "+")
    var exp = /^(\+)[\s]*(.*)$/;
    if (exp.test(telnum) == true) {
    telNumberErrorNo = 2;
    return false;
    }

    // Remove spaces from the telephone number to help validation
    while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
    }

    // Remove hyphens from the telephone number to help validation
    while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
    }  

    // Now check that all the characters are digits
    exp = /^[0-9]{10,11}$/
    if (exp.test(telnum) != true) {
    telNumberErrorNo = 3;
    return false;
    }

    // Now check that the first digit is 0
    exp = /^0[0-9]{9,10}$/
    if (exp.test(telnum) != true) {
    telNumberErrorNo = 4;
    return false;
    }

    // Finally check that the telephone number is appropriate.
    exp = /^(01|02|03|05|070|077|07624|078|079)[0-9]+$/;
    if (exp.test(telnum) != true) {
    telNumberErrorNo = 5;
    return false;
    }

    // Telephone number seems to be valid - return the stripped telehone number  
    return true;
    }
    var telNumberErrorNo = 0;
    var telNumberErrors = new Array ();
    telNumberErrors[0] = "Valid UK telephone number";
    telNumberErrors[1] = "Telephone number not provided";
    telNumberErrors[2] = "Please remove country code";
    telNumberErrors[3] = "Telephone numbers should contain 10 or 11 digits";
    telNumberErrors[4] = "The telephone number should start with a 0";
    telNumberErrors[5] = "The telephone number is either invalid or inappropriate";

    
    function val_email(str) 
    {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
        var iChars = "!#$%^&*()+=[]\\\';,/{}|\":<>?";
		if (str.indexOf(at)==-1)
		{
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    return false;
		 }
	 
		 for (var i = 0; i < str.length; i++) 
	     {	  
			if (iChars.indexOf(str.charAt(i)) != -1) 
			{
			   return false;			
			}
         }
    	return true;	
	}
	
	function ValidateFields(value,minLength,maxLength)
    {
        if (value=='')
		{
    		return false;
		}

		if (value.length < minLength || value.length > maxLength)  
		{
		//alert('Your password must be at least ' + minLength + ' characters long and be less than' + maxLength + ' characters.');
    		return false;
		}

		// check for spaces invalid chars
		if (validateChars(value) !=true)
		{
		//alert ("Your password has special characters or spaces. \nThese are not allowed.\n Please remove them and try again.");
    		return false;
		}
		return true;
    }
////postcode validation function//////
	   function funpostcode(post_code)
 { //check postcode format is valid
     //test = document.details.pcode.value; 
     var test = post_code.value
     var size = test.length
     
     test = test.toUpperCase(); //Change to uppercase
     while (test.slice(0,1) == " ") //Strip leading spaces
      {test = test.substr(1,size-1);size = test.length
      }
     while(test.slice(size-1,size)== " ") //Strip trailing spaces
      {test = test.substr(0,size-1);size = test.length
      }
     //document.details.pcode.value = test; //write back to form field
     if (size < 6 || size > 8){ //Code length rule
      alert(test + " is not a valid postcode - wrong length");
      post_code.focus();
      return false;
      }
     if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
       alert(test + " is not a valid postcode - cannot start with a number");
       post_code.focus();
       return false;
      }
     if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
       alert(test + " is not a valid postcode - alpha character in wrong position");
       post_code.focus();
       return false;
      }
     if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
       alert(test + " is not a valid postcode - number in wrong position");
       post_code.focus();
       return false;
      }
     if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
       alert(test + " is not a valid postcode - number in wrong position");
       post_code.focus();
       return false;
      }
     if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
       alert(test + " is not a valid postcode - no space or space in wrong position");
       post_code.focus();
       return false;
       }
       
     var count1 = test.indexOf(" ")
     var count2 = test.lastIndexOf(" ")
     if (count1 != count2){//only one space rule
       alert(test + " is not a valid postcode - only one space allowed");
       post_code.focus();
       return false;
      }
    //alert("Postcode Format OK");
    return true;
    }
//*Validation for Quick Registration control */
   
    function validate_registration()
    {
   
        var txtName = document.getElementById("txtName");
        var txtEmailid = document.getElementById("txtEmail");
        var txtReEmailid = document.getElementById("txtReEmail");
        var txtPassword = document.getElementById("txtPassword");
        var txtConfirmpass = document.getElementById("txtRePassword");
        var txtMobile = document.getElementById("txtMobile");
        var txtPost_Code= document.getElementById("txtpost_code");
  
        

        if (txtName.value=='')
        {
            alert('Enter the Name');
            txtName.focus();
            return false;
            
        }
        if (txtName.value.length > 50)
	    {
		    alert('Login Id should not be more than 50 characters');
		     txtName.focus();
		    return false;
	    }

	 if (validateUsername(txtName.value) !=true)
		{
		alert ("Your user name has special characters or spaces. \nThese are not allowed.\n Please remove them and try again.");
		txtName.focus();
		return false;
		}

	     if(!ValidateFields(txtPassword.value,4,15))
        {
        alert('Invalid Password');
        txtPassword.focus();
        return false;
        }
        
        
        if(!ValidateFields(txtConfirmpass.value,4,15))
        {
        alert('Please Re-Enter Same Password');
         txtConfirmpass.focus();
        return false;
        }
        
           
        if(txtPassword.value != txtConfirmpass.value)
        {
        alert('Password Mismatch');
         txtConfirmpass.focus();
        return false;
        }
        if (txtEmailid.value=='')
        {
            alert('Enter Email id');
            txtEmailid.focus();
            return false;
            
        }
        if(!val_email(txtEmailid.value))
        {
        alert('Please Enter valid Email id');
         txtEmailid.focus();
        return false;
        }
        
        if(!val_email(txtReEmailid.value))
        {
        alert('Please Re-Enter valid Email id');
         txtReEmailid.focus();
        return false;
        }

        if(txtEmailid.value != txtReEmailid.value)
        {
        alert('Email Address Mismatch');
        txtReEmailid.focus();
        return false;
        }

       
   
       /* if (checkUKTelephone (txtMobile.value)!=true)
        {
            alert (telNumberErrors[telNumberErrorNo]);
             txtMobile.focus();
            return false;
        }*/
        if( txtPost_Code.value=="")
		{
		alert("Please enter postcode.");
		 txtPost_Code.focus();
		return false;
		}
		
		if (funpostcode(txtPost_Code)!=true)
		{
		//alert ("Enter valid postcode.");
		 txtPost_Code.focus();
		return false;
		}
		
        if(!document.getElementById("chkterms").checked)
        { 
            alert('You need to agree terms & conditions before proceeding ahead.'); 
            return false;
        }
       
        document.getElementById("imgSubmit").style.visibility = "hidden"
        
              
        var sessionObj = new sack();
        sessionObj.requestFile ='frm_quickregister.aspx?qname=' + document.getElementById("txtName").value + '&qemail=' +  document.getElementById("txtEmail").value + '&qpassword=' + document.getElementById("txtPassword").value  + '&qmobile=' + document.getElementById("txtMobile").value + '&qpost_code=' + document.getElementById("txtpost_code").value ;
        
        sessionObj.onCompletion = function(){ 
        ////close registration table and open download table
        var status=sessionObj.response;
        if(status=='Success')
        {
        document.getElementById("panel_download").style.display='inline';
        document.getElementById("panel_try").style.display='inline';
        document.getElementById("panel_register").style.display='none';
        }
        else if(status=='Failed')
        {
        alert('Registration failed, Please try again');
        }
        else if(status=='LoginExist')
        {
        alert('Login Id exists');
        }
        else if(status=='EmailExist')
        {
        alert('Email Id already exists');
        }
        else
        {
        alert('Some error has occured with server,Please try again');
        }
          document.getElementById("imgSubmit").style.visibility = "visible"
        }; 

        sessionObj.runAJAX(); 
        
    }
    

  function validate_login()
    {
        var txtLoginid = document.getElementById("txtloginid");
        var txtPass = document.getElementById("txtpass");
        
        if (txtLoginid.value=='')
        {
            alert('Enter the Login id');
            txtLoginid.focus();
            return false;
            
        }
        if (txtLoginid.value.length > 50)
	    {
		    alert('Login Id should not be more than 50 characters');
		    txtLoginid.focus();
		    return false;
	    }
	     if(!ValidateFields(txtPass.value,4,15))
        {
        alert('Invalid Password');
        txtPass.focus();
        return false;
        }
         var sessionObj = new sack();
        sessionObj.requestFile ='Checklogin.aspx?loginid=' + document.getElementById("txtloginid").value + '&password=' +  document.getElementById("txtpass").value ;
        
        sessionObj.onCompletion = function(){ 
        ////close registration table and open download table
        var status=sessionObj.response;
        if(status=='Success')
        {
        document.getElementById("panel_download").style.display='inline';
        document.getElementById("panel_try").style.display='inline';
        document.getElementById("panel_register").style.display='none';
        document.getElementById("panel_login").style.display='none';
        }
        else if(status=='Failed')
        {
        alert('Login failed, Please try again');
         txtLoginid.focus();
        }
        else
        {
        alert('Some error has occured with server,Please try again');
         txtLoginid.focus();
        }
        
        }; 

        sessionObj.runAJAX(); 
               
    
    }

