function Trim(str)
{

  return LTrim(RTrim(str));

}


function LTrim(str)
{

  for (var i=0; str.charAt(i)==" "; i++)
  {
    str =  str.substring(i,str.length-1);
  }
  return str;
}


function RTrim(str)
{

  for (var i=str.length-1; str.charAt(i)==" "; i--)
  {  
    str = str.substring(0,i);
  }
  return str;
}


function enterRTrim(str)
{
var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;

}
function enterLTrim(str)
{
 var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;

}
function enterTrim(str)
{
return enterRTrim(enterLTrim(str));

}


function isNum(phone)
{
 var reNum=/^[0-9\-]+$/;
	if(!reNum.test(phone))
	{
		return false;
	}
		return true;
}
function isNumeric(elem){
	var reNum=/^[0-9]+$/;
	if(!reNum.test(elem))
	{
		return false;
	}
		return true;
}
function isqty(elem){
	var reNum=/^[0-9]+$/;
	if(!reNum.test(elem))
	{
		return false;
	}
		return true;
}
function isusername(usename)
{
 var reNum=/^[a-zA-Z0-9\._@]+$/;
	if(!reNum.test(usename))
	{
		return false;
	}
		return true;
}
function isAlphabet(elem){
	var alphaExp = /^[a-zA-Z]+$/;
	
	if(!alphaExp.test(elem))
	{
	    return false;
		
	}
		return true;
	
}

function ismaxlength_textarea(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function numbersonly(myfield, e, dec)
{
   var unicode=e.charCode? e.charCode : e.keyCode    
    if (unicode!=8 && unicode!=9)
    { //if the key isn't the backspace key (which we should allow)
   
        if (unicode<48||unicode>57)  //if not a number
        return false //disable key press
    } 
    return true;   
}

function validatePwd(fieldname) {
      //Initialise variables
        var errorMsg = "";
        var space = " ";
        //fieldname = document.myform.password;
        fieldvalue = fieldname;
        fieldlength = fieldvalue.length;

        //It must not contain a space
        if (fieldvalue.indexOf(space) > -1) {
            errorMsg += "\nPasswords cannot include a space.\n";
        }
        //It must contain at least one character     
        if (!((fieldvalue.match(/[A-Z]/)) || (fieldvalue.match(/[a-z]/)))) {
            errorMsg += "\nStrong passwords must include at least one character.\n";
        }
        //It must contain at least one number
        if (!(fieldvalue.match(/\d/))) {
            errorMsg += "\nStrong passwords must include at least one number.\n";
        }
        //It must be at least 7 characters long.
        if (!((fieldlength >= 7) && (fieldlength <= 16))) {
            errorMsg += "\nStrong passwords must be between 7-16 characters long.\n";
        }
        //If there is aproblem with the form then display an error
        if (errorMsg != "") {
           // msg="";
           // msg = "______________________________________________________\n\n";
           // msg += "Please correct the problem(s) with your trial password test it again.\n";
          //  msg += "______________________________________________________\n";
           // errorMsg += alert(msg + errorMsg + "\n\n");
           errorMsg += alert(errorMsg);
            return false;
        }
        return true;
    }



function productqty_validate(pid)
	{
	        var field_name="products_qty_"+pid;
			var av_field_name ="products_av_qnt_"+pid;
			var purchase_val = parseInt(RTrim(document.getElementById(field_name).value));
			var av_val = parseInt(RTrim(document.getElementById(av_field_name).value));
			//alert("Purchased value= "+purchase_val+" "+av_val);
	        if(RTrim(document.getElementById(field_name).value)=="")
                {
                    alert("Please enter product qty");
                    document.getElementById(field_name).focus();
                    document.getElementById(field_name).value="";
                    return false;
                }
				          
                else if(((document.getElementById(field_name).value)=="0") || ((document.getElementById(field_name).value)=="00"))
                {
                    alert("Please enter correct product qty");
                    document.getElementById(field_name).focus();
                    document.getElementById(field_name).value="";
                    return false;
                }
               else if(!isqty(document.getElementById(field_name).value))
			  {
				alert("Please enter correct product qty");
				document.getElementById(field_name).focus();
				return false;
			 }
			else if(purchase_val>av_val)
                {
                   // alert("Your order qnt["+RTrim(document.getElementById(field_name).value)+"] greather than available qnt["+document.getElementById(av_field_name).value+"]");
					alert("Your ordered quantity value is greater than the current stock level ("+document.getElementById(av_field_name).value+" items).\n Please place an order equal or less then the available quantity .");
                    //document.getElementById(av_field_name).focus();
                    return false;
                }
                
                
	return true;
	}
	
function cust_accounteditvalid()
{
        invalid =" ";
        var radio_choice = false;
        for (counter = 0; counter < document.frm_accountedit.customer_gender.length; counter++)
        {
        if (document.frm_accountedit.customer_gender[counter].checked)
        radio_choice = true; 
        }

        if (!radio_choice)
        {
        alert("Please choose a gender type.")
        return (false);
        }
              

    if(RTrim(document.frm_accountedit.customer_fname.value)=="")
        {
            alert("Please enter first name");
            document.frm_accountedit.customer_fname.focus();
            return false;
        }
      if(RTrim(document.frm_accountedit.customer_lname.value)=="")
        {
            alert("Please enter last name");
            document.frm_accountedit.customer_lname.focus();
            return false;
        }
       if(RTrim(document.frm_accountedit.customer_phoneno.value)=="")
        {
            alert("Please enter phone number");
            document.frm_accountedit.customer_phoneno.focus();
            return false;
        }
      if(RTrim(document.frm_accountedit.customer_phoneno.value)!="")
        {
            if(!isNum(document.frm_accountedit.customer_phoneno.value))
			  {
				alert("Please enter correct phone number");
				document.frm_accountedit.customer_phoneno.focus();
				return false;
			 }
        }
        if(RTrim(document.frm_accountedit.customer_faxno.value)!="")
        {
            if(!isNum(document.frm_accountedit.customer_faxno.value))
			  {
				alert("Please enter correct fax number");
				document.frm_accountedit.customer_faxno.focus();
				return false;
			 }
        }
       return true;
}

function addressbook_validate()
{
  
      var radio_choice = false;
       var invalid =" ";
       var counter;
        // Loop from zero to the one minus the number of radio button selections
        for (counter = 0; counter < document.frm_addbook.c_gender.length; counter++)
        {
          // If a radio button has been selected it will return true
        // (If not it will return false)
        if (document.frm_addbook.c_gender[counter].checked)
        radio_choice = true; 
        }

        if (!radio_choice)
        {
        // If there were no selections made display an alert box 
        alert("Please choose a gender type.")
        return false;
        }
              

    if(RTrim(document.frm_addbook.c_fname.value)=="")
        {
            alert("Please enter first name");
            document.frm_addbook.c_fname.focus();
            return false;
        }
      if(RTrim(document.frm_addbook.c_lname.value)=="")
        {
            alert("Please enter last name");
            document.frm_addbook.c_lname.focus();
            return false;
        }
        if(RTrim(document.frm_addbook.c_address1.value)=="")
        {
            alert("Please enter Address 1");
            document.frm_addbook.c_address1.focus();
            return false;
        }
       if(RTrim(document.frm_addbook.c_country.value)=="")
        {
            alert("Please select country name");
            document.frm_addbook.c_country.focus();
            return false;
        }
      
    return true;
}

function validate_password()
{
    var invalid = " ";
		if(RTrim(document.frm_pass.current_password.value)=="")
        {
            alert("Please enter the current password.");
            document.frm_pass.current_password.focus();
            return false;
        }
        if(RTrim(document.frm_pass.current_password.value)!=RTrim(document.frm_pass.oldpassword.value))
		{
		alert("Please enter the correct current password.");
		document.frm_pass.current_password.focus();
		return false;
		}	
        
        
       if(RTrim(document.frm_pass.new_password.value)=="")
        {
            alert("Please enter the new password ");
            document.frm_pass.new_password.focus();
            return false;
        }
        if (document.frm_pass.new_password.value.indexOf(invalid) > -1) {
			 alert( "Please enter valid password. Password should not contain white spaces at any place." );
			 document.frm_pass.new_password.focus();
            return false;
		 }
		 
		if(document.frm_pass.new_password.value!="")
        {
            if(!validatePwd(document.frm_pass.new_password.value))
            {
                 return false;
             }
        }
      if(RTrim(document.frm_pass.new_confpassword.value)=="")
        {
            alert("Please enter the confirm password");
            document.frm_pass.new_confpassword.focus();
            return false;
        }
       if(RTrim(document.frm_pass.new_password.value)!=RTrim(document.frm_pass.new_confpassword.value))
		{
		alert("Password and confirm password do not match. \n Please re-enter the passwords.!");
		document.frm_pass.new_confpassword.value="";
		document.frm_pass.new_confpassword.focus();
		return false;
		}	
	  return true;
}


	function create_account_validation()
{

        invalid =" ";
        var radio_choice = false;
        for (counter = 0; counter < document.frm_create_account.gender.length; counter++)
        {
       if (document.frm_create_account.gender[counter].checked)
        radio_choice = true; 
        }

        if (!radio_choice)
        {
        alert("Please choose a gender.")
        return (false);
        }
            

    if(RTrim(document.frm_create_account.firstname.value)=="")
        {
            alert("Please enter first name");
            document.frm_create_account.firstname.focus();
            return false;
        }
      if(RTrim(document.frm_create_account.lastname.value)=="")
        {
            alert("Please enter last name");
            document.frm_create_account.lastname.focus();
            return false;
        }
        if(RTrim(document.frm_create_account.email_address.value)=="")
        {
            alert("Please enter Email Id");
            document.frm_create_account.email_address.focus();
            return false;
        }
        if (document.frm_create_account.email_address.value != "")
		{
		        re=/\w{1,}/;
		        emailExp= /^\w+(\-\w+)*(\.\w+(\-\w+)*)*@\w+(\-\w+)*(\.\w+(\-\w+)*)+$/ ; 
		        if(!(emailExp.test(document.frm_create_account.email_address.value)))
		        {
		        alert("Please Enter Correct Email Id");
		        document.frm_create_account.email_address.focus();
		        return false;
		        }
		}
        
        
        if(RTrim(document.frm_create_account.address1.value)=="")
        {
            alert("Please enter address1");
            document.frm_create_account.address1.focus();
            return false;
        } 
        
        if(RTrim(document.frm_create_account.country_create_account.value)=="")
        {
            alert("Please select country");
            document.frm_create_account.country_create_account.focus();
            return false;
        } 
        if(RTrim(document.frm_create_account.postcode.value)=="")
        {
            alert("Please enter post code");
            document.frm_create_account.postcode.focus();
            return false;
        }
      if(RTrim(document.frm_create_account.postcode.value)!="")
        {
            if(!isNum(document.frm_create_account.postcode.value))
			  {
				alert("Please enter correct post code");
				document.frm_create_account.postcode.focus();
				return false;
			 }
        }
        
        
      if(RTrim(document.frm_create_account.telephone.value)=="")
        {
            alert("Please enter phone number");
            document.frm_create_account.telephone.focus();
            return false;
        }
      if(RTrim(document.frm_create_account.telephone.value)!="")
        {
            if(!isNum(document.frm_create_account.telephone.value))
			  {
				alert("Please enter correct phone number");
				document.frm_create_account.telephone.focus();
				return false;
			 }
        }
        
        
        if(RTrim(document.frm_create_account.fax.value)!="")
        {
            if(!isNum(document.frm_create_account.fax.value))
			  {
				alert("Please enter correct fax number");
				document.frm_create_account.fax.focus();
				return false;
			 }
        }
                        
        if(RTrim(document.frm_create_account.password.value)=="")
        {
            alert("Please enter password ");
            document.frm_create_account.password.focus();
            return false;
        }
        if (document.frm_create_account.password.value.indexOf(invalid) > -1) {
			 alert( "Please enter valid password. Password should not contain white spaces at any place." );
			 document.frm_create_account.password.focus();
            return false;
		 }
		 
		if(RTrim(document.frm_create_account.password.value) !="")
        {
             if(!validatePwd(document.frm_create_account.password.value))
            {
                 return false;
            }
        }
      if(RTrim(document.frm_create_account.conf_password.value)=="")
        {
            alert("Please enter the password confirmation");
            document.frm_create_account.conf_password.focus();
            return false;
        }
       if(RTrim(document.frm_create_account.password.value)!=RTrim(document.frm_create_account.conf_password.value))
		{
		alert("Password and password confirmation do not match. \n Please re-enter the password confirmation.!");
		document.frm_create_account.conf_password.value="";
		document.frm_create_account.conf_password.focus();
		return false;
		}	
        
        /*
         if(document.getElementById("customer_count").value == '1')
                {           
                
                alert("Only one product can be featured.To make this product featured please make the previous product unfeatured");
                return false;
                }
        */
         
    return true;
}

function productqtyspecial_validate(speid)
	{
	        var field_name_spe="products_qty_"+speid;
	        if(RTrim(document.getElementById(field_name_spe).value)=="")
                {
                    alert("Please enter product qty");
                    document.getElementById(field_name_spe).focus();
                    document.getElementById(field_name_spe).value="";
                    return false;
                }
                
                
                if(((document.getElementById(field_name_spe).value)=="0") || ((document.getElementById(field_name_spe).value)=="00"))
                {
                    alert("Please enter correct product qty");
                    document.getElementById(field_name_spe).focus();
                    document.getElementById(field_name_spe).value="";
                    return false;
                }
                
                
              if(!isqty(document.getElementById(field_name_spe).value))
			  {
				alert("Please enter correct product qty");
				document.getElementById(field_name_spe).focus();
				return false;
			 }
                
                
	return true;
	}
	
function validate_shipping() {
        var radio_choice = false;
        for (var counter = 0; counter < document.frm_shipping.shipping.length; counter++)
        {
           if (document.frm_shipping.shipping[counter].checked)
            radio_choice = true; 
            }

        if (!radio_choice)
        {
        alert("Please choose shipping method.")
        return (false);
        }
                      
	return true;  
        
}
	
function contactus_validation()
{

        

    if(RTrim(document.contact.fullname.value)=="")
        {
            alert("Please enter full name");
            document.contact.fullname.focus();
            return false;
        }
     
        if(RTrim(document.contact.emailadd.value)=="")
        {
            alert("Please enter Email Id");
            document.contact.emailadd.focus();
            return false;
        }
        if (document.contact.emailadd.value != "")
		{
		        re=/\w{1,}/;
		        emailExp= /^\w+(\-\w+)*(\.\w+(\-\w+)*)*@\w+(\-\w+)*(\.\w+(\-\w+)*)+$/ ; 
		        if(!(emailExp.test(document.contact.emailadd.value)))
		        {
		        alert("Please Enter Correct Email Id");
		        document.contact.emailadd.focus();
		        return false;
		        }
		}
        
             
      if(RTrim(document.contact.contactno.value)!="")
        {
            if(!isNum(document.contact.contactno.value))
			  {
				alert("Please enter correct contact number");
				document.contact.contactno.focus();
				return false;
			 }
        }
        
         if(enterTrim(document.contact.enquiry.value)=="")
        {
            alert("Please enter enquiry detail.");
            document.contact.enquiry.focus();
            return false;
        }
      	
    return true;
}


function search_validation()
{
   if(RTrim(document.frm_seach.prodkey.value)=="")
        {
            alert("Please product name");
            document.frm_seach.prodkey.focus();
            return false;
        }
       return true;
}
		
		function emailfriend_validation()
{

        

    if(RTrim(document.emailtofriend.y_name.value)=="")
        {
            alert("Please enter your name");
            document.emailtofriend.y_name.focus();
            return false;
        }
     
        if(RTrim(document.emailtofriend.y_email_id.value)=="")
        {
            alert("Please enter your Email Id");
            document.emailtofriend.y_email_id.focus();
            return false;
        }
        if (document.emailtofriend.y_email_id.value != "")
		{
		        re=/\w{1,}/;
		        emailExp= /^\w+(\-\w+)*(\.\w+(\-\w+)*)*@\w+(\-\w+)*(\.\w+(\-\w+)*)+$/ ; 
		        if(!(emailExp.test(document.emailtofriend.y_email_id.value)))
		        {
		        alert("Please Enter Correct Email Id");
		        document.emailtofriend.y_email_id.focus();
		        return false;
		        }
		}
        
             
       if(RTrim(document.emailtofriend.f_name.value)=="")
        {
            alert("Please enter your friend name");
            document.emailtofriend.f_name.focus();
            return false;
        }
     
        if(RTrim(document.emailtofriend.f_email_id.value)=="")
        {
            alert("Please enter your friend Email Id");
            document.emailtofriend.f_email_id.focus();
            return false;
        }
        if (document.emailtofriend.f_email_id.value != "")
		{
		        re=/\w{1,}/;
		        emailExp= /^\w+(\-\w+)*(\.\w+(\-\w+)*)*@\w+(\-\w+)*(\.\w+(\-\w+)*)+$/ ; 
		        if(!(emailExp.test(document.emailtofriend.f_email_id.value)))
		        {
		        alert("Please Enter Correct Email Id");
		        document.emailtofriend.f_email_id.focus();
		        return false;
		        }
		}
      	
    return true;
}
