﻿// JScript File

function Validate()
{
var isvalid = true

var idprefix='';

    var Validate2 = new Array(idprefix+'txtPassword',idprefix+'txtRetype', idprefix+'txtFirstName',  idprefix+'txtState',idprefix+'txtEmail',idprefix+'txtCity',idprefix + 'txtPostCode',idprefix+'txtCaptcha',idprefix+'txtAnswer');
    document.getElementById(idprefix+'lblError').innerHTML = '';
    if (!AlphaNumericValidations(idprefix+'txtPassword'))
    {
     isvalid = false;    
    }
    else if(eval(document.getElementById(idprefix+'txtPassword').value.length)<8)
    {
    alert('The Password length should be atleast 8 chars');
     isvalid = false;    
    }
    else if(document.getElementById(idprefix+'txtPassword').value != document.getElementById(idprefix+'txtRetype').value)
    {
        CreateSpan(document.getElementById(idprefix+'txtRetype').id, 'Incorrect Password', 'SpanStyle')
        isvalid =false;
    }
    else
    {
        ClearSpanText(document.getElementById(idprefix+'txtRetype').id)
    }
    if(!RequiredFieldWithBorder(Validate2))
    {
        isvalid = false;
    }    
    else if(!EmailCheck(idprefix+'txtEmail'))
    {
        isvalid = false;
    }
    var chkbox = 'divTerms';
    if(document.getElementById('cboxTerms').checked == true)
    {
        document.getElementById(chkbox).className='';
    }
    else
    {
        document.getElementById(chkbox).className='inputText_Valid_Wrong';
         isvalid = false;
    }
   
    if(document.getElementById('dlSecurity').selectedIndex ==0)
    {
        document.getElementById('divSecQuestion').className='inputText_Valid_Wrong';
                  isvalid = false;
    }
    else
    {
          document.getElementById('divSecQuestion').className='';

    }
    if(document.getElementById('ddCountry').selectedIndex ==0)
    {
        document.getElementById('divCountry').className='inputText_Valid_Wrong';
                  isvalid = false;
    }
    else
    {
          document.getElementById('divCountry').className='';

    }
     if(document.getElementById('dlaboutus').selectedIndex ==0)
    {
        document.getElementById('divAboutUs').className='inputText_Valid_Wrong';
                  isvalid = false;
    }
    else
    {
          document.getElementById('divAboutUs').className='';

    }
    
    
    
    //Radio Button List Validation
	  var rbl = 'rblGender';	        
      if (document.getElementById(rbl + '_1').checked == true || document.getElementById(rbl + '_0').checked == true)
      {
         document.getElementById(rbl).className='inputText';  
      }
      else
      {
        document.getElementById(rbl).className='inputText_Valid_Wrong';
        isvalid = false;
      }
     var postcode=document.getElementById(idprefix + 'txtPostCode');
     if(isvalid == true)
     {
         if(!postit(postcode))
         {
         CreateBorder(idprefix + 'txtPostCode');
            isvalid = false;
         }
     }  
    if(isvalid==true)
    {
    if(document.getElementById(idprefix + 'txtMobile').value!='')
    {
        if(!ValidateUKMobileNumber(idprefix + 'txtMobile'))
        {
            isvalid = false;
        }
     }      
    }   
    if(isvalid==true)
    {
    if(document.getElementById(idprefix + 'txtPhone').value!='')
    {
        if(!ValidateUKPhoneNumber(idprefix + 'txtPhone'))
        {
            isvalid = false;
        }
     }      
    }   
    return isvalid;
}
function postit(postcode) 
{ 
//check postcode format is valid
test = postcode.value; 
size = test.length
//Change to uppercase
test = test.toUpperCase();
        
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
                }
//write back to form field
        postcode.value = test;
                if (size < 6 || size > 8)
{ 
//Code length rule
                alert(test + " is not a valid postcode - wrong length");
                        postcode.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");
                        postcode.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");
                        postcode.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");
                        postcode.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");
                        postcode.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");
                        postcode.focus();
                        return false;
                }
                count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
                if (count1 != count2)
{
//only one space rule
                        alert(test + " is not a valid postcode - only one space allowed");
                        postcode.focus();
                        return false;
                }
//alert("Postcode Format OK");
return true;
}


function AlphaNumericValidations(Id)
{
	try
	{
		var reg1 = /((?:[a-z]*[0-9]+[a-z0-9]*))/; // valid
			
		Value = trimAll(document.getElementById(Id).value)
		

		if(Value != '')
		{
		if(! reg1.test(document.getElementById(Id).value))
		{
			CreateBorder(Id);
			alert('Password Doen\'t match the specified Format');
			return false;
		}
		}
		ClearBorder(Id);
		return true;
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('8')
		return false ;
	}
}



function EmailCheck(Id)
{
	try
	{
		var reg1 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		var reg2 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		
		Value = trimAll(document.getElementById(Id).value)
		

		if(Value != '')
		{
		if(! reg1.test(document.getElementById(Id).value) || reg2.test(document.getElementById(Id).value))
		{
			CreateBorder(Id);
			return false
		}
		}
		ClearBorder(Id);
		return true
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('8')
		return false 
	}
}

function LoginValidate()
{
    var isvalid = true
    var idprefix='';
    var Validate2 = new Array(idprefix+'txtUserName',idprefix+'txtPassword');
    if(!RequiredFieldWithBorder(Validate2))
    {
        isvalid = false;
    }
    if (!EmailCheck(idprefix+'txtUserName'))
    {
     isvalid = false;
    }
    
    return isvalid;
}
function ValidateShipping()
{
var isvalid = true


    var Validate1 = new Array('txtBillingAddress','txtBillingPostCode','txtContactNumber');
    var Validate2 = new Array('txtDeliveryAddress','txtDeliveryPostCode');
   
   

    if(!RequiredFieldWithBorder(Validate1))
    {
        isvalid = false;
    }

   

    if(document.getElementById('ddBillingCountry').value=='0')
    {
        CreateBorder('divCountry1');
        isvalid = false;
    }
    else
    {
        document.getElementById('divCountry1').className='';
    }
    var postcode=document.getElementById('txtBillingPostCode');
         if(isvalid == true)
         {
             if(!postit(postcode))
             {
             CreateBorder('txtBillingPostCode');
                isvalid = false;
             }
         }    
    if(document.getElementById('rdDeliverySame_1').checked==true)
    {
        if(!RequiredFieldWithBorder(Validate2))
        {
            isvalid = false;
        }  
        if(document.getElementById('ddDeliveryCountry').value=='0')
        {
            CreateBorder('divCountry2');
            isvalid = false;
        }
        else
        {
            document.getElementById('divCountry2').style.border='';
        } 
         var postcode=document.getElementById('txtDeliveryPostCode');
         if(isvalid == true)
         {
             if(!postit(postcode))
             {
             CreateBorder('txtDeliveryPostCode');
                isvalid = false;
             }
         }      
    }
    else
    {
        document.getElementById('txtDeliveryAddress').className='inputText';
        document.getElementById('txtDeliveryPostCode').className='inputText';
        document.getElementById('divCountry2').className='inputText'; 
         
    }
   if(document.getElementById('ddParcelService').value=='0')
    {
        CreateBorder('divParcel');
        isvalid = false;
    }
    else
    {
        document.getElementById('divParcel').className='';
    }
    
   if(document.getElementById('rdProtx').checked==false)
    {
        isvalid = false;
        alert('Please select a payment method for your order');
    }
    
    if(!ValidateNumber('txtContactNumber'))
    {
    isvalid = false;
    }
    if(document.getElementById('txtFaxNumber').value.length!=0)
    {  
        if(!ValidateNumber('txtFaxNumber'))
        {
        document.getElementById('txtFaxNumber').className='inputText_Valid_Wrong';
        isvalid = false;
        }
    }
    else
    document.getElementById('txtFaxNumber').className='inputText';
   if(isvalid==true)
    {
    if(document.getElementById('txtContactNumber').value!='')
    {
        if(!ValidateUKPhoneNumber('txtContactNumber'))
        {
            isvalid = false;
        }
     }      
    }   
      
    
//    if(isvalid==true)
//    {
//    if(!EmailCheck(idprefix+'txtEmail'))
//    {
//        isvalid = false;
//    }
//    }
    return isvalid;
}
function ValidateNumber(ID)
{

var digits='0123456789';
var temp;

for (var i=0;i<document.getElementById(ID).value.length;i++)
{
temp=document.getElementById(ID).value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
{
document.getElementById(ID).className='inputText_Valid_Wrong';
return false;
}
else
{
document.getElementById(ID).className='inputText';
return true;
}
}
}
function TogglePop_Login()
{

if (document.getElementById('divError').style.display!='none')
    document.getElementById('divError').style.display='none';
    else
    document.getElementById('divError').style.display='';
 }

function changePassword()
{
var isvalid = true

var idprefix='TabContainer1_tabPwd_';

document.getElementById(idprefix+'lblPwdMsg').innerHTML='';
var Validate = new Array(idprefix+'txtOldPassword',idprefix+'txtNewPassword',idprefix+'txtConfirmPassword');
    if(!RequiredFieldWithBorder(Validate))
    {
        isvalid = false;
    }  
    if(isvalid == true)
    {
if(document.getElementById(idprefix+'txtOldPassword').value != document.getElementById(idprefix+'hidValue').value)
{
    CreateBorder(idprefix+'txtOldPassword');
    alert('Incorrect Old Password');
    //CreateSpan(document.getElementById(idprefix+'txtOldPassword').id, 'Incorrect Old Password', 'SpanStyle')
    return false;
}
else
ClearSpanText(document.getElementById(idprefix+'txtOldPassword').id);

if (!AlphaNumericValidations(idprefix+'txtNewPassword'))
    {
     isvalid = false;    
    }
    else if(eval(document.getElementById(idprefix+'txtNewPassword').value.length)<8)
    {
    alert('The Password length should be atleast 8 chars');
     isvalid = false;    
    }
    else if(document.getElementById(idprefix+'txtNewPassword').value != document.getElementById(idprefix+'txtConfirmPassword').value)
    {
    CreateBorder(idprefix+'txtConfirmPassword');
    //CreateSpan(document.getElementById(idprefix+'txtConfirmPassword').id, 'Incorrect Confirm Password', 'SpanStyle')
    return false;
    }
    else
    ClearSpanText(document.getElementById(idprefix+'txtConfirmPassword').id);
    }
return isvalid;
}
function ValidateAccount()
{
var isvalid = true
var idprefix='TabContainer1_tabAccountSetting_';

    var Validate2 = new Array(idprefix+'txtFirstName',  idprefix+'txtState',idprefix+'txtCity',idprefix + 'txtPostCode',idprefix+'txtPhone');
    document.getElementById(idprefix+'lblMsg').innerHTML = '';
    if(!RequiredFieldWithBorder(Validate2))
    {
        isvalid = false;
    }    
    
    if(document.getElementById(idprefix+'ddCountry').value=='Select')
    {
        CreateBorder('divCountry');
        isvalid = false;
    }
    else
    {
        document.getElementById('divCountry').style.border='';
    }
    //Radio Button List Validation
	  var rbl = idprefix+'rblGender';	        
      if (document.getElementById(rbl + '_1').checked == true || document.getElementById(rbl + '_0').checked == true)
      {
         document.getElementById(rbl).className='inputText';   
      }
      else
      {
        document.getElementById(rbl).className='inputText_Valid_Wrong';
        invalid = false;
      }
     var postcode=document.getElementById(idprefix + 'txtPostCode');
     if(isvalid == true)
     {
         if(!postit(postcode))
         {
         CreateBorder(idprefix + 'txtPostCode');
            isvalid = false;
         }
     }  
    if(isvalid==true)
    {        
        if(!ValidateUKPhoneNumber(idprefix + 'txtPhone'))
        {
            isvalid = false;
        }       
    }     
    return isvalid;
}
