//-----------------start numbersOnly  function validations --------------bindu on 08-17-2009//
function validNumbers(text)
{
	if(text.value.length==0)
		return;
if(text.value==0)
{
		alert('Invalid Number');
		text.value='1';
		return false;
}    		
	var regexp=/^[0-9]*$/;
	if(text.value.search(regexp)==-1)
		{
			text.value = text.value.substring(0,(text.value.length-1));
			alert('Numbers Only');
			if(text.value.search(regexp)==-1)
			text.value="";
		}	
}
//-----------------end numbersOnly  function validations -----------------------------------------//


//-----------------start numbers with space Only  function validations --------------bindu on 08-17-2009//
function validNumberswithspace(text)
{
	if(text.value.length==0)
		return;
if(text.value==0)
{
		alert('Invalid Number');
		text.value='1';
		return false;
}    		
	var regexp=/^[0-9 -]*$/;
	if(text.value.search(regexp)==-1)
		{
			text.value = text.value.substring(0,(text.value.length-1));
			alert('Numbers Only');
			if(text.value.search(regexp)==-1)
			text.value="";
		}	
}
//-----------------end numbers with space Only  function validations -----------------------------------------//


//-----------------start check space validations -------------------bindu on 08-17-2009//
function checkspace(text)
{
  var str=text.value;
  var first=str.substring(0,1);
  var second=str.substring(0,1);
  var val='false';
  if(first==' ')
  {
		val='true';
		if(val=='true')
		{
		   if(second==' ')
		   {
			 val='true';
			 //alert('Please Enter Valid Information');
			 text.value = "";
		   }
		}
  }
		 
}
//-----------------end check space validations ----------------------------------------//
//-- start required function validations --//
function validRequiredField(text,msg,val)
{
	if(val!="")
	{
		if(document.getElementById(text).value==val)
		{
			alert(msg);
			if(text!="hdnselaccrediation")
				document.getElementById(text).focus();
			return false;
		} 
	}
    if(Trim(document.getElementById(text).value)=="")
    {
        alert(msg);
		if(text!="hdnselaccrediation")
			document.getElementById(text).focus();
        return false;
    } 
    else
        return true;
    
}
function LTrim(str) 
{
	for (var i=0; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i++);
	return str.substring(i,str.length);
}
function RTrim(str) 
{
	for (var i=str.length-1; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i--);
	return str.substring(0,i+1);
}
function Trim(str) 
{
	return LTrim(RTrim(str));
}
//-- validations for create producer ends here --//
function chkcontact()
{
     if(
		validRequiredField('txtsname','Please Enter Name','Your Name')&&
		validMinCount('txtsname',3,'Please Enter Minimum 3 Characters For Name')&&
		validRequiredField('txtsemail','Please enter Email','Your email address')&&
		EmailValidation('txtsemail')&&
		validRequiredField('txtsphone','Please enter Phone','Your phone number')&&
		validMinCount('txtsphone','10','Please enter Minimum 10 characters for Phone')&&
		validRequiredField('txtsdname','Please enter Message','And how can we help you')
	  )
	{
        document.frmcontact.action="functions/insert.php";
		//document.frmcontact.submit();
	}
		
	else
	{
		return false;
	}
}

function chkstockist()
{
     if(
		validRequiredField('txtsname','Please Enter Contact Name','Contact Name')&&
		validMinCount('txtsname',3,'Please Enter Minimum 3 Characters For Contatct Name')&&
		validRequiredField('txtretailer','Please Enter Retailer','Retailer')&&
		validMinCount('txtretailer',3,'Please Enter Minimum 3 Characters For Retailer')&&
		validRequiredField('txtaddress','Please Enter Address','Address')&&
		validMinCount('txtaddress',3,'Please Enter Minimum 3 Characters For Address')&&		
		validRequiredField('txtsphone','Please enter Phone','Phone Number')&&
		validMinCount('txtsphone','10','Please enter Minimum 10 characters for Phone Number')&&
		validRequiredField('txtsemail','Please enter Email','Email Address')&&
		EmailValidation('txtsemail')&&
		validRequiredField('txtsdname','Please enter Message','And how can we help you')
	  )
	{
        document.frmstockist.action="functions/insert.php";
		//document.frmcontact.submit();
	}
		
	else
	{
		return false;
	}
}

function showparsubcategory(str)
{	
	var val=document.getElementById(str).value;
	self.location="products.php?catid="+val;
}

function showcolor(str1,str2,str3,str4,str5)
{	
	var val1=document.getElementById(str1).value;
	var val2=document.getElementById(str2).value;
	var val3=document.getElementById(str3).value;
	var val4=document.getElementById(str4).value;
	var val5=document.getElementById(str5).value;
	self.location="product_details.php?catid="+val2+"&scid="+val3+"&pid="+val4+"&cid="+val1+"&sid="+val5;
}

function showdivdesc()
{
	document.getElementById('div_spec').style.display="none";
	document.getElementById('div_descr').style.display="block";
	document.getElementById('divdescrclass').className="descrr";
	document.getElementById('divspecclass').className="specc";
}

function showdivspec()
{
	document.getElementById('div_spec').style.display="block";
	document.getElementById('div_descr').style.display="none";
	document.getElementById('divspecclass').className="spec";
	document.getElementById('divdescrclass').className="descr";
}

function chkqty(pcid)
{
	var qty=document.getElementById('qty'+pcid).value;
	var stock=document.getElementById('stock'+pcid).value;

	var stk=stock-qty;
	if((stk<=0)&&(qty>stock))
	{
	alert('Stock now availabale: '+stock+' only');
	}
	//else
	//{
		document.form1.submit();
	//}
}

function showdivforgopwd()
{
	document.getElementById('div_forgotpassword').style.display="block";
	document.getElementById('div_login').style.display="none";
}

function hidedivforgopwd()
{
	document.getElementById('div_forgotpassword').style.display="none";
	document.getElementById('div_login').style.display="block";
}


/*change from focus to normal starts here------------bindu on08-05-2009*/ 
function changefocusfields(txt,txtvalue)
{
	if(txt.value==txtvalue)
	{
		txt.value="";
		//txt.style.backgroundColor ='#FFFFFF';
	}
}
/*change from focus to normal ends here*/ 

/*change to focus from normal starts here------------bindu on08-05-2009*/ 
function changeonblurfields(txt,val)
{
	if(txt.value=="")
	{
		txt.value=val;
		//txt.style.backgroundColor ='#EAEAEA';
	}
}
/*change to focus from normal ends here*/ 

function chkemailnews(ctrl,strcheck)
{
	if((document.getElementById(ctrl).value==strcheck) || (document.getElementById(ctrl).value==""))
	{
		alert("Please Enter Email");
		document.getElementById(ctrl).focus();
		return false;
	}
	else
	{
        regexp=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if(document.getElementById(ctrl).value.search(regexp)==-1)
		{
			document.getElementById(ctrl).value="";
			alert('Please Enter Valid Email');
			document.getElementById(ctrl).focus();
			return false;
		}
		else if(document.getElementById(ctrl).value==document.getElementById('hdn_adminemail').value)
		{
			alert("Please enter Valid email");
			document.getElementById(ctrl).value="";
			document.getElementById(ctrl).focus();
			return false;
		}
		else
		{
			SimpleModal.open('newsletter.php?email='+document.getElementById(ctrl).value, 480, 500);
			document.getElementById(ctrl).value=strcheck;
			return true;
		}
	}

}
//----------------------------------News Letter Form----------------------------------------
function validatenewsletterpage()
{
    if(
		RequiredField('pfname','Please Enter First Name')&&
		validMinCount('pfname',3,'Please Enter Minimum 3 Characters For First Name')&&
		/*RequiredField('plname','Please Enter Last Name')&&
		validMinCount('plname',3,'Please Enter Minimum 3 Characters For Last Name')&&*/
		RequiredField('txtcountry','Please Enter Country')						
	  )
	{
        //document.form.action="functions/update.php";
		document.frm1.submit();
	}
		
	else
	{
		return false;
	}
}

function skipnewsletterinfo(email)
{
	if(
		RequiredField('pfname','Please Enter First Name')&&
		validMinCount('pfname',3,'Please Enter Minimum 3 Characters For First Name')&&
		/*RequiredField('plname','Please Enter Last Name')&&
		validMinCount('plname',3,'Please Enter Minimum 3 Characters For Last Name')&&*/
		RequiredField('txtcountry','Please Enter Country')						
	  )
	{
		//document.form.target = '_self';
		//document.form.action="newsletter.php?type=skip&emailid="+email;
		document.frmStep1.submit();
	}
}

function chkemailfp()
{
	if(document.getElementById('forgot_email').value=="")
	{
		alert("Please Enter Email");
		document.getElementById('forgot_email').focus();
		document.getElementById('forgot_email').value="";
		return false;
	}
	if(document.getElementById('forgot_email').value!="")
	{ 
		regexp=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if(document.getElementById('forgot_email').value.search(regexp)==-1)
		{
			document.getElementById('forgot_email').value="";
			alert('Please Enter Valid Email');
			document.getElementById('forgot_email').focus();
			return false;
		}
	 }
	document.frm_login.submit();
}

function RequiredField(text,msg)
{
    if((document.getElementById(text).value)== "" )
    {
        alert(msg);
        document.getElementById(text).focus();
        return false;
    } 
    else
    {
      return true;
    }   
}

function chkRadioField()
{
	var shippricename="";
	coutshiprows=document.getElementById('hdn_shipnumrows').value;
	var x=0;
	for(i=1;i<=coutshiprows;i++)
	{
		shippricename="rd_shippriceid"+i;
			if(document.getElementById(shippricename).checked==true)
			{
				x=x+1;
			} 				
	}
	if(x==0)
	{
		alert("Please Select Shipping");
		return false;
	}
	else
	{
		return true;
	}
}

function EmailValidation(txt)
{
    if(((document.getElementById(txt).value)!="")&&((document.getElementById(txt).value)!=""))
     {
            regexp=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
            if(document.getElementById(txt).value.search(regexp)==-1)
            {
                  document.getElementById(txt).value="";
                  alert('Invalid Email, Please Re-Enter');
                  document.getElementById(txt).focus();
                  return false;
            }
            else
	            return true;
	 }
	 else
	    return true;
}

function chkdiv(text,msg)
{
    if((Trim(document.getElementById(text).innerHTML))!= "" )
    {
        alert(msg);
        return false;
    } 
    else
    {
      return true;
    }   
}


function chklogin()
{
    if(RequiredField('login_email','Please Enter Email Address') && RequiredField('login_password','Please Enter Password'))
	{
		document.frm_chkout_login.action="chk.php";
		document.frm_chkout_login.submit();
	}
    else
    {
        return false;
    }    
}

function chkregister(paymentMethod)
{
	document.getElementById('hdnPaymentType').value = paymentMethod;
		
 if(
		 RequiredField('reg_surname','Please Enter Surname') &&
		 RequiredField('reg_name','Please Enter Firstname') &&
		 RequiredField('reg_address','Please Enter Address1') &&
		 RequiredField('reg_emailAddress','Please Enter Email Address') &&
		 EmailValidation('reg_emailAddress') &&
		 RequiredField('reg_city','Please Enter City') &&
		 RequiredField('reg_zip','Please Enter Postcode') &&
		 //RequiredField('reg_county','Please Enter County') &&
		 //RequiredField('reg_phone','Please Enter Zip')&&
		 chkdiv('div_showemailexists','Email Address existed already, Please try with another Email Address')&&
		 chkRadioField()
	) 
	{
		return true;
	}
    else
    {
     return false;
    }    
}

function chkcouponcode()
{
	if(document.getElementById('txt_couponcode').value=="")
	{
		alert("Please enter Coupon or Voucher code");
		return false;
	}
	else
		document.frm_couponcode.submit();
}

function chkshipping(shipprice,shipName)
{
	gtotincvat=document.getElementById('gtotincvat').value;
	gtot=parseFloat(shipprice)+parseFloat(gtotincvat);
	
	hdntax=document.getElementById('hdn_tax').value;
	taxval=gtot*(hdntax/100);
	document.getElementById('div_totoamt').innerHTML="<b>Total inc VAT</b><br/>&pound;"+parseFloat(gtot).toFixed(2)+"  <br/>VAT at "+hdntax+"%";

	document.getElementById('div_showgtot').innerHTML="<b>Total inc VAT<br/>&pound; "+parseFloat(gtot).toFixed(2)+"</b>";
	document.getElementById('hdn_grandtotalamt').value=parseFloat(gtot).toFixed(2);
	document.getElementById('hdn_shipprice').value=shipprice;
	document.getElementById('div_tax').innerHTML="<b>Total ex VAT</b><br/>&pound; "+parseFloat(gtot-taxval).toFixed(2);
	//document.getElementById('td_showvatmsg').innerHTML='<span style="font-size:12pt;">VAT will be calculated on the Grand Total which includes <strong>'+parseFloat(taxval).toFixed(2)+'</strong> at <strong>'+hdntax+'%</strong></span>';
	document.getElementById('chkout_showvatmsg').innerHTML='<span style="font-size:10pt;">(this includes <strong>'+parseFloat(taxval).toFixed(2)+'</strong> VAT at <strong>'+hdntax+'%</strong>)</span>';
	document.getElementById('hdn_vatvalue').value=parseFloat(taxval).toFixed(2);
	document.getElementById('hdnShippingMethod').value = shipName;
}
function chkloginindex()
{
    if(RequiredField('login_email','Please Enter Email Address') && RequiredField('login_password','Please Enter Password'))
	{
		document.frm_login.action="chk.php";
		document.frm_login.submit();
	}
    else
    {
        return false;
    }    
}
function updateprofile()
{
 if(
 RequiredField('txt_fname','Please Enter Name') &&
 RequiredField('txt_address','Please Enter Address') &&
 RequiredField('txt_city','Please Enter Town') &&
 RequiredField('reg_county','Please Enter County') &&
 RequiredField('reg_zip','Please Enter Postcode')&&
 RequiredField('txt_phone','Please Enter Phone')) 
 {
	return true;
 }
    else
    {
     return false;
    }    
}
function validchangepwd()
{
	if(
		RequiredField('txtoldpwd','Please enter Old Password')&&
		RequiredField('txtnewpwd','Please enter New Password')&&		
		validMinCount('txtnewpwd',5,'New Password Should be Minimum of 5 Characters')&&
		RequiredField('txtcnfpwd','Please enter Confirm Password')&&
		validMinCount('txtcnfpwd',5,'Confirm Password Should be Minimum of 5 Characters')&&
		changePwd('txtnewpwd','txtcnfpwd')
		)
	{
		return true;
	}	
	else
		return false;
}
function validMinCount(text,long,msg) 
{
  if(document.getElementById(text).value!="")
	{
		var maxlength = new Number(long); // Change number to your max length.
		if (document.getElementById(text).value.length < maxlength)
		{		
			alert(msg);		
			document.getElementById(text).focus();
			return false;
		}		
		else
			return true;
	}
	else
			return true;
    
}
//-----------------end mincount function validations -------------------------------------------//

//-----------------start count function validations -------------------Narayana on 31-08-2009//
function validCount(text,len)
{
    if(text.value.length>len)
    {
        alert(len+' Characters only');
        text.value=text.value.substring(0,len);
    }
}

/*-------------------start validating admin change password function------------------------------*/
/*-------------------Narayana------------------------------*/
function changePwd(newpwd,confirmpwd)
{
	//alert('enter');
    var	newpassword=document.getElementById(newpwd).value;
	var confirmpassword=document.getElementById(confirmpwd).value;
	
	if((newpassword)!=(confirmpassword))
	{
    alert('New Password and Confirm Password Should be Same');
	document.getElementById(newpwd).value="";
	document.getElementById(confirmpwd).value="";
	document.getElementById(newpwd).focus();	
	return false;
	}
	else
		return true;

}

function chkrdbtn()
{
	if(document.getElementById('rd_paymentpaypal').checked)
	{
		document.getElementById('chkradiobtn').value="1";
	}
	if(document.getElementById('rd_paymentsagepay').checked)
	{
		document.getElementById('chkradiobtn').value="2";
	}
}

function chknewsletter()
{
	if(document.getElementById('chk_newsletter').checked)
	{
		document.getElementById('chknewslettervalue').value=1;
	}
	else
	{
		document.getElementById('chknewslettervalue').value=0;
	}
}

function chkcatsearch(str)
{
	self.location="products.php?catid="+str;
}

/* Start Login Page Validations*/

function chkloginpage()
{
    if(RequiredField('login_email','Please Enter Email address') && RequiredField('login_password','Please Enter Password'))
	{
		document.frm_login.action="chk.php";
		document.frm_login.submit();
	}
    else
    {
        return false;
    }    
}
/*End Login Page Validations*/


/*----Start Change Password Validations------------*/
function chkpassword()
{
	if(document.getElementById("txt_oldpass").value=="")
	{
		alert("Please Enter Old Password");
		document.getElementById("txt_oldpass").value="";
		document.getElementById("txt_oldpass").focus();
		return false;
	}

	if(document.getElementById("txt_newpass").value=="")
	{
		alert("Please Enter New Password");
		document.getElementById("txt_newpass").value="";
		document.getElementById("txt_newpass").focus();
		return false;
	}

	if(document.getElementById('txt_newpass').value.length<5)
	{
		alert("New Password should be minimum of 5 characters");
		document.getElementById('txt_newpass').value="";
		document.getElementById('txt_newpass').focus();
		return false;
	}

	if(document.getElementById('txt_confirmpass').value=="")
	{
		alert("Please enter Confirm New Password");
		document.getElementById('txt_confirmpass').value="";
		document.getElementById('txt_confirmpass').focus();
		return false;
	}

	if(document.getElementById('txt_confirmpass').value.length<5)
	{
		alert("Confirm New Password should be minimum of 5 characters");
		document.getElementById('txt_confirmpass').value="";
		document.getElementById('txt_confirmpass').focus();
		return false;
	}

	if(document.getElementById('txt_confirmpass').value!=document.getElementById('txt_newpass').value)
	{
		alert("New Password and Confirm New Password not matched");
		document.getElementById('txt_newpass').value="";
		document.getElementById('txt_confirmpass').value="";
		document.getElementById('txt_newpass').focus();
		return false;
	}
}

/*--------End Change PAssword Validations-----------*/

/*----------start Forgot Password validations-----------------*/

function chkforgotpwd()
{
	if(document.getElementById('txt_email').value=="")
	{
		alert("Please enter Email");
		document.getElementById('txt_email').value="";
		document.getElementById('txt_email').focus();
		return false;
	}
	if(document.getElementById('txt_email').value!="")
	{
		regexp=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if(document.getElementById('txt_email').value.search(regexp)==-1)
		{
			document.getElementById('txt_email').value="";
			alert('Please Enter Valid Email');
			document.getElementById('txt_email').focus();
			return false;
		}
	}
	return true;
}

function ChangeShippingDiv(val)
{
	if(val=="GB")
	{
		document.getElementById('divGB').style.display='block';
		document.getElementById('divOGB').style.display='none';
	}
	else
	{
		document.getElementById('divGB').style.display='none';
		document.getElementById('divOGB').style.display='block';
	}
	for(i=1;i<=document.getElementById('hdn_shipnumrows').value;i++)
	{
		if(document.getElementById('hdn_shipnumrows')!=null)
			document.getElementById('rd_shippriceid'+i).checked=false;
	}
	chkshipping(0,'');
}
/*----------End Forgot Password validations-----------------*/
