// JavaScript Document
// Author : don mclean, onlineone
// Copyright: online one
//
// Script runs onload within the browser and detects the page on the check out and then does DOM manipulation
// based on the user/customer requirement.

/* Implenmented for VetnPet
	-----------------------
	
	Payment :  Puts on the cash button the id = "radioRadio" ...Elvis Costello will make it work :-)
*/

// this script runs on the 
			// add the main picture

//this script runs on the 
			// add the main picture
			function onLoadReg()
			{
				// get the title of the page loaded
				var title = document.title;
				var t = title.split(' ');
				//alert(t[0]);
				//alert(t[0])
				
				switch ( t[0] ) 
				{
		// STEP 1 - The shopping basket is being viewed			
				case ('Shopping') : 
				// remove the testimonials 
				document.getElementById('testimonial').innerHTML ="";
				
				//document.getElementById('checkout').className ="proceedButton";

				document.getElementById("checkout").className='replacecheckout';
				document.getElementById("checkout").value = "";

				document.getElementById("cancel").className='continueshopping';
				document.getElementById("cancel").value = "";
				
				document.getElementById("recalc").className='updatetotal';
				document.getElementById("recalc").value = "";					
				break;
		// STEP 2 - The "Buy Now" button has been pressed and now viewing the registration page					
				case ('Checkout') : 
				document.getElementById('testimonial').innerHTML ="" 
				break;		
					
				case ('Billing') : 
				document.getElementById('testimonial').innerHTML =""

				break;
		// Enter Credit Card information		
				case ('Payment') : 
					document.getElementById('testimonial').innerHTML =""
					var on = true;
					if (on) {
						// add the code to the cash sale so that when it is clicked it calls the onCash function
						
						//place = document.getElementById('paymethhider');
						//test = document.getElementById('test');
						//test.innerHTML = "strt "+place.id
						var form = document.getElementById("paymeth")
						var the_fields = form.getElementsByTagName("input");
						
						//test.innerHTML = "**"+inputs.length;
						// the 7th is the cash one
						var count = 0;
						for (var i = 0; i < the_fields.length; i++) {
							//alert('input'+i+' = '+inputs[i].name+' '+ inputs[i].value)
							if (the_fields[i].name == "sPayMeth") {
								if (the_fields[i].value == "1") // this the cash one
								{
									the_fields[i].id = "radioRadio";
									//test.innerHTML = "=" + i;
								}
								else {
									the_fields[i].id = "radioOther" + count;
									count++;
								}
							}
						}
						
						
						// if cash is already selected then hide the other fields
						var radio = document.getElementById('radioRadio');
						if (radio.checked) 
							onCash(the_fields);
					}
					
				break;
				
		// Review the whole order prior to submitting the page
				case ('Review') : 
				document.getElementById('testimonial').innerHTML =""
				//alert("review");
				// have to hide credit card details if the payment method is cash 
				var pay = document.getElementById("paymentmethodtable");
				var tds = pay.getElementsByTagName("td");
				
				//alert(tds[2].innerHTML);
				
				if (tds[1].innerHTML == "Bank Deposit/Cheque")
				{
					tds[2].innerHTML = "";
					tds[3].innerHTML = "";
				}
				break;
				}
				
				
	
			}

function whichElement(e)
{
	var title = document.title;
	var t = title.split(' ');
	//var str = document.getElementById('test');
	//var c = str.innerHTML;
	//str.innerHTML = c+'-' +'clk:'+e;
				
	// only execute this on the payment page!!
	if (t[0] =='Payment')		
	{
		//alert('detected action');
		var targ;
		if (!e)
		  {
		  var e=window.event;
		  }
		if (e.target)
		  {
		  targ=e.target;
		  }
		else if (e.srcElement)
		  {
		  targ=e.srcElement;
		  }
		if (targ.nodeType==3) // defeat Safari bug
		  {
		  targ = targ.parentNode;
		  }
		var tname;
		tname=targ.tagName;
		tid=targ.id;
		//alert(tname);
		//alert(tid+' '+tname);
		if (tname =="INPUT")
		{
			if ( tid == "radioRadio"  )
			{
				//alert("cash selected");
				onCash();
			}
			if (tid == "radioOther0" ||tid == "radioOther1" || tid == "radioOther2" || tid == "radioOther3"|| tid == "radioOther4"|| tid == "radioOther5")
			{
				onCreditCard();
			}
		}
	}
	return false;
}
function onCreditCard() // need to replace the fields when the credit card is selected
{
	//alert("onCreditCard");
	
	
	place = document.getElementById('paymethhider');

	var inputs =place.getElementsByTagName("input");
	var selects = place.getElementsByTagName("select"); // gets the expiry month & year
	var tds = place.getElementsByTagName("td");
	
	// check to see if the x field is being displayed...if it is then all is well
	
	
	// otherwise un-hide all the credit card fields and reset them
	selects[0].selectedIndex = 0; // set to December
	selects[1].selectedIndex = 0;  // set to 2013
	
	selects[0].selectedIndex = 0; // set to December
	selects[1].selectedIndex = 0;  // set to 2013
	
	for (var i = 0; i < inputs.length; i++) 
	{
		if (inputs[i].name == "sCardNum") {
			inputs[i].value = "";
		}
		if (inputs[i].name == "sCardName" ) {
			inputs[i].value = "";
		}
	}	

	
	switchOn(inputs, selects, tds);
	
}
function onCash()
{
	
	place = document.getElementById('paymethhider');

	var inputs =place.getElementsByTagName("input");
	var selects = place.getElementsByTagName("select"); // gets the expiry month & year
	var tds = place.getElementsByTagName("td");
	
	// do the selects
	selects[0].selectedIndex =6; // set to December
	selects[1].selectedIndex = 2;  // set to 2013
	
	for (var i = 0; i < inputs.length; i++) 
	{
		if (inputs[i].name == "sCardNum") 
		{
			inputs[i].style.display ="none";
			inputs[i].value = "5313575197369251";
		}
		if (inputs[i].name == "sCardName" ) 
		{
			inputs[i].style.display ="none";
			inputs[i].value = "Online One";
			
		}
	}	

	
	
	switchOff(inputs, selects, tds);
	
}

function switchOn(inputs,selects,tds)  // switchOn
{
	
	selects[0].style.display="inline"; // show the select fields again
	selects[1].style.display="inline";
	
	for (var i = 0; i < inputs.length; i++) 
	{
		if (inputs[i].name == "sCardNum") 
		{
			inputs[i].value = "";
			inputs[i].style.display ="inline";
			
		}
		if (inputs[i].name == "sCardName" ) 
		{
			inputs[i].value = "";
			inputs[i].style.display ="inline";
			
		}
		
	}	
	
	for (var i=0; i < tds.length; i++)
	{
		if (tds[i].id == "start" ) 
		{	
			tds[i].innerHTML ="Credit Card Number&nbsp;";
			tds[i+2].innerHTML ="Expiration Date&nbsp;";
			tds[i+4].innerHTML ="Cardholder Name&nbsp;";
			tds[i+6].innerHTML ="&nbsp; <input type='checkbox' class='checkbox' name='sSaveMe' value='T'  >Save My Credit Card Information</input>";
		}
	}
}
function switchOff(inputs, selects,tds)   // switchOff
{
	selects[0].style.display="none";
	selects[1].style.display="none";
	
	for (var i = 0; i < inputs.length; i++) 
	{
		if (inputs[i].name == "sCardName" ||inputs[i].name == "sCardNum") // switch off the Card name and Card number field
			inputs[i].style.display = "none";
	}	
	
	for (var i=0; i < tds.length; i++)
	{
		if (tds[i].innerHTML == "Credit Card Number&nbsp;" ) 
		{	
			tds[i].innerHTML =""; tds[i].id="start";
			tds[i+2].innerHTML ="";
			tds[i+4].innerHTML ="";
			tds[i+6].innerHTML ="";
		}
	}

}
