function switchCountry(el)
{
	if (document.getElementById('Country'))
	{
//		document.getElementById('Country').value = el.options[el.selectedIndex].innerHTML;
		if (document.getElementById('DeliveryCountry'))
			document.getElementById('DeliveryCountry').value = el.options[el.selectedIndex].innerHTML;
	}

	var webshop = document.getElementById('webshop');
	webshop.options.length = 0;
	
	var sel = el.selectedIndex;
	var selVal = el.options[el.selectedIndex].value;
	var j = 0;
	if (webshops[selVal])
	{
		document.getElementById('webshop_a').className = 'webshopShow';
		document.getElementById('webshop_b').className = 'webshhpShow';
		for (var i in webshops[selVal])
			webshop[j++] = new Option(webshops[selVal][i], i);
	}
	else
	{
		document.getElementById('webshop_a').className = 'webshopHidden';
		document.getElementById('webshop_b').className = 'webshopHidden';
		webshop[j++] = new Option('no webshops in this country', 0);
	}
	if (selVal == 'NL')
	{
		if (document.getElementById('foreignCountryContainer'))
			document.getElementById('foreignCountryContainer').style.display = 'none';
	}
	else	/* not NL, so foreign country */
	{
		if (document.getElementById('foreignCountryContainer'))
			document.getElementById('foreignCountryContainer').style.display = 'block';
	}
}

function getFormElementByName(name, frm)
{
	if (typeof(frm) == 'undefined')
		frm = document.getElementById('cartForm');

	if (name == 'Company')
		name = 'Name';
	for (var i=0; i<frm.elements.length; i++)
	{
		if (frm.elements[i].name == name)
			return frm.elements[i];
	}
//	alert('didnt find '+name);
}

function checkCountries()
{
	return document.getElementById('Country').options[document.getElementById('Country').selectedIndex].value == getFormElementByName('DeliveryCountry').value;
}

function checkDelivery()
{
	var frm = document.getElementById('cartForm');
	var inputArr = frm.getElementsByTagName('INPUT');
	if (document.getElementById('deliveryMirror').checked)
	{
		if (document.getElementById('Country').selectedIndex != 0 && !checkCountries())
		{
			alert('Country should be the same as Delivery Country.\nTo change \'Delivery Country\', please click the change button at the top of the page');
			document.getElementById('deliveryMirror').checked = false;
			return;
		}
		for (var i=0; i<inputArr.length; i++)
		{
			if (inputArr[i].name.substr(0, 8) == 'Delivery' && inputArr[i].name != 'DeliveryCountry')
			{
				var mirrorEl = getFormElementByName(inputArr[i].name.substr(8), frm);
				inputArr[i].value = mirrorEl.value;
				inputArr[i].disabled = 'disable';
			}
		}
	}
	else
	{
		for (var i=0; i<inputArr.length; i++)
		{
			if (inputArr[i].name.substr(0, 8) == 'Delivery' && inputArr[i].name != 'DeliveryCountry')
			{
				inputArr[i].value = '';
				inputArr[i].disabled = false;
			}
		}

	}
}

function copyToDelivery(field)
{
	if (document.getElementById('deliveryMirror').checked)
	{
		if (field == 'Country')
		{
			if (document.getElementById('Country').selectedIndex != 0 && !checkCountries())
			{
				alert('Country should be the same as Delivery Country.\nTo change \'Delivery Country\', please click the change button at the top of the page\n\nIf the Delivery Address is not the same as the Customer Details, please uncheck the checkbox at the bottom of the Delivery Address column');
				document.getElementById('Country').selectedIndex = 0;
			}
		}
		else
		{
			var deliveryField = field == 'Name' ? 'DeliveryCompany' : 'Delivery'+field;
			getFormElementByName(deliveryField).value = getFormElementByName(field).value;
		}
	}
}

/***
 * following functions are used to create the summary
 */
function validateForm(manFields)
{
	// checking mandatory fields
	for (var i in manFields)
	{
		if (typeof(document.getElementById('cartForm')[i]) == 'undefined')
			alert(i+'doesnt exist');
		else if (document.getElementById('cartForm')[i].value == '')
		{
			alert(manFields[i]+' is a mandatory field');
			document.getElementById('cartForm')[i].focus();
			return false;
		}
	}

	// checking mandatory checkboxes
	if (!document.getElementById('agrees2Terms').checked)
	{
		alert('You have to indicate that \'you have read and agree with the TERMS & CONDITIONS\'');
		return false;
	}
	return true;
}
function appendValue(node, val)
{
	var parent = node.parentNode;
	var textEl = document.createTextNode(val);
	parent.appendChild(textEl);
}
function convertFormElements(frm)
{
	// converting all input.text and input.checkbox elements
	var inputArr = frm.getElementsByTagName('INPUT');
	for (var i=0; i<inputArr.length; i++)
	{
		if (inputArr[i].type == 'text')
		{
			inputArr[i].style.borderColor = 'white';
		}
		if (inputArr[i].type == 'checkbox')
		{
			inputArr[i].style.display = 'none';
			var spanNode = inputArr[i].parentNode.getElementsByTagName('SPAN')[0];
			if(spanNode && inputArr[i].name != 'foreignCompany')
			{
				if (inputArr[i].checked)
				{
					spanNode.innerHTML = 'Yes, '+spanNode.innerHTML;
				}
				else
				{
					spanNode.style.textDecoration = 'line-through';
				}
			}
		}
	}

	// converting all select elements
	var selectArr = frm.getElementsByTagName('SELECT');
	for (var i=0; i<selectArr.length; i++)
	{
		selectArr[i].style.display = 'none';
		appendValue(selectArr[i], selectArr[i].options[selectArr[i].selectedIndex].value);
	}

	// converting all textarea elements
	var taArr = frm.getElementsByTagName('TEXTAREA');
	for (var i=0; i<taArr.length; i++)
	{
		taArr[i].style.display = 'none';
		appendValue(taArr[i], taArr[i].value);
	}

	// summary created, now we display the proceed button
	document.getElementById('proceedButton').style.display = 'inline';
	document.getElementById('summaryButton').style.display = 'none';
	// and we change the steps-image
	document.getElementById('steps').src = document.getElementById('steps').src.replace(/stap3/, 'stap4');
}
function initCheckout()
{
	// if true, we're on the right page
	if (document.getElementById('proceedButton'))
	{
		// we hide the proceed button and display the summary button
		document.getElementById('proceedButton').style.display = 'none';
		document.getElementById('summaryButton').style.display = 'inline';

		// we copy all the values from normal to delivery if necessary
		// and set onchange event
		var normalFields = ['Name', 'FirstName', 'LastName', 'Address', 'Address2', 'ZipCode', 'City', 'County', 'Country', 'Phone'];
		for (var i in normalFields)
		{
			copyToDelivery(normalFields[i]);

			var el = getFormElementByName(normalFields[i]);
			el.onchange = function () {
				copyToDelivery(this.name);
			}
		}
		checkDelivery();
	}
}

/***
 * following function is used when we click proceed
 */
function BlockForm()
{
	var ErrorMsg = "";
	var form = self.document.forms[0];
	if (form.ButCheckOut.value != 'Sending...')
	{
		form.ButCheckOut.value = 'Sending...';
	}
	else
	{
		alert("One moment please ...");
		return false;
	}
}

/**
 * initiating page logic
 */
if (window.addEventListener)
{
	window.addEventListener('load', initCheckout, false);
}
else
{
	window.attachEvent('onload', initCheckout);
}