  function ltrim(string)
  {
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=0,j=0;i<string.length;i++)
    {
      if(j==0)
      {
        if(string.charAt(i)!=" ")
        {
            string1[j++]=string.charAt(i);
        }
      }
      else
      {
        string1[j++]=string.charAt(i);
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    } 
    return string;
  }

function rtrim(string)
  {
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=string.length;i>=0;i--)
    {
      if(string.charAt(i-1)==" ")
      {
        continue;
      }
      else
      {
        for(j=0;j<i;j++)
        {
          string1[j]=string.charAt(j);
        }
        break;
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    }
    
    
    return string;
  }
  
  function trim(string)
  {
    string=ltrim(string); // // This function is used to trim the left side of a String
    string=rtrim(string);// This function is used to trim the right side of a String
    return string;
  }

////////////////////////////////////////////////////////////////////////////////////////////////////////////

function submitlogin()
{
      
	 if(trim(document.form1.email.value).length<1)
    {
      alert("Please enter username");
      document.form1.email.focus();
	  return;
    }
	else if(trim(document.form1.pwd.value).length<1)
	{
	  alert("Please enter password");
      document.form1.pwd.focus();
      return;
	}
	else
	{
      document.form1.submit();
	}
}

function submitsearch()
{
	  
	if(trim(document.frmsearch.prodname.value).length<1)
    {
      alert("Please enter product code");
      document.frmsearch.prodname.focus();
	  return;
	}
	else
	{
      document.frmsearch.submit();
	}
}

function ac(ina){
var tempstr=eval("document.form"+ina+".qty.value")
window.location.href='ac.asp?rec_id='+ina+'&qty='+tempstr;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function addchange(){
	if(document.form1.addOp.checked){
		document.form1.shipadd.value=document.form1.billadd.value;
	}else{
		document.form1.shipadd.value='';	
	}
}
function cns(){
var go=true;
var gof=true;
	if(document.form1.email.value=='' || document.form1.email.value==' '){
		alert('Please provide valid email address');
		gof=false;
		go=false;
		document.form1.email.focus();
	}
	if(gof && (document.form1.email.value).indexOf("@")<0){
		alert('Please provide valid email address');
		gof=false;
		go=false;		
	}
	if(gof && (document.form1.email.value).indexOf(".")<0){
		alert('Please provide valid email address');
		gof=false;
		go=false;		
	}	
	if(gof && (document.form1.pwd.value=='' || document.form1.pwd.value==' ')){
		alert('Please provide valid password');
		gof=false;
		go=false;
		document.form1.pwd.focus();
		
	}	
	if(gof && (document.form1.bizzname.value=='' || document.form1.bizzname.value==' ')){
		alert('Please provide valid business name');
		gof=false;
		go=false;
		document.form1.bizzname.focus();
	}		
	if(gof && (document.form1.billadd.value=='' || document.form1.billadd.value==' ')){
		alert('Please provide valid value for billing address');
		gof=false;
		go=false;
		document.form1.billadd.focus();
	}		
	if(gof && (document.form1.shipadd.value=='' || document.form1.shipadd.value==' ')){
		alert('Please provide valid value for shipping address');
		gof=false;
		go=false;
		document.form1.shipadd.focus();
	}			
	if(gof && (document.form1.postcode.value=='' || document.form1.postcode.value==' ')){
		alert('Please provide valid value for postcode');
		gof=false;
		go=false;
		document.form1.postcode.focus();
	}			
	if(gof && (document.form1.contactphone.value=='' || document.form1.contactphone.value==' ')){
		alert('Please provide valid value for contact phone number');
		gof=false;
		go=false;
		document.form1.contactphone.focus();
	}			
	/*if(gof && (document.form1.contactfax.value=='' || document.form1.contactfax.value==' ')){
		alert('Please provide valid value for contact fax');
		gof=false;
		go=false;
	}	*/				
	
	if(document.form1.nobothers.value=='')
	{
		 document.form1.nobothers2.value="NA";
	}
	else
	{
		document.form1.nobothers2.value=document.form1.nobothers.value;
	}
	if(gof && go){
		document.form1.submit();
	}

}

function cnsupdate(){
var go=true;
var gof=true;
	/*if(document.form1.email.value=='' || document.form1.email.value==' '){
		alert('Please provide valid email address');
		gof=false;
		go=false;
	}
	if(gof && (document.form1.email.value).indexOf("@")<0){
		alert('Please provide valid email address');
		gof=false;
		go=false;		
	}
	if(gof && (document.form1.email.value).indexOf(".")<0){
		alert('Please provide valid email address');
		gof=false;
		go=false;		
	}	*/
	if(gof && (document.form1.pwd.value=='' || document.form1.pwd.value==' ')){
		alert('Please provide valid password');
		gof=false;
		go=false;
	}	
	if(gof && (document.form1.bizzname.value=='' || document.form1.bizzname.value==' ')){
		alert('Please provide valid business name');
		gof=false;
		go=false;
	}		
	if(gof && (document.form1.billadd.value=='' || document.form1.billadd.value==' ')){
		alert('Please provide valid value for billing address');
		gof=false;
		go=false;
	}		
	if(gof && (document.form1.shipadd.value=='' || document.form1.shipadd.value==' ')){
		alert('Please provide valid value for shipping address');
		gof=false;
		go=false;
	}			
	if(gof && (document.form1.postcode.value=='' || document.form1.postcode.value==' ')){
		alert('Please provide valid value for postcode');
		gof=false;
		go=false;
	}			
	if(gof && (document.form1.contactphone.value=='' || document.form1.contactphone.value==' ')){
		alert('Please provide valid value for contact phone number');
		gof=false;
		go=false;
	}			
	if(gof && (document.form1.contactfax.value=='' || document.form1.contactfax.value==' ')){
		alert('Please provide valid value for contact fax');
		gof=false;
		go=false;
	}					
	
	if(gof && go){
		document.form1.submit();
	}

}
//////////////////////////////////////////////////////////////

function goback()
{
	window.history.back();
}

///////////////////////////////////////////////////////////////////////////////////////////////

function validateZIP(field)
{
	var valid = "0123456789-";
	var hyphencount = 0;
	if (field.length!=5 && field.length!=10)
	{
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	for (var i=0; i < field.length; i++)
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1")
		{
			alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-"))
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
  s=stripCharsInBag(strPhone,validWorldPhoneChars);
  return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function checkbox_checker()
{
  var i=0;
  for(counter = 0; counter < document.frmfacilityb.chk.length; counter++)
  {

   if (document.frmfacilityb.chk[counter].checked)
   {
	 if(trim(document.frmfacilityb.pro[counter].value).length<1)
	   {
		 i=1;
	   }
	   else
	   {
		  document.frmfacilityb.insidpro[counter].value=document.frmfacilityb.chk[counter].value+"-"+document.frmfacilityb.pro[counter].value;
		 i=2;		
	   }
   }
   
  }
  if(i==2) return true;
  else return false;

}

function checkbox_checker2()
{
  var i=0;
  for(counter = 0; counter < document.frmupdatefacilityb.chk.length; counter++)
  {

   if (document.frmupdatefacilityb.chk[counter].checked)
   {
	 if(trim(document.frmupdatefacilityb.pro[counter].value).length<1)
	   {
		 i=1;
	   }
	   else
	   {
		  document.frmupdatefacilityb.insidpro[counter].value=document.frmupdatefacilityb.chk[counter].value+"-"+document.frmupdatefacilityb.pro[counter].value;
		 i=2;		
	   }
   }
   
  }
  if(i==2) return true;
  else return false;

}




function charCounter(field, maxlimit)
{
  if (field.value.length > maxlimit)
  {
    field.value = field.value.substring(0, maxlimit);
	alert("Maximum "+maxlimit+" character allowed");
    field.focus();
  }
}

function validphone(val)
{
	var objRegExp  =/(^\d{3}-\d{3}-\d{4}$)/;
	if(!objRegExp.test(trim(val.value)))
	{
		alert("Check phone format");
		val.focus();
	}
}

function validtax(val)
{
	var objRegExp  =/(^\d{2}-\d{7}$)/;
	if(!objRegExp.test(trim(val.value)))
	{
		alert("Check tax format");
		val.focus();
	}
}


/////////////////////////////////////
function showimage(val)
{
	 window.open(val,"_new","width=310,height=370");
}

////========cart functions===============////

function updatecart()
{
  var itemcount=document.frmcart.totalitem.value;
  var sum=0;
  for(j=0;j<itemcount;j++)
  {
      var qty=eval("document.frmcart.pqty"+j+".value");
	  if(isNaN(qty)==true)
	  {
		  alert("Please enter a valid quantity");
		  return;
	  }
  }
  for(i=0;i<itemcount;i++)
	{
	  var price=eval("document.frmcart.phprice"+i+".value");
	  var qty=eval("document.frmcart.pqty"+i+".value");
	  var netprice=parseFloat(price)*parseInt(qty);
	  var p=eval("document.frmcart.pprice"+i);
	  p.value=CurrencyFormatted(netprice);
	  sum=sum+netprice;
	}

	document.frmcart.totalprice.value=CurrencyFormatted(sum);
}


function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
	
}
///////////////////////////////////////////////


function scale_load(conta,pimg)

{
var box = document.getElementById(conta);
var img = document.getElementById(pimg);

// Grab the image's dimensions
var imgH = img.clientHeight;
var imgW = img.clientWidth;

// Find which dimension is scaled the most
var scaleH = box.clientHeight / img.clientHeight;
var scaleW = box.clientWidth / img.clientWidth;

// Scale the image
if (scaleH < scaleW) {
	img.style.height = box.clientHeight + "px";
	img.style.width = Math.round(imgW * scaleH) + "px";
} else {
	img.style.width = box.clientWidth + "px";
	img.style.height = Math.round(imgH * scaleW) + "px";
}

}