function colortable()
	{	
	i = document.getElementsByTagName("tr");
	var cv = 0;
	for (j=1;j<=i.length-1;j++)
		{
//		if ((i[j].style.visibility != "hidden")&&!(i[j].id.match("Subclients")))
		if ((i[j].style.visibility != "hidden"))
			{
			i[j].className = "tr"+cv%2;
			cv++;
			}
		}
	}


var browserType;

if (document.layers) {browserType = "nn4"}
if (navigator.appCodeName=="Mozilla") {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
   browserType= "gecko"
}

function hide(div_id) {
  if (browserType == "gecko" )
     document.poppedLayer =
         eval('document.getElementById(div_id)');
  else if (browserType == "ie")
     document.poppedLayer =
        eval('document.getElementById(div_id)');
  else
     document.poppedLayer =
        eval('document.layers[div_id]');
  document.poppedLayer.style.display = "none";
}

function show(div_id) {
  if (browserType == "gecko" )
     document.poppedLayer =
         eval('document.getElementById(div_id)');
  else if (browserType == "ie")
     document.poppedLayer =
        eval('document.getElementById(div_id)');
  else
     document.poppedLayer =
         eval('document.layers[div_id]');
  document.poppedLayer.style.display = "table";
}


function shagentselection(type)
{
  div_id = "agentselection";
  
  if (browserType == "gecko" )
     document.poppedLayer =
         eval('document.getElementById(div_id)');
  else if (browserType == "ie")
     document.poppedLayer =
        eval('document.getElementById(div_id)');
  else
     document.poppedLayer =
        eval('document.layers[div_id]');

  if (type==4) {document.poppedLayer.style.visibility = 'visible';document.poppedLayer.style.display = 'inline'}
  else {document.poppedLayer.style.visibility = 'hidden';document.poppedLayer.style.display = 'none'} 

  div_id = "companyselection";
  
  if (browserType == "gecko" )
     document.poppedLayer =
         eval('document.getElementById(div_id)');
  else if (browserType == "ie")
     document.poppedLayer =
        eval('document.getElementById(div_id)');
  else
     document.poppedLayer =
        eval('document.layers[div_id]');

/*  if (type==1||type==2||type==4) {document.poppedLayer.style.visibility = 'visible';document.poppedLayer.style.display = 'inline'}
  else {document.poppedLayer.style.visibility = 'hidden';document.poppedLayer.style.display = 'none'} */

}



function fieldvalidation(field)
	{
	if (field.value.length==0)
		{
		field.style.background = "#FCFFC6";
		return false;
		}
		else
		{
		field.style.background = "#ffffff"; 
		return true;
		}
	}
	
	
function formvalidate(scrollY)
	{
	document.getElementById('errors').innerHTML = "";
	var a = true;
	
	typevalidate();

	if (!typevalidate())
		{
		document.getElementById('errors').innerHTML += "<br>Select Type";
		a = false;
		}
	else if (($("input[name=c2s_type]:radio:checked").val()==4)&&(!fieldvalidation(document.form1.c2s_parentid)))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Agent number" : "Enter Agent number";
		a = false;
		}
	else if (($("input[name=c2s_type]:radio:checked").val()==5)&&(!fieldvalidation(document.form1.c2s_parentid)))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Charity name or number" : "Enter Charity name or number";
		a = false;
		}
		
	if (!fieldvalidation(document.form1.c2s_contactname))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Contact name" : "Enter Contact name";
		a = false;
		}
	if (!fieldvalidation(document.form1.c2s_companyname))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Company" : "Enter Company";
		a = false;
		}
	if (!fieldvalidation(document.form1.c2s_billingaddress1))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Billing address line 1" : "Enter Billing address line 1";
		a = false;
		}
	if (!fieldvalidation(document.form1.c2s_billingcity))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Billing address city" : "Enter Billing address city";
		a = false;
		}
	if (!fieldvalidation(document.form1.c2s_billingpostcode))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Billing postcode" : "Enter Billing postcode";
		a = false;
		}

	if (!fieldvalidation(document.form1.c2s_shippingaddress1))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Shipping address line 1" : "Enter Shipping address line 1";
		a = false;
		}
	if (!fieldvalidation(document.form1.c2s_shippingcity))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Shipping address city" : "Enter Shipping address city";
		a = false;
		}
	if (!fieldvalidation(document.form1.c2s_shippingpostcode))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Shipping postcode" : "Enter Shipping postcode";
		a = false;
		}

	if (!fieldvalidation(document.form1.c2s_email))
		{
		document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br>Enter Email" : "Enter Email";
		a = false;
		}
	
	document.getElementById('errors').innerHTML += document.getElementById('errors').innerHTML ? "<br><br><br>" : "";
	window.scroll(0,0); 	 //600
	return a;
	}

function onloadvalidation()
	{

            $('#form1 input', ':visible', document.body).each(function(i) { 
                //copy the values from the billing_fields inputs
                //to the equiv inputs on the shipping_fields
                //$(this).val( $('#billing_fields input').eq(i).val() );
				if   ((this.id != "copyaddress")
					&&(this.id != "c2s_type5")
					&&(this.id != "c2s_type2")
					&&(this.id != "c2s_type3")
					&&(this.id != "c2s_type4")
					&&(this.id != "c2s_fax")
					&&(this.id != "c2s_www")
					&&(this.id != "c2s_billingaddress2")
					&&(this.id != "c2s_shippingaddress2")
					&&(this.id != "c2s_telephone")
					&&(this.id != "submit")
					) fieldvalidation(this);
                });
			typevalidate();
//			checkboxvalidation(document.getElementById('c2s_type'));
	
/*	fieldvalidation(document.form1.c2s_contactname,document.getElementById('c2s_contactname_ok'))
	fieldvalidation(document.form1.c2s_companyname,document.getElementById('c2s_companyname_ok'))
	fieldvalidation(document.form1.c2s_address,document.getElementById('c2s_address_ok'))
	fieldvalidation(document.form1.c2s_postcode,document.getElementById('c2s_postcode_ok'))
	fieldvalidation(document.form1.c2s_telephone,document.getElementById('c2s_telephone_ok'))
	emailvalidation(document.form1.c2s_email,document.getElementById('c2s_email_ok'))
	fieldvalidation(document.form1.c2s_collpoint,document.getElementById('c2s_collpoint_ok'))
	fieldvalidation(document.form1.c2s_hours,document.getElementById('c2s_hours_ok'))  */
	}

function typevalidate()
	{
	value = $("input[name=c2s_type]:radio:checked").val();
	if (typeof(value) == "undefined") {
//		$("input[name=c2s_type]:radio").css("background","#FCFFC6");
		return false
		}
	else
		{
//		$("input[name=c2s_type]:radio").css("background","#FFFFFF");
		if (value == 4) {
			$("#agentselection div.name").html("Agent number")
			$("#agentselection").show("slow")
			}
		else if (value == 5) {
			$("#agentselection div.name").html("Charity name or number")
			$("#agentselection").show("slow")
			}
		else {
			$("#agentselection").hide("slow")
			}
		return true
		}
	}
function checkboxvalidation(obj)
	{
//	confirm ((obj.value=="please select")||(obj.value==""));
	if ((obj.value=="please select")||(obj.value==""))
		{
		obj.style.background = "#FCFFC6";
		return false;
		}
		else
		{
		obj.style.background = "#ffffff"; 
		return true;
		}
	}

function showhide(id)
{
if (document.getElementById(id + "Icon").getAttribute("rel")=="closed")
	{
	document.getElementById(id + "Icon").src = 'img/open.png';
	document.getElementById(id + "Icon").setAttribute("rel","open");

	i = document.getElementsByTagName("tr");
	for (j=1;j<=i.length-1;j++)
		{
		if ((i[j].id.match(id + "Subclients")))
			{
			i[j].style.visibility = "visible";
			i[j].style.display = "";
			}
		}

//	document.getElementById(id + "Subclients").style.visibility = "visible";
//	document.getElementById(id + "Subclients").style.display = "";
	}
else if (document.getElementById(id + "Icon").getAttribute("rel")=="open")
	{
	document.getElementById(id + "Icon").src = 'img/close.png';
	document.getElementById(id + "Icon").setAttribute("rel","closed");

	i = document.getElementsByTagName("tr");
	for (j=1;j<=i.length-1;j++)
		{
		if ((i[j].id.match(id + "Subclients")))
			{
			i[j].style.visibility = "hidden";
			i[j].style.display = "none";
			}
		}


//	document.getElementById(id + "Subclients").style.visibility = "hidden";
//	document.getElementById(id + "Subclients").style.display = "none";
	}
//colortable();
}



function shagentselection(type)
{
  div_id = "agentselection";
  
  if (browserType == "gecko" )
     document.poppedLayer =
         eval('document.getElementById(div_id)');
  else if (browserType == "ie")
     document.poppedLayer =
        eval('document.getElementById(div_id)');
  else
     document.poppedLayer =
        eval('document.layers[div_id]');


//  if (type==4) {document.poppedLayer.innerHTML = '<div class="name" style="font-size:larger">Agent number</div><div class="field"><input name="c2s_parentid" type="text" id="c2s_parentid" value="<?php echo $_POST['c2s_parentid']; ?>"  onkeyup="fieldvalidation(this)"/></div>';
//}
// else {document.poppedLayer.innerHTML = '';
//} 

  if (type==4) {document.poppedLayer.style.visibility = 'visible';document.poppedLayer.style.display = 'inline'}
  else {document.poppedLayer.style.visibility = 'hidden';document.poppedLayer.style.display = 'none'} 
}



function emailvalidation(entered,id)
{
// E-mail Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
/*if (entered.value.length==0)
	{
	id.innerHTML = "";
	return false;
	}
	else*/
	{

//	with (entered)
		{
		apos=entered.value.indexOf("@"); 
		dotpos=entered.value.lastIndexOf(".");
		lastpos=entered.value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
			{
			id.innerHTML = "<img src='x.png'>";
			return false;
			}
		else
			{
			id.innerHTML = "<img src='ok.png'>"; 
			return true;
			}
		}
	}
}

function fieldvalidation(field)
	{
	if (field.value.length==0)
		{
		field.style.background = "#FCFFC6";
		return false;
		}
		else
		{
		field.style.background = "#ffffff"; 
		return true;
		}
	}
	
	


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function radioIsChecked(radioObj) {
	if(!radioObj)
		return false;
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return true;
		else
			return false;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return true;
		}
	}
	return false;
}	

function generateCode()
	{
	if (document.getElementById('codeEdit').checked==false)
		{
		document.form1.ProductCode.value = getCheckedValue(document.form1.codeTI) + "-" + getCheckedValue(document.form1.codeVNV) + "-" + document.form1.codeBrand.value + "-" + document.form1.codeCode.value;
		if ((getCheckedValue(document.form1.codeTI)!="") && (getCheckedValue(document.form1.codeVNV)!="") && (document.form1.codeBrand.value!="") && (document.form1.codeCode.value!=""))
			{return true;} else
			{return false;}
 		}
	}
	
function generateDescriptionPrefix()
	{
	if (document.form1.generateDescription.checked==true)
		{
		var vnv = new Array;
		vnv[''] = "";
		vnv['V'] = "Virgin";
		vnv['NV'] = "Non-virgin";
		
		document.form1.Description.value = "Empty Printer Cartridge: " + document.form1.codeCode.value + " " + vnv[getCheckedValue(document.form1.codeVNV)];
		if ((getCheckedValue(document.form1.codeVNV)!="") && (document.form1.codeCode.value!=""))
			{return true;} else
			{return false;}
		}
	}
	
	
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function editProductFormValidate()
	{
	var errors = new String;
//	document.form1.Description.value = radioIsChecked(document.form1.codeTI);
	if ((document.form1.ProductCode.value=="") || (document.form1.ProductCode.value=="---")) errors += "Please enter Product code<br>";
//	if (radioIsChecked(document.form1.codeTI)==false) errors += "Please select Toner / Ink<br>";
//	if (radioIsChecked(document.form1.codeVNV)==false) errors += "Please select Virgin / Not virgin<br>";
	if (document.form1.Description.value=="") errors += "Please enter Description<br>";
	if (document.form1.PerBox.value=="") {errors += "Please enter Per box value<br>";}
	else if (!IsNumeric(document.form1.PerBox.value)) {errors += "Per box has to be a number<br>";}
	if (document.form1.StockNotDamaged.value=="") {errors += "Please enter Stock not damaged<br>";}
	else if (!IsNumeric(document.form1.StockNotDamaged.value)) {errors += "Stock not damaged has to be a number<br>";}
	if (document.form1.StockDamaged.value=="") {errors += "Please enter Stock damaged<br>";}
	else if (!IsNumeric(document.form1.StockDamaged.value)) {errors += "Stock damaged has to be a number<br>";}

	if (document.form1.Price1.value=="") {errors += "Please enter Price1<br>";}
	else if (!IsNumeric(document.form1.Price1.value)) {errors += "Price1 has to be a number<br>";}
	if (document.form1.Price2.value=="") {errors += "Please enter Price2<br>";}
	else if (!IsNumeric(document.form1.Price2.value)) {errors += "Price2 has to be a number<br>";}
	if (document.form1.Price3.value=="") {errors += "Please enter Price3<br>";}
	else if (!IsNumeric(document.form1.Price3.value)) {errors += "Price3 has to be a number<br>";}
	if (document.form1.Price4.value=="") {errors += "Please enter Price4<br>";}
	else if (!IsNumeric(document.form1.Price4.value)) {errors += "Price4 has to be a number<br>";}

	window.scroll(0,0); 	

	if (errors.length>0)
		{
		document.getElementById("errors").innerHTML=errors;
		return false;
		}
	else
		{
		document.form1.ProductCode.disabled = false;
		return true;
		}
		
	}
	
function editPricelistFormValidate()
	{
	var errors = new String;
	e = document.getElementsByTagName("input");
	for (var i = 0;i<=e.length-3;i++)
		{
		if (!(e[i].value)&&(e[i].id.match("Price")))
			{
			errors += "Please enter value " + e[i].id + "<br>";
			}
		else if (!IsNumeric(e[i].value)&&(e[i].id.match("Price")))
			{
			errors += e[i].id + " value must be a number<br>";
			}
		else if (!(e[i].value)&&(e[i].id == "name"))
			{
			errors += "Please pricelist name<br>";
			}
	
		}

	window.scroll(0,0); 	

	if (errors.length>0)
		{
		document.getElementById("errors").innerHTML=errors;
		return false;
		}
	else
		{
		document.form1.ProductCode.disabled = false;
		return true;
		}

	}
	
	
	
function evaluatePricelistFunction()
	{
	var errors = new String;
	e = document.getElementsByTagName("input");
	for (var i = 0;i<=e.length-3;i++)
		{
		if (e[i].id.match("Price"))
			if ((e[i].id.match("Price"))&&(IsNumeric(e[i].value)))
			{
			func = document.getElementById('function').value.replace("%","/100*"+e[i].value)
//			document.write(e[i].value + func);
			e[i].value = eval(e[i].value + func)
			e[i].value = Math.round(e[i].value*100)/100
			}
	
		}
	}
