function CustCartAdd()
{	
  var xmlHttp = false;
	
	try 
	{
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
	}
	catch (e) 
	{
	  try
	  {
		  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e2)
		{
		  xmlHttp = false
    }
  }
	
  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
	{
	  xmlHttp = new XMLHttpRequest();
  }

	var Qty = document.getElementById("Qty").value;
	
	// make sure the qty is numeric
	if(!isNaN(Qty))
	{	
	  document.getElementById("Err").innerHTML = '';
	
		if(xmlhttp) 
		{
			xmlhttp.open("POST","/assets/scrollto/ajax/ajax-custom-Add.php",true);
			xmlhttp.onreadystatechange = CustomSaverResponse;
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send("Qty="+Qty);
		}
	}
	else
	{
		document.getElementById("Err").innerHTML = 'Please enter a numeric quantity.';
	}
}

function AddToCable(Group, Desc1, ID, Tab)
{
	// sort out the XML Object
	var xmlHttp = false;
	var CheckAll = true;
	var Err = '';
	var Colour = '';
	var CableLength = '';
	
	try 
	{
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
	}
	catch (e) 
	{
	  try
	  {
		  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e2)
		{
		  xmlHttp = false
    }
  }
	
  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
	{
	  xmlHttp = new XMLHttpRequest();
  }
	
	// we dont need the desc1 for cable color
	if(Desc1 == 'Cable Colour'){Desc1 = '';}
		
	// do we need to get the colour?
	if((Group == 'Boot Type' || Group == 'Cable Colour') && Desc1 != 'Unbooted')
	{
		if(Desc1 == '')
		{Colour = document.getElementById("Sboot"+ID).value;}
		else
		{Colour = ' ('+document.getElementById("Sboot"+ID).value+')';}
	}	
	else
	{
		Coloue = '';
	}
	
	// do we need to get the length?
	if(Desc1 == 'Length')
	{
		CableLength = document.getElementById("CabLen").value;
		
		if(CableLength != '' && CableLength > 0)
		{	
			// check if the length is numeric
			if(isNaN(CableLength))
			{
				Err = '<b>Warning: </b>The cable length must be numeric. Please correct the length using the text box above.';
				CheckAll = false;
			}
			
			if(CheckAll == true)
			{
				// if we are still true, make sure the length is less that 300
				if(CableLength > 300)
				{
					Err = '<b>Warning: </b>The cable length must be less than 300 meters. Please correct the length using the text box above.';
					CheckAll = false;
				}
			}
			
			if(CheckAll == true)
			{
				Desc1 = CableLength+' Meter(s)';
			}
		}
		else
		{
			Err = '<b>Warning: </b>Please enter a valid length to continue.';
			CheckAll = false;
		}
	}
	
	if(Group == 'Cable Type')
	{
		if(Desc1 == 'Cat6')
		{document.getElementById('PVC').style.display = "none";}
		else
		{document.getElementById('PVC').style.display = "block";}
		
		Cable = Desc1;
	}
	
	if(Group == 'Sheath Type' || Group == 'Cable Type')
	{
		var selectbox = document.getElementById('Sboot0');
		var len = selectbox.options.length;
		
		// remove all the cable colour options			
		for(i=len-1; i>=0; i--){selectbox.remove(i);}
		
		if(Group == 'Sheath Type'){Sheath = Desc1;}
		
		//alert("Desc1 = "+Desc1+"\nSheath = "+Sheath+"\nCable = "+Cable);
				
		if(Sheath == 'LS0H')
		{	
			if(Cable == 'Cat5e FTP')
			{
				var optn = document.createElement("OPTION");
				optn.text = 'Grey';
				optn.value = 'Grey';
				selectbox.options.add(optn);
			}
			else
			{
				var LSOHarr = new Array('Blue', 'Green', 'Red', 'Yellow', 'Grey');
				var Len = LSOHarr.length;
				
				for(i=0; i<Len; i++)
				{
					var optn = document.createElement("OPTION");
					optn.text = LSOHarr[i];
					optn.value = LSOHarr[i];
					selectbox.options.add(optn);
				}
			}
		}	
		else
		{
			var LSOHarr = new Array('Black', 'Blue', 'Green', 'Orange', 'Pink', 'Purple', 'Red', 'White', 'Yellow', 'Grey');
			var Len = LSOHarr.length;

			for(i=0; i<Len; i++)
			{
				var optn = document.createElement("OPTION");
				optn.text = LSOHarr[i];
				optn.value = LSOHarr[i];
				selectbox.options.add(optn);
			}
		}
	}
	
	// do we have a label here?
	if(Group == 'Labels')
	{
		// create an array of the label ID's
		var IdArr = new Array('LL1', 'LL2', 'LL3', 'RL1', 'RL2', 'RL3');
		var NameArr = new Array('L1', 'L2', 'L3', 'R1', 'R2', 'R3');
		var lblStr = '';
		
		for(i=0; i<=5; i++)
		{
			var Str = document.getElementById(IdArr[i]).value;
			if(Str != '')
			{
				lblStr += NameArr[i]+' - '+Str+'@@';
			}
		}
		
		if(lblStr == '')
		{
		  Err = '<b>Warning: </b>To add a label to your cable please enter some text in the boxes above.';
			CheckAll = false;	
		}
		else
		{
			var Len = lblStr.length;						
			Desc1 = lblStr.substr(0, Len-2);
		}
	}
	
	//alert("Group = "+Group+"\nDesc1 = "+Desc1+"\nColour = "+Colour);
  
	if(CheckAll == true)
	{
		document.getElementById("Err").innerHTML = '';
		
		if(xmlhttp) 
		{
			xmlhttp.open("POST","/assets/scrollto/ajax/ajax-custom.php",true);
			xmlhttp.onreadystatechange = Response;
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send("Group="+Group+"&Desc1="+Desc1+Colour);
		}
	}
	else
	{
		document.getElementById("Err").innerHTML = Err;
	}
}

function Response() 
{	
  if (xmlhttp.readyState == 4) 
	{
    if(xmlhttp.status == 200 || xmlhttp.status == 0) 
	 	{
			var response = xmlhttp.responseText;
			//alert(""+response); 
			var update = new Array();			
			if(response.indexOf('|' != -1)) 
			{ 
        update = response.split('|');
				ArrLen = update.length-1;

				for(i=0; i<=ArrLen; i++)
				{
					if(update[i] == 'VALUE')
					{
						var values = update[i+1].split('##');
						if(document.getElementById(values[0])){document.getElementById(values[0]).value = values[1];}
					}
					else
				  {
					  if(document.getElementById(update[i])){document.getElementById(update[i]).innerHTML = update[i+1];}					  
					}
					i++;
				}
      }
    }
    else 
		{
    	alert("Error during AJAX call 5. Please try again");
    }
  }
}

function CustomSaverResponse()
{
  if(xmlhttp.readyState == 4) 
	{
    if(xmlhttp.status == 200) 
	 	{
			var response = xmlhttp.responseText;
			//;alert(""+response);
			var update = new Array();			
			if(response.indexOf('|' != -1)) 
			{
      	update = response.split('|');
        document.getElementById(update[0]).innerHTML = update[1];
				if (  document.getElementById(update[2]))
			 { document.getElementById(update[2]).innerHTML = update[3]; }
			 if (  document.getElementById(update[4]))
			 { document.getElementById(update[4]).innerHTML = update[5]; }
			 if (  document.getElementById(update[6]))
			 { document.getElementById(update[6]).innerHTML = update[7]; }
			 if (  document.getElementById(update[8]))
			 { document.getElementById(update[8]).innerHTML = update[9]; }			 
			 if (  document.getElementById(update[10]))
			 { document.getElementById(update[10]).innerHTML = update[11]; }
			}
			Toggle(); 
    }
    else 
		{
    	alert("Error during AJAX call No1. Please try again");
    }
  }	
}

function CngColor(ImgId, DropID)
{	
	var color = document.getElementById(DropID).value;
	
  //alert("DropID = "+DropID+"\nImgId = "+ImgId+"\ncolor = "+color);
	
	document.getElementById(ImgId).innerHTML = '<img src="/assets/images/config/RJ45/booted-'+color+'-left.png" width="133px">';
}

function CngCount(MaxChars, ID1, ID2)
{
	var Str = document.getElementById(ID1).value;
	var StrLen = Str.length;
	
	if(StrLen > 10)
	{document.getElementById(ID1).value = Str.substring(0, 10);}
	else
	{document.getElementById(ID2).innerHTML = StrLen;}
	
	//alert("MaxChars = "+MaxChars+"\nID = "+ID+"\nStr = "+Str+"\nStrLen = "+StrLen);
}
