String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/*------------------------ Start user validation ----------------------------------*/

function user_required(field,alerttxt)
{ 
	if (field==null||field=="")
	{ 
		alert(alerttxt); 
		return false; 
	}
}


function validateUser()
{

 	var char_format=/^[A-Za-z][A-Za-z .-_0-9]*$/;
 	var state_format=/^[A-Za-z][A-Za-z.-_0-9()[] ]*$/;
 	var name_format=/^[A-Za-z][A-Za-z .-]*$/;
 	var email_format=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
 	var num_format=/^[0-9]*$/;
 	var password_format=/^[A-Za-z._0-9-]*$/;
// 	var password_format=/[^ ]*$/;

	var first_name=document.getElementById('customers_name').value;
	if(user_required(first_name,'Please enter customer name.')==false)
	{
		$('customers_name').focus();
		$('customers_name').select();
		return false;
	}
	if (!(name_format.test(first_name)))
	{
		alert('Please enter only character in customer name.');
		$('customers_name').focus();
		$('customers_name').select();
		return false;
	}
 	var month=document.getElementById('month').value;
	if(month=='month' || month=='')
	{
		alert('Please Select month.');
		$('month').focus();
		return false;
	}
	var day=document.getElementById('day').value;
	if(day=='day' || day=='')
	{
		alert('Please Select day.');
		$('day').focus();
		return false;
	}
	var year=document.getElementById('year').value;
	if(year=='year' || year=='')
	{
		alert('Please Select year.');
		$('month').focus();
		return false;
	}
	if(year==dateObj.getFullYear())
	{
		var cur = 1 + dateObj.getMonth();
		if(month > cur){
			alert('Please select valid date of birth.');
			$('month').focus();
			return false;
		}else if( (month == cur) || (day > dateObj.getDate()) ){
			alert('Please select valid date of birth.');
			$('month').focus();
			return false;
		}
	}
	var email=$('email').value;
	if (email==null||email=="")
	{ 
		alert('Please enter email.'); 
// 		$('email').focus();
		$('email').select();
		return false; 
	}
	if (!(email_format.test(email)))
	{
		alert('Please enter valid email.');
// 		$('email').focus();
		$('email').select();
		return false;
	}

// 		var url = host+'customers/emailcheck/email_ch/'+email;
// 		new Ajax.Updater('availablemsg',url,{asynchronous:true, evalscripts:true, onComplete: function(request){ 
// 
// 				if(request.responseText=="<span class='available_msg'>Email available.</span>")
// 				{ 
// 				document.getElementById('status_value').value="Y";
// 				}
// 				else
// 				{
// 				document.getElementById('status_value').value="N";
// 				}
// 			}
// 		});
	if($('status_value').value!="Y")
	{
		alert('Please enter another email, this email is already exists.');
// 		$('email').focus();
		$('email').select();
		return false;
	}
	var confirmemail=$('confirmemail').value;
	if (confirmemail==null||confirmemail=="")
	{ 
		alert('Please enter confirm email.'); 
		$('confirmemail').focus();
		$('confirmemail').select();
		return false; 
	}
	if (!(email_format.test(confirmemail)))
	{
		alert('Please enter valid confirm email.');
		$('confirmemail').focus();
		$('confirmemail').select();
		return false;
	}

	if (confirmemail!=email)
	{ 
		alert('Email and confirm email not match.'); 
		$('confirmemail').focus();
		$('confirmemail').select();
		return false; 
	}
	var password=$('cust_password').value;
	//var password=document.getElementById('password').value;
		//alert("AAA"+password);
	if (password==null||password=="")
	{ 
		alert('Please enter password.'); 
		$('cust_password').focus();
		$('cust_password').select();
		return false; 
	}

	if (!(password_format.test(password)))
	{
		alert('Please enter password without space.');
		$('cust_password').focus();
		$('cust_password').select();
		return false;
	}

	var passwordlength=$('cust_password').value.length;
//alert(passwordlength);
	if (passwordlength<6 || passwordlength>18)
	{ 
		alert('Please enter password between 6 to 18 character.'); 
		$('cust_password').focus();
		$('cust_password').select();
		return false; 
	}
	var confirm_password=$('confirm_password').value;
	if (confirm_password==null||confirm_password=="")
	{ 
		alert('Please enter confirm password.'); 
		$('confirm_password').focus();
		$('confirm_password').select();
		return false; 
	}

	if (confirm_password!=password)
	{ 
		alert('Confirm password and password should be same.'); 
		$('confirm_password').focus();
		$('confirm_password').select();
		return false; 
	}
	var address=$('cust_address').value;
	if (address==null||address=="")
	{ 
		alert('Please enter address.'); 
		$('cust_address').focus();
		$('cust_address').select();
		return false; 
	}
	var postcode_format=/^[0-9]*$/;
	var cust_postcode=$('cust_postcode').value;
/*	if (cust_postcode==null||cust_postcode=="")
	{ 
		alert('Please enter postcode.'); 
		$('cust_postcode').focus();
		$('cust_postcode').select();
		return false; 
	}
	*/

	if (!(postcode_format.test(cust_postcode)))
	{
		alert('Please enter only numbers in postcode.');
		$('cust_postcode').focus();
		$('cust_postcode').select();
		return false;
	}
/*	var cust_postcodelength=$('cust_postcode').value.length;
	if (cust_postcodelength<4)
	{ 
		alert('Please enter minimum 4 digit for post code.'); 
		$('cust_postcode').focus();
		$('cust_postcode').select();
		return false; 
	}
	if (cust_postcodelength>6)
	{ 
		alert('Please enter maximum 6 digit for post code.'); 
		$('cust_postcode').focus();
		$('cust_postcode').select();
		return false; 
	}
*/
	var city=$('cust_city').value;
// 	if (city==null||city=="")
// 	{ 
// 		alert('Please enter city name.'); 
// 		$('cust_city').focus();
// 		$('cust_city').select();
// 		return false; 
// 	}
	if(city!=""){
		if (!(char_format.test(city)))
		{
			alert('Please enter only character in city name.');
			$('cust_city').focus();
			$('cust_city').select();
			return false;
		}
	}
	var state=$('cust_state').value;
 	if (state==null||state==0)
 	{ 
 		alert('Please select state name.'); 
 		$('cust_state').focus();
 		$('cust_state').select();
 		return false; 
 	}
	/*if(state!=""){
		if (!(state_format.test(state)))
		{
			alert('Please enter only character in state name.');
			$('cust_state').focus();
			$('cust_state').select();
			return false;
		}
	}*/
	var country=$('cust_country').value;
	if (country==null||country=="")
	{ 
		alert('Please select country name.'); 
		$('cust_country').focus();
		$('cust_country').select();
		return false; 
	}

	var telephone1=$('cust_telephone1').value;
	if (telephone1==null||telephone1=="")
	{ 
		alert('Please enter telephone number with country code.'); 
		$('cust_telephone1').focus();
		$('cust_telephone1').select();
		return false; 
	}
	if (!(num_format.test(telephone1)))
	{
		alert('Please enter numeric value for telephone number.');
		$('cust_telephone1').focus();
		$('cust_telephone1').select();
		return false;
	}
	
// 	var telephone1length=$('cust_telephone1').value.length;
// 	if (telephone1length<4)
// 	{ 
// 		alert('Please enter minimum 3 digit for area code.'); 
// 		$('cust_telephone1').focus();
// 		$('cust_telephone1').select();
// 		return false; 
// 	}
// 	if (telephone1length>4)
// 	{ 
// 		alert('Please enter maximum 4 digit for area code.'); 
// 		$('cust_telephone1').focus();
// 		$('cust_telephone1').select();
// 		return false; 
// 	}

/*
	var telephone2=$('cust_telephone2').value;
	if (telephone2==null||telephone2=="")
	{ 
		alert('Please enter area code.'); 
		$('cust_telephone2').focus();
		$('cust_telephone2').select();
		return false; 
	}
	if (!(num_format.test(telephone2)))
	{
		alert('Please enter numeric value on telephone number.');
		$('cust_telephone2').focus();
		$('cust_telephone2').select();
		return false;
	}*/
// 	var telephone2length=$('cust_telephone2').value.length;
// 	if (telephone2length<5)
// 	{ 
// 		alert('Please enter minimum 5 digit for telephone number.'); 
// 		$('cust_telephone2').focus();
// 		$('cust_telephone2').select();
// 		return false; 
// 	}
// 	if (telephone2length>6)
// 	{ 
// 		alert('Please enter maximum 6 digit for telephone number.'); 
// 		$('cust_telephone2').focus();
// 		$('cust_telephone2').select();
// 		return false; 
// 	}

// 	var telephone3=$('cust_telephone3').value;
// 	if (telephone3==null||telephone3=="")
// 	{ 
// 		alert('Please enter phone number.'); 
// 		$('cust_telephone3').focus();
// 		$('cust_telephone3').select();
// 		return false; 
// 	}
// 	if (!(num_format.test(telephone3)))
// 	{
// 		alert('Please enter numeric value on telephone phone number.');
// 		$('cust_telephone3').focus();
// 		$('cust_telephone3').select();
// 		return false;
// 	}
// 	var telephone3length=$('cust_telephone3').value.length;
// 	
// 	if (telephone3length<4)
// 	{ 
// 		alert('Please enter minimum 4 digit for telephone number.'); 
// 		$('cust_telephone3').focus();
// 		$('cust_telephone3').select();
// 		return false; 
// 	}
// 	if (telephone1length>5)
// 	{ 
// 		alert('Please enter maximum 5 digit for telephone number.'); 
// 		$('cust_telephone3').focus();
// 		$('cust_telephone3').select();
// 		return false; 
// 	}


	var faxno1=$('cust_faxno1').value;
	
	if (faxno1!="" && !(num_format.test(faxno1)))
	{
		alert('Please enter numeric value for fax.');
		$('cust_faxno1').focus();
		$('cust_faxno1').select();
		return false;
	}
	
	/*
	if(faxno1=="")
	{
		alert('Please enter value for fax number.');
		$('cust_faxno2').focus();
		$('cust_faxno2').select();
		return false;
	}
	else if (!(num_format.test(faxno1)))
	{
		alert('Please enter numeric value for fax.');
		$('cust_faxno1').focus();
		$('cust_faxno1').select();
		return false;
	}
	*/
// 	if(faxno1!="")
// 	{
// 		var faxno1length=$('cust_faxno1').value.length;
// 		if (faxno1length<4)
// 		{ 
// 			alert('Please enter minimum 4 digit for fax area code.'); 
// 			$('cust_faxno1').focus();
// 			$('cust_faxno1').select();
// 			return false; 
// 		}
// 		if (faxno1length>4)
// 		{ 
// 			alert('Please enter maximum 4 digit for fax area code.'); 
// 			$('cust_faxno1').focus();
// 			$('cust_faxno1').select();
// 			return false; 
// 		}
// 	}
// 	var faxno2=$('cust_faxno2').value;
// 	if (!(num_format.test(faxno2)))
// 	{
// 		alert('Please enter numeric value on fax number.');
// 		$('cust_faxno2').focus();
// 		$('cust_faxno2').select();
// 		return false;
// 	}
 	
//  	if(faxno1=="" && faxno2!="")
// 	{
// 		alert('Please enter value on fax code.');
// 		$('cust_faxno1').focus();
// 		$('cust_faxno1').select();
// 		return false;
// 	}
// 	if(faxno2!="")
// 	{
// 		var faxno2length=$('cust_faxno2').value.length;
// 
// 		if (faxno2length<5)
// 		{ 
// 			alert('Please enter minimum 5 digit for fax number.'); 
// 			$('cust_faxno2').focus();
// 			$('cust_faxno2').select();
// 			return false; 
// 		}
// 		if (faxno2length>6)
// 		{ 
// 			alert('Please enter maximum 6 digit for fax number.'); 
// 			$('cust_faxno2').focus();
// 			$('cust_faxno2').select();
// 			return false; 
// 		}
// 	}
// 	var faxno3=$('cust_faxno3').value;
// 
// 	if (!(num_format.test(faxno3)))
// 	{
// 		alert('Please enter numeric value on fax area code.');
// 		$('cust_faxno3').focus();
// 		$('cust_faxno3').select();
// 		return false;
// 	}
// 	if(faxno3!="")
// 	{
// 		var faxno3length=$('cust_faxno3').value.length;
// 
// 		if (faxno3length<4)
// 		{ 
// 			alert('Please enter minimum 4 digit for fax number.'); 
// 			$('cust_faxno3').focus();
// 			$('cust_faxno3').select();
// 			return false; 
// 		}
// 		if (faxno3length>5)
// 		{ 
// 			alert('Please enter maximum 5 digit for fax number.'); 
// 			$('cust_faxno3').focus();
// 			$('cust_faxno3').select();
// 			return false; 
// 		}
// 	}


	var valid_extensions = /(.jpg|.jpeg|.gif|.bmp)$/i;
	var img=document.getElementById('picture');
// 	if (!(valid_extensions.test(img.value)) && (fld.value!=''))
	if(img.value!="")
	{
		if (!(valid_extensions.test(img.value)))
		{
			alert("Please upload only images:  .jpg or .jpeg or .gif or .bmp \nPlease select a new file to upload.");
			return false;
		}
	}


	var captcha_input=$('recaptcha_response_field').value;
	if(captcha_input==null||captcha_input=="")
	{ 
		alert('Please enter security code.'); 
		$('recaptcha_response_field').focus();
		$('recaptcha_response_field').select();
		return false; 
	}

// 	var captcha_input=$('captcha-input').value;
// 	if(captcha_input==null||captcha_input=="")
// 	{ 
// 		alert('Please enter varification code.'); 
// 		$('captcha-input').focus();
// 		$('captcha-input').select();
// 		return false; 
// 	}
	if(document.getElementById('terms').value)
        {
	if(document.getElementById('terms').checked==false){ 
		alert('Please check on Terms & Conditions.'); 
		return false;
	}
	else
	{
		return true;
	}
        }
}

var numDays = {jan: 31, mar: 31, apr: 30, may: 31, jun: 30,
jul: 31, aug: 31, sep: 30, oct: 31, nov: 30, dec: 31};

function checkLeap(nYear) {
  return (nYear % 4 == 0) ? 29 : 28;
}

function isDefault(oSelect) {
  return oSelect[oSelect.selectedIndex].defaultSelected;
}

function setDays(oMonthSel, oDaySel, oYearSel) {
   if (isDefault(oMonthSel) && isDefault(oDaySel)) return false;
   var opt, oForm = oMonthSel.form;
   var nDays = numDays[oMonthSel[oMonthSel.selectedIndex].value];
   if (nDays == null && !isDefault(oYearSel))
   nDays = checkLeap(oYearSel[oYearSel.selectedIndex].value);
   var oDaySelLgth = oDaySel.length - 1;
   if (nDays != oDaySelLgth) {
     if (nDays<oDaySelLgth)
       oDaySel.length = nDays + 1;
     else {
       for (var i=1; i<nDays-(oDaySelLgth-1); i++) {
       opt = new Option(oDaySelLgth + i, oDaySelLgth + i);
       oDaySel.options[oDaySel.length] = opt;
       }
     }
   }
}

function checkdate(oForm) {
   var oMonthSel = oForm.month, oDaySel = oForm.day, oYearSel = oForm.year;
   var aStr = msg = '';
   if (isDefault(oMonthSel))
   msg += 'MONTH\n';
   if (isDefault(oDaySel))
   msg += 'DAY\n';
   if (isDefault(oYearSel))
   msg += 'YEAR\n';
   if (msg != '' && !msg.match(/^MONTH\nDAY\nYEAR\n$/)) {
      aStr = '\nPlease choose from the following:\n -- ';
      aStr += msg + ' -- to complete the date selection.\nThanks !\n';
      alert(aStr);
      oMonthSel.focus();
      return false;
   }
   return true;
}


/*------------------------ End of customer validation ----------------------------------*/


//To check user email on registration page.
function checkUserMailExist(obj)
{
	var email = obj.value;
	var emailFlag=$('emailFlag').value;
	var url = host+'user/emailcheck/email_ch/'+email;
	if(email!="") {
		var email_format=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if (!(email_format.test(email)))
		{
			var msg="<span class='mendatory_one'>Please enter valid email address.</span>";
// 			new Ajax.Updater('availablemsg',msg);
			document.getElementById('availablemsg').innerHTML=msg;
			$('status_value').value='N';
			
		} else {
			new Ajax.Updater('availablemsg',url,{asynchronous:true, evalscripts:true, encoding: 'UTF-8', onComplete: function(request){
	
					var result=request.responseText;
						result=result.trim();
						
						/*
					if(result=="<span class='available_msg'>This email address is available.</span>")
					{ 
						//$('status_value').value='Y';
						document.getElementById('status_value').value="Y";
					}
					else
					{
						//$('status_value').value='N';
						document.getElementById('status_value').value="N";
					}
					*/
				}
			});
		}
	}
}

function floorcategory()
	{
		var floorcate=document.getElementById('website_category').value;
		document.getElementById('web').value=floorcate;
		return floorcate;
	}
	
function checkPassword()
{
	var oldpassword=document.getElementById('oldpassword').value;
	var newpassword=document.getElementById('newpassword').value;
	var retypepassword=document.getElementById('retypepassword').value;
	if($('oldpassword').value==null||$('oldpassword').value==""){
		alert("Old Password must be filled out!");
		$('oldpassword').focus();
		$('oldpassword').select();
		return false;
	}
	if($('newpassword').value==null||$('newpassword').value==""){
		alert("New Password must be filled out!");
		$('newpassword').focus();
		$('newpassword').select();
		return false;
	}
	if($('retypepassword').value==null||$('retypepassword').value==""){
		alert("Retype Password must be filled out!");
		$('retypepassword').focus();
		$('retypepassword').select();
		return false;
	}
	if($('newpassword').value!=$('retypepassword').value){
		alert("Retype password & New password not matched!");
		$('retypepassword').focus();
		$('retypepassword').select();
		return false;
	}
	else
	{
		return true;
	}

}

/*function changeTerms()
	{
		var terms=$('terms').value;
		var term1=document.getElementById('terms').checked;
		alert(term1);
		return false;
		if (term1==true)
		{ 
			$('terms').value=1;
		}
		else
		{ 
			$('terms').value=0;
		}
	}
*/

