
//List of products and their price

var NO_OF_PRODUCTS=5 //Enter total number of products here

var prods=new Array(NO_OF_PRODUCTS)
for(q=0;q<prods.length;q++)
	prods[q]=new Array(2)


//--------------------------------------------------------------------
//Enter product name and price here
// First Row => Product Name
// Second Row => Price
//--------------------------------------------------------------------
prods[0][0]="Please Select"
prods[0][1]="0.00"
prods[4][0]="Able Skills Tiling DVD"
prods[4][1]="4.99"
prods[1][0]="Able Skills Plumbing DVD"
prods[1][1]="4.99"
prods[2][0]="Able Skills Advanced Plumbing DVD"
prods[2][1]="4.99"
prods[3][0]="Able Skills Plastering DVD"
prods[3][1]="4.99"
//--------------------------------------------------------------------
//--------------------------------------------------------------------




//Code starts from here-->

var count=0
function addproduct()
{
	count++
	var row=document.createElement("tr")
	row.setAttribute("id","row_"+count)

	var col0=document.createElement("td")
	newQuantity=document.createElement("input")
	newQuantity.setAttribute("name","Quantity_"+count)
	newQuantity.setAttribute("id","quantity_"+count)
	newQuantity.setAttribute("value","1")
	newQuantity.setAttribute("size","5")
	newQuantity.setAttribute("style","width:50px")
	newQuantity.onblur=new Function("calculateTotal("+count+")")
	col0.appendChild(newQuantity)
	row.appendChild(col0)


	var col1=document.createElement("td")
	newSelect=document.createElement("select");
	for(i=0;i<prods.length;i++)
	{
		newSelect.options[newSelect.options.length] = new Option(prods[i][0],prods[i][0]);
	}

	newSelect.name="Product_"+count;
	newSelect.id="Product_"+count;
	newSelect.onchange=new Function("UpdateSelect("+count+")")
	newSelect.setAttribute
	col1.appendChild(newSelect);
	row.appendChild(col1)

	var col2=document.createElement("td")
	newText=document.createElement("input")
	newText.readOnly="readOnly"
	newText.setAttribute("size","5")
	newText.setAttribute("style","width:50px")
	newText.id="price_"+count
	newText.name="Price_"+count
	newText.setAttribute("value",prods[0][1])
	col2.appendChild(newText)
	row.appendChild(col2)

	var col3=document.createElement("td")
	newTotal=document.createElement("input")
	newTotal.setAttribute("name","Total_"+count)
	newTotal.setAttribute("id","Total_"+count)
	newTotal.readOnly="readOnly"
	newTotal.setAttribute("size","6")
	newTotal.setAttribute("style","width:60px")
	col3.appendChild(newTotal)
	row.appendChild(col3)

	var col4=document.createElement("td")
	remOpt=document.createElement("input")
	remOpt.setAttribute("type","button")
	remOpt.setAttribute("value","Remove")
	remOpt.setAttribute("name","Remove_"+count)
	remOpt.setAttribute("id","Remove_"+count)
	remOpt.onclick=new Function ("delRow(document.getElementById('row_'+count))")
	col4.appendChild(remOpt)
	row.appendChild(col4)

	var t = document.getElementById('grosstotal')
	t.parentNode.insertBefore(row,t);

	calculateTotal(count)

}

function delRow(elemToDel)
{
	if(!elemToDel)return;
	if(count>1)
	{
		elemToDel.parentNode.removeChild(elemToDel);
		count--
		CalculateAllTotal()
	}

}


function calculateTotal(cnt)
{
	document.getElementById('quantity_'+cnt).value=Math.round(document.getElementById('quantity_'+cnt).value)
	var q=document.getElementById('quantity_'+cnt).value
	if(q<1)
	{
		alert("Quantity cannot be less than 1")
		document.getElementById('quantity_'+cnt).value=1
		q=1
	}
	var p=document.getElementById('price_'+cnt).value
	var t=p*q
	document.getElementById('Total_'+cnt).value=t.toFixed(2)
	CalculateAllTotal()

}

function CalculateAllTotal()
{
	var tot=0
	for(z=1;z<=count;z++)
	{
		tot+=parseFloat(document.getElementById('Total_'+z).value)
	}
	document.getElementById('grosstotalvalue').innerHTML=tot.toFixed(2)
	//CalculateDiscount(tot)
        CalculateNetTotal(tot,0)

}

function CalculateDiscount(tot)
{
	var discper=0
	switch(count)
	{
		case 1:discper=0
				break
		case 2:discper=0
			   break
		case 3:discper=0
			   break
		case 4:discper=0
				break
		default:discper=0
	}
	var discamt=tot*discper
	document.getElementById('discdesc').innerHTML="Discount: "+discper*100+"%"
	document.getElementById('discamt').innerHTML=discamt.toFixed(2)
	document.getElementById('hidden_discount').value=discamt.toFixed(2)
	CalculateNetTotal(tot,discamt)
}

function CalculateDiscountold(tot)
{
	var discper=0
	switch(count)
	{
		case 1:discper=0
				break
		case 2:discper=0.05
			   break
		case 3:discper=0.1
			   break
		case 4:discper=0.15
				break
		default:discper=0.15
	}
	var discamt=tot*discper
	document.getElementById('discdesc').innerHTML="Discount: "+discper*100+"%"
	document.getElementById('discamt').innerHTML=discamt.toFixed(2)
	document.getElementById('hidden_discount').value=discamt.toFixed(2)
	CalculateNetTotal(tot,discamt)
}

function CalculateNetTotal(tot,discamt)
{
	var nettot=tot-discamt
	document.getElementById('grosstotalvalue').innerHTML=nettot.toFixed(2)
	document.getElementById('hidden_total_price').value=nettot.toFixed(2)
	document.getElementById('product_count').value=count
}
function UpdateSelect(cnt)
{
	var s=document.getElementById('Product_'+cnt)
	document.getElementById('price_'+cnt).value=prods[s.selectedIndex][1]
	calculateTotal(cnt)
}


/////////////////////////


		var account_code='ables11112';
		var license_code='RG55-YN67-MF91-RF59';
		var machine_id='';

		function pcaByPostcodeBegin()
		   {
			  var postcode = document.forms[0]["postcode_search"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";

		      document.getElementById("divLoading").style.display = '';

		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=lookup";
		      strUrl += "&type=by_postcode";
		      strUrl += "&postcode=" + escape(postcode);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&machine_id=" + escape(machine_id);
		      strUrl += "&callback=pcaByPostcodeEnd";

		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and should be interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);



		   }

		function pcaByPostcodeEnd()
		   {
			  document.getElementById("divLoading").style.display = 'none';

		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            document.getElementById("selectaddress").style.display = 'none';
		            document.getElementById("btnFetch").style.display = 'none';
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  document.getElementById("selectaddress").style.display = 'none';
						  document.getElementById("btnFetch").style.display = 'none';
		                  alert("Sorry, no matching items found. Please try another postcode.");
		               }
		            else
		               {
						  document.forms[0]["selectaddress"].style.display = '';
						  document.forms[0]["btnFetch"].style.display = '';

						  for (i=document.forms[0]["selectaddress"].options.length-1; i>=0; i--){
							  document.forms[0]["selectaddress"].options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.forms[0]["selectaddress"].options[document.forms[0]["selectaddress"].length] = new Option(pca_description[i], pca_id[i]);
		                  }
		               }
		         }
		   }

		function pcaFetchBegin()
		   {
			  var address_id = document.forms[0]["selectaddress"].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";

		      //Build the url
		      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=fetch";
		      strUrl += "&id=" + escape(address_id);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      strUrl += "&machine_id=" + escape(machine_id);
		      strUrl += "&callback=pcaFetchEnd";

		      //Make the request
		      if (scriptTag)
		         {
		            //The following 2 lines perform the same function and should be interchangeable
		            headTag.removeChild(scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      scriptTag = document.createElement("script");
		      scriptTag.src = strUrl
		      scriptTag.type = "text/javascript";
		      scriptTag.id = "pcaScriptTag";
		      headTag.appendChild(scriptTag);

		      document.forms[0]["selectaddress"].style.display = 'none';
		      document.forms[0]["btnFetch"].style.display = 'none';
		   }

		function pcaFetchEnd()
		   {
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  alert("Sorry, no matching items found");
		               }
		            else
		               {
						  document.forms[0]["company"].value = '' + pca_organisation_name[0];
						  document.forms[0]["line1"].value = '' + pca_line1[0];
						  document.forms[0]["line2"].value = '' + pca_line2[0];
						  document.forms[0]["line3"].value = '' + pca_line3[0];
						  document.forms[0]["line4"].value = '' + pca_line4[0];
						  document.forms[0]["line5"].value = '' + pca_line5[0];
						  document.forms[0]["town"].value = '' + pca_post_town[0];
						  document.forms[0]["county"].value = '' + pca_county[0];
						  document.forms[0]["postcode"].value = '' + pca_postcode[0];
		               }
		         }
		   }

/////////////////////////


		function ship_pcaByPostcodeBegin()
		   {
			  var ship_postcode = document.forms[0]["ship_postcode_search"].value;
		      var ship_scriptTag = document.getElementById("ship_pcaScriptTag");
		      var ship_headTag = document.getElementsByTagName("head").item(0);
		      var ship_strUrl = "";

		      document.getElementById("ship_divLoading").style.display = '';

		      //Build the url
		      ship_strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      ship_strUrl += "&action=lookup";
		      ship_strUrl += "&type=by_postcode";
		      ship_strUrl += "&postcode=" + escape(ship_postcode);
		      ship_strUrl += "&account_code=" + escape(account_code);
		      ship_strUrl += "&license_code=" + escape(license_code);
		      ship_strUrl += "&machine_id=" + escape(machine_id);
		      ship_strUrl += "&callback=ship_pcaByPostcodeEnd";

		      //Make the request
		      if (ship_scriptTag)
		         {
		            //The following 2 lines perform the same function and should be interchangeable
		            ship_headTag.removeChild(ship_scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      ship_scriptTag = document.createElement("script");
		      ship_scriptTag.src = ship_strUrl
		      ship_scriptTag.type = "text/javascript";
		      ship_scriptTag.id = "ship_pcaScriptTag";
		      ship_headTag.appendChild(ship_scriptTag);



		   }

		function ship_pcaByPostcodeEnd()
		   {
			  document.getElementById("ship_divLoading").style.display = 'none';

		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            document.getElementById("ship_selectaddress").style.display = 'none';
		            document.getElementById("ship_btnFetch").style.display = 'none';
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  document.getElementById("ship_selectaddress").style.display = 'none';
						  document.getElementById("ship_btnFetch").style.display = 'none';
		                  alert("Sorry, no matching items found. Please try another postcode.");
		               }
		            else
		               {
						  document.forms[0]["ship_selectaddress"].style.display = '';
						  document.forms[0]["ship_btnFetch"].style.display = '';

						  for (i=document.forms[0]["ship_selectaddress"].options.length-1; i>=0; i--){
							  document.forms[0]["ship_selectaddress"].options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.forms[0]["ship_selectaddress"].options[document.forms[0]["ship_selectaddress"].length] = new Option(pca_description[i], pca_id[i]);
		                  }
		               }
		         }
		   }

		function ship_pcaFetchBegin()
		   {
			  var ship_address_id = document.forms[0]["ship_selectaddress"].value;
		      var ship_scriptTag = document.getElementById("ship_pcaScriptTag");
		      var ship_headTag = document.getElementsByTagName("head").item(0);
		      var ship_strUrl = "";

		      //Build the url
		      ship_strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		      ship_strUrl += "&action=fetch";
		      ship_strUrl += "&id=" + escape(ship_address_id);
		      ship_strUrl += "&account_code=" + escape(account_code);
		      ship_strUrl += "&license_code=" + escape(license_code);
		      ship_strUrl += "&machine_id=" + escape(machine_id);
		      ship_strUrl += "&callback=ship_pcaFetchEnd";

		      //Make the request
		      if (ship_scriptTag)
		         {
		            //The following 2 lines perform the same function and should be interchangeable
		            ship_headTag.removeChild(ship_scriptTag);
		            //scriptTag.parentNode.removeChild(scriptTag);
		         }
		      ship_scriptTag = document.createElement("script");
		      ship_scriptTag.src = ship_strUrl
		      ship_scriptTag.type = "text/javascript";
		      ship_scriptTag.id = "ship_pcaScriptTag";
		      ship_headTag.appendChild(ship_scriptTag);

		      document.forms[0]["ship_selectaddress"].style.display = 'none';
		      document.forms[0]["ship_btnFetch"].style.display = 'none';
		   }

		function ship_pcaFetchEnd()
		   {
		      //Test for an error
		      if (pcaIsError)
		         {
		            //Show the error message
		            alert(pcaErrorMessage);
		         }
		      else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               {
		                  alert("Sorry, no matching items found");
		               }
		            else
		               {
						  document.forms[0]["ship_company"].value = '' + pca_organisation_name[0];
						  document.forms[0]["ship_line1"].value = '' + pca_line1[0];
						  document.forms[0]["ship_line2"].value = '' + pca_line2[0];
						  document.forms[0]["ship_line3"].value = '' + pca_line3[0];
						  document.forms[0]["ship_line4"].value = '' + pca_line4[0];
						  document.forms[0]["ship_line5"].value = '' + pca_line5[0];
						  document.forms[0]["ship_town"].value = '' + pca_post_town[0];
						  document.forms[0]["ship_county"].value = '' + pca_county[0];
						  document.forms[0]["ship_postcode"].value = '' + pca_postcode[0];
		               }
		         }
		   }
/////////////////////////

function checkShip()
{
	if(document.getElementById('shipaddsame').checked)
		document.getElementById('ship_div').style.display='none'
	else
		document.getElementById('ship_div').style.display=''
}

function validate_onlyalnum(field)
{
with (field)
{
if (value==null||value=="")
{return true}
else{
	var objRegExp  =  /[a-zA-Z0-9-,.()/#]/;
	return objRegExp.test(field.value);
}
}
}

function validate_onlynum(field)
{
with (field)
{
if (value==null||value=="")
{return true}
else{
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	return objRegExp.test(field.value);
}
}
}

function validate_numeric(field)
{
with (field)
{
if (value==null||value=="")
  {return false}
else {
	var objRegExp  =  /(^-?\d\d*\.\d*$)|\s|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	return objRegExp.test(field.value);
}
}
}

function validate_alphanumeric(field)
{
with (field)
{
if (value==null||value=="")
  {return false}
else {
	var objRegExp  =  /[a-zA-Z0-9-,.()/#]/;
	return objRegExp.test(field.value);
}
}
}

function validate_email(field)
{
	with (field)
	{
		var objRegExp= /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*((\.[A-Za-z][A-Za-z])|(\.[A-Za-z][A-Za-z][A-Za-z]))+$/;
		  return objRegExp.test(field.value);

	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_alphanumeric(name)==false)
		  {alert("Please type a name. Please do not use special characters.");name.focus();return false}
		if (validate_alphanumeric(line1)==false)
		  {alert("Please type an address. Please do not use special characters.");line1.focus();return false}
		if (validate_alphanumeric(line2)==false)
		  {alert("Please type the full address. Please do not use special characters.");line2.focus();return false}
		if (validate_alphanumeric(town)==false)
		  {alert("Please type town name. Please do not use special characters.");town.focus();return false}
		if (validate_alphanumeric(county)==false)
		  {alert("Please type the County name. Please do not use special characters.");county.focus();return false}
		if (validate_alphanumeric(postcode)==false)
		  {alert("Please type the postcode. Please do not use special characters.");postcode.focus();return false}
		if (validate_numeric(telephone)==false)
		  {alert("Please type a contact number");telephone.focus();return false}
		if (validate_email(email)==false)
		  {alert("Please type a correct email address");email.focus();return false}

		if (!document.getElementById('shipaddsame').checked)
		{
			if (validate_alphanumeric(ship_name)==false)
			  {alert("Please type a shipping name. Please do not use special characters.");ship_name.focus();return false}
			if (validate_alphanumeric(ship_line1)==false)
			  {alert("Please type a shipping address. Please do not use special characters.");ship_line1.focus();return false}
			if (validate_alphanumeric(ship_line2)==false)
			  {alert("Please type the full shipping address. Please do not use special characters.");ship_line2.focus();return false}
			if (validate_alphanumeric(ship_town)==false)
			  {alert("Please type town name. Please do not use special characters.");ship_town.focus();return false}
			if (validate_alphanumeric(ship_county)==false)
			  {alert("Please type the shipping County name. Please do not use special characters.");ship_county.focus();return false}
			if (validate_alphanumeric(ship_postcode)==false)
			  {alert("Please type the shipping postcode. Please do not use special characters.");ship_postcode.focus();return false}
		}
		
		return true;

/*
		if (validate_numeric(cardnumber)==false)
		  {alert("Please type a correct card number");cardnumber.focus();return false}

		if (validate_onlyalnum(company)==false)
		  {alert("Please do not use special characters.");company.focus();return false}
		if (validate_onlyalnum(line3)==false)
		  {alert("Please do not use special characters.");line3.focus();return false}
		if (validate_onlyalnum(line4)==false)
		  {alert("Please do not use special characters.");line4.focus();return false}
		if (validate_onlyalnum(line5)==false)
		  {alert("Please do not use special characters.");line5.focus();return false}
		if (validate_onlyalnum(ship_company)==false)
		  {alert("Please do not use special characters.");ship_company.focus();return false}
		if (validate_onlyalnum(ship_line3)==false)
		  {alert("Please do not use special characters.");ship_line3.focus();return false}
		if (validate_onlyalnum(ship_line4)==false)
		  {alert("Please do not use special characters.");ship_line4.focus();return false}
		if (validate_onlyalnum(ship_line5)==false)
		  {alert("Please do not use special characters.");ship_line5.focus();return false}
		if (validate_onlynum(verification_num)==false)
		  {alert("Please type numbers only.");verification_num.focus();return false}
		if (validate_onlynum(switch_issue_num)==false)
		  {alert("Please type numbers only.");switch_issue_num.focus();return false}

*/


	}
}

function submitpayment(thisform) {

	if (validate_form(thisform)) {
		var amount = parseFloat(thisform.amount.value) * 100;
		thisform.amount.value = parseInt(amount);

		thisform.dvd_names.value = "";

		if (thisform.Product_Count.value > 0) {
			for (var i=1; i <= thisform.Product_Count.value; i++) {
				eval('thisform.dvd_names.value += thisform.Product_' + i +'.value + " + ";');
			}
		}

		thisform.dvd_names.value = thisform.dvd_names.value.replace(/DVD \+/g,"+");
		thisform.dvd_names.value = thisform.dvd_names.value.replace(/\+\s+$/, 'DVD')

		return true;
	} else {
		return false;
	}
}