
var xmlHttp;
var color;
var zoom;
var modelSession;
var sizeSession;
var page;
var inputSession;
var inputChoiceSession;
var inputCategorySession;
var colorSession;
var amount;

zoom = "small";

function createXMLHttpRequest()
{
	if (window.ActiveXObject)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest();
	}
}



function changeImg(form)
{
	var url="model.php";
	
	if(document.getElementById('divOptions').style.visibility != 'hidden')
	{
	var color = document.getElementById("color").options[document.getElementById("color").selectedIndex].text;
	
	var text_color = document.getElementById("printcolor").options[document.getElementById("printcolor").selectedIndex].text;
	
		var font = document.getElementById("font").options[document.getElementById("font").selectedIndex].text;
	}
	
	if(document.getElementById('choice').value == 'portret')
	{
		var portret_id =  form.imageselect.options[form.imageselect.selectedIndex].value;
		
		url=url+"?category=portret&portret="+portret_id+"&color="+color+"&zoom="+zoom+"&show_zoom="+dressgoat.show_zoom.value;
		document.getElementById("gdimg").src=url;
	}
	else
	{
		var category = form.site_category.options[form.site_category.selectedIndex].text;
		
		if (form.inputtext.value)
		{
			var text = form.inputtext.value;
		}
		else if (form.inputselect.value != "")
		{
			var text = form.inputselect.options[form.inputselect.selectedIndex].text;
		}
		else
		{
			var text = "";
		}
		
		url=url+"?category="+escape(category)+"&text="+escape(text)+"&color="+color+"&text_color="+text_color+"&font="+font+"&zoom="+zoom+"&show_zoom="+dressgoat.show_zoom.value;
		document.getElementById("gdimg").src=url;
	}
}

function changeZoom()
{
	if (zoom == "large"){zoom = "small"}
	else if(zoom == "small") {zoom = "large"}
}

function changeSelectBoxDesign(id) {
  	createXMLHttpRequest();
	xmlHttp.onreadystatechange=processDesign;
	xmlHttp.open("GET","xml_output.php?action=design&id="+escape(id),true);
	xmlHttp.send(null);
}

function processDesign()
{
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{
		var xmlDoc=xmlHttp.responseXML;
		var designList=xmlDoc.getElementsByTagName("design");
		var selectObject=document.getElementById("inputselect");
		
		selectObject.options.length=0;
		
		selectObject.disabled=false;
		selectObject.options[0]=new Option("", "");
		
		var designIndexSession = "";
		for (var i=0; i<designList.length; i++)
		{
			selectObject.options[i+1]=new Option(designList[i].childNodes[1].firstChild.nodeValue, designList[i].childNodes[1].firstChild.nodeValue);
			if(designList[i].childNodes[1].firstChild.nodeValue==inputSession){
				designIndexSession=i+1;
			}		
		}	
		if(designIndexSession!=""){
			selectObject.selectedIndex=designIndexSession;
		}
	}
}

function changeSelectBoxSize(id) {
  	createXMLHttpRequest();
	xmlHttp.onreadystatechange=processSize;
	xmlHttp.open("GET","xml_output.php?action=size&id="+escape(id),true);
	xmlHttp.send(null);
}

function processSize()
{
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{
		var xmlDoc=xmlHttp.responseXML;
		var list=xmlDoc.getElementsByTagName("size");
		var selectObject=document.getElementById("size");
		
		selectObject.options.length=0;
		
		selectObject.disabled=false;
		selectObject.options[0]=new Option("", "");
		
		var sizeIndexSession = "";
		for (var i=0; i<list.length; i++)
		{
			selectObject.options[i+1]=new Option(list[i].childNodes[1].firstChild.nodeValue, list[i].childNodes[0].firstChild.nodeValue);
			if(list[i].childNodes[0].firstChild.nodeValue==sizeSession){
				sizeIndexSession=i+1;
			}
		}	
		if(sizeIndexSession!=""){
			selectObject.selectedIndex=sizeIndexSession;
		}
		
		if(sizeSession!="false" && sizeSession!="")
		{
			changeDimensionsShirt(modelSession, sizeSession);
		}
		else
		{
			changeProductInformation();
		}
	}
}

function changeDimensionsShirt(product_id, size_id) {
	if(size_id != "")
	{
	  	createXMLHttpRequest();
		xmlHttp.onreadystatechange=processDimensions;
		xmlHttp.open("GET","xml_output.php?action=dimension&id="+escape(product_id)+"&size_id="+escape(size_id),true);
		xmlHttp.send(null);
	}
}

function processDimensions()
{
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{
		var xmlDoc=xmlHttp.responseXML;
		var list=xmlDoc.getElementsByTagName("dimension");

		document.getElementById('span_shirt_width').innerHTML= list[0].childNodes[0].firstChild.nodeValue+" cm";
		document.getElementById('span_shirt_height').innerHTML= list[0].childNodes[1].firstChild.nodeValue+" cm";
		
		changeProductInformation();
	}
}

function emptyDimensions()
{
	document.getElementById('span_shirt_width').innerHTML="-- cm";
	document.getElementById('span_shirt_height').innerHTML="-- cm";
	sizeSession = "";
}

function changeProductInformation()
{
	createXMLHttpRequest();
	xmlHttp.onreadystatechange=fillProductInformation;
	
	xmlHttp.open("GET","price.php?action=product&id="+escape(document.getElementById("model").value)+"&amount="+escape(parseInt(document.getElementById("amount").value)), true);
	xmlHttp.send(null);
}

function fillProductInformation()
{			
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{		
		if(document.getElementById('choice').value == 'portret')
		{
			document.getElementById('span_label_text').innerHTML = document.getElementById("imageselect").options[document.getElementById("imageselect").selectedIndex].text;
		}
		else
		{
			var value;
	
			if (dressgoat.inputtext.value)
			{
				value = htmlspecialchars(dressgoat.inputtext.value);
			}
			else if (dressgoat.inputselect.value)
			{
				value = htmlspecialchars(dressgoat.inputselect.options[dressgoat.inputselect.selectedIndex].text);
			}
			else value = "?????";
				document.getElementById('span_label_text').innerHTML= document.getElementById('site_category').options[document.getElementById('site_category').selectedIndex].text+" "+value;
		}
		
		if(document.getElementById('divOptions').style.visibility != 'hidden')
		{
			document.getElementById('span_label_color').innerHTML=document.getElementById('color').options[document.getElementById('color').selectedIndex].text;
	
			document.getElementById('span_label_shirt_size').innerHTML= document.getElementById('size').options[document.getElementById('size').selectedIndex].text;
			
				if(document.getElementById('printcolor').options[document.getElementById('printcolor').selectedIndex].text)
				{
					document.getElementById('span_label_font').innerHTML= document.getElementById('font').options[document.getElementById('font').selectedIndex].text+" - "+document.getElementById('printcolor').options[document.getElementById('printcolor').selectedIndex].text;
				}
				else
				{
					document.getElementById('span_label_font').innerHTML= document.getElementById('font').options[document.getElementById('font').selectedIndex].text;
				}
		
			if(parseInt(document.getElementById("amount").value))
			{
				document.getElementById('span_label_amount').innerHTML= parseInt(document.getElementById("amount").value);
			}
			else
			{
				document.getElementById('span_label_amount').innerHTML = "";
			}
			
			if (value != "" && color != "" &&  document.getElementById("amount").value != "" && document.getElementById("amount").value > 0)
			{
				price = xmlHttp.responseText;
				if(price == Math.round(price))
				{
					document.getElementById('span_label_cost').innerHTML= "&euro; "+price+",- (inclusief verzendkosten)";
				}
				else
				{
					price = price * 100;
					price=price.toString();
					price = price.substring(0, price.length-2)+','+price.substring(price.length-2, price.length);
					document.getElementById('span_label_cost').innerHTML= "&euro; "+price+" (inclusief verzendkosten)";
				}
				
				document.getElementById('btnAddToCart').disabled = false;
				document.getElementById('btnCheckOut').disabled = false;
			}
			else
			{
				document.getElementById('span_label_cost').innerHTML= "";				
				document.getElementById('btnAddToCart').disabled = true;
				document.getElementById('btnCheckOut').disabled = true;
			}
		}
		
		
	 if(document.getElementById('color').value != "" && document.getElementById('size').value != "" && document.getElementById("amount").value != "" && document.getElementById("amount").value > 0)
	 {
			if(document.getElementById('choice').value == "text" && document.getElementById('font').value != "" && document.getElementById('printcolor').value != "" && document.getElementById('site_category').value != "")
			{
				document.getElementById('btnAddToCart').disabled = false;
				document.getElementById('btnCheckOut').disabled = false;
			}
			else if(document.getElementById('choice').value == "portret")
			{
				document.getElementById('btnAddToCart').disabled = false;
				document.getElementById('btnCheckOut').disabled = false;
			}
		}
	}
}


function sendMail(form) 
{
	recipients = form.recipients.value;
	sendername = form.sendername.value;
	senderemail = form.senderemail.value;
	subject = form.subject.value;
	message = form.message.value;
	//url = form.url.value;
	//alert(url);
	var queryString = "recipients="+recipients+"&sendername="+sendername+"&senderemail="+senderemail+"&subject="+subject+"&message="+message;
	
  	createXMLHttpRequest();
	xmlHttp.open("POST","sendmail.php?action=tellafriend",true);
	xmlHttp.onreadystatechange=processMail;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(queryString);
}

function processMail()
{
	document.getElementById('emailconfirm').style.display = '';
	document.getElementById('emailconfirm').innerHTML = "<img src=\"img/progress.gif\" alt=\"progress\">";
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{		
		mailstatus = xmlHttp.responseText;
		if (mailstatus == "mailsend")
		{
			//document.getElementById('emailconfirm').class = 'green';
			window.location.href = 'tellafriend.php?mail=send';
		}
		else if (mailstatus == "emailnotvalid")
		{
			document.getElementById('emailconfirm').style.color = '#FF0000';
			document.getElementById('emailconfirm').innerHTML = "Controleer geadresseerde(n)";
		}
		else if (mailstatus == "sendernotvalid")
		{
			document.getElementById('emailconfirm').style.color = '#FF0000';
			document.getElementById('emailconfirm').innerHTML = "Controleer uw naam en e-mail";
		}
		else if (mailstatus == "emptyfields")
		{
			document.getElementById('emailconfirm').style.color = '#FF0000';
			document.getElementById('emailconfirm').innerHTML = "Vul alle velden in";
		}
		else
		{
			document.getElementById('emailconfirm').style.color = '#FF0000';
			document.getElementById('emailconfirm').innerHTML = "Bericht niet verzonden";
		}
		
	}
}

function showdiv(divId) 
{ 
	if (document.getElementById) 
	{ // DOM3 = IE5, NS6 
		if(divId == "divAmount")
		{
			if(document.getElementById('color').value != "" && document.getElementById('size').value != "")
			{
				if(document.getElementById('font').value != "" && document.getElementById('printcolor').value != "")
				{
					document.getElementById(divId).style.visibility = 'visible';
				}
				else if(document.getElementById('choice').value == "portret")
				{
					document.getElementById(divId).style.visibility = 'visible'; 
				}
			}
		}
		else
		{
			document.getElementById(divId).style.visibility = 'visible'; 
		}
	} 
}

var firstClick = true;
function emptyOnFirstClick(input)
{
	if (firstClick == true)
	{
		document.getElementById(input).value = '';
		firstClick = false;
	}
}

function checkVariables(){
	if(page=="index")
	{		
		changeProductInformation();
		
		if(inputSession != "false" && inputSession != "" || inputTextSession != "false" && inputTextSession != "")
		{
			document.getElementById('divOptions').style.visibility = 'visible';
			var input = "input";
		}
		else
		{
			var input = "";
		}
		
		if(inputCategorySession != "false" && inputCategorySession != "")
		{
			document.getElementById('inputselect').disabled = false;
		}
		
		if(printSession == 'text')
		{
			changeInputChoice('text');
			showdiv('divPrint');
			
			if(input == "input" && colorSession != "false" && colorSession != "" && sizeSession != "false" && sizeSession != "" && fontSession != "false" && fontSession != "" && printColorSession != "false" && printColorSession != "")
			{
				document.getElementById('btnAddToCart').disabled = false;
				document.getElementById('btnCheckOut').disabled = false;
				showdiv('divAmount');
			}
		}
		else if(printSession == 'portret')
		{
			changeInputChoice('portret');
			showdiv('divPrint');
			
			if(colorSession != "false" && colorSession != "" && sizeSession != "false" && sizeSession != "")
			{
				document.getElementById('btnAddToCart').disabled = false;
				document.getElementById('btnCheckOut').disabled = false;
				showdiv('divAmount');
			}
		}
		
		if(sizeSession != "false" && sizeSession != "")
		{
			changeDimensionsShirt(modelSession, sizeSession);
		}
		
		changeImg(dressgoat);
		/*
		if(inputChoiceSession == "select")
		{
			document.getElementById('inputselect').disabled = false;
			changeInputChoice('select');
		}
		else
		{
			changeInputChoice('text');
		}
		
		if(inputSession != "false" && inputSession != "")
		{
			document.getElementById('divOptions').style.visibility = 'visible';
			
			if(colorSession != "false" && colorSession != "")
			{
				document.getElementById('divSize').style.visibility = 'visible';
				
				if(sizeSession != "false" && sizeSession != "")
				{
					document.getElementById('divAmount').style.visibility = 'visible';
				}
			}
		}
		*/
	}	
}

function changeShoppingCartAmount(rowId, operator)
{

	var spanName;
	
	spanName = "amountRow"+rowId;
	
	amount = document.getElementById(spanName).innerHTML;
	
	if (operator == 'minus')
	{
		if (amount > 1)
		{
			amount--;
		}
	}
	
	if (operator == 'plus')
	{
		amount++;
	}
	
	document.getElementById(spanName).innerHTML = amount;
	updateShoppingCartProduct(rowId, amount);
	changeShoppingCartPriceRow(rowId);
}

function changeShoppingCartPriceRow(rowId)
{
	var productTotal;
	var spanName;
	var productPrice;
	var extraCost;
	var giftCost;
	var discount;
	
	deleted = "deleted"+rowId;
	if (document.getElementById(deleted).value == 'false')
	{
	spanAmount = "amountRow"+rowId;
	amount = document.getElementById(spanAmount).innerHTML;

	spanName = "priceRow"+rowId;
	productPrice = parseFloat(document.getElementById('productPrice'+rowId).value);
	extraCost = parseFloat(document.getElementById('extraCost'+rowId).value);
	if (document.getElementById('checkGift'+rowId).checked == true)
	{
		giftCost = parseFloat(document.getElementById('giftCost'+rowId).value);
	}
	else
	{
		giftCost = parseFloat(0);
	}
	
	//pricePerProduct = productPrice + extraCost;
	productTotal = (productPrice + giftCost) * amount;
	productTotal = productTotal * 100;
	productTotal = productTotal.toString();
	productTotal = productTotal.substring(0, productTotal.length-2)+','+productTotal.substring(productTotal.length-2, productTotal.length);
	document.getElementById(spanName).innerHTML = productTotal;
	}

	//productTotal = totalAmount + giftCost;
	
	calculateShoppingCartDiscount(1);
	
}

function updateShoppingCartProduct(rowId, quantity) {
  	createXMLHttpRequest();
	//xmlHttp.onreadystatechange=processShoppingCart;
	idInput = "cartProductId"+rowId;
	shoppingCartId = document.getElementById(idInput).value;
	giftInput = "checkGift"+rowId;
	if (document.getElementById(giftInput).checked == true)
	{
		gift = '5';
	}
	else
	{
		gift = '6';
	}
	xmlHttp.open("GET","updateshoppingcart.php?action=update&id="+shoppingCartId+"&quantity="+quantity+"&gift="+gift,true);
	xmlHttp.send(null);
}

function deleteShoppingCartProduct(rowId) {
  	createXMLHttpRequest();
	//xmlHttp.onreadystatechange=processShoppingCart;
	idInput = "cartProductId"+rowId;
	shoppingCartId = document.getElementById(idInput).value;
	
	xmlHttp.open("GET","updateshoppingcart.php?action=delete&id="+shoppingCartId,true);
	xmlHttp.send(null);
	if (document.getElementById('deletedRows').value >= document.getElementById('amountProducts').value - 1)
	{
		window.location.href = 'shoppingcart.php';
	}
}

function changeShoppingCartTotalPrice()
{
	var i;
	var total = 0;
	var idRow;

	
	for (i = 0; i < document.getElementById('amountProducts').value; i++)
	{
		deleted = "deleted"+i;
		if (document.getElementById(deleted).value == 'false')
		{
			idRow = 'priceRow'+i;
			total = parseFloat(total) + parseFloat(document.getElementById(idRow).innerHTML);
		}
	}
	return total;
}



function calculateShoppingCartDiscount(id) {
  	createXMLHttpRequest();
	xmlHttp.onreadystatechange=processShoppingCartDiscount;
	xmlHttp.open("GET","xml_output.php?action=discount&id="+escape(id),true);
	xmlHttp.send(null);
}

function processShoppingCartDiscount()
{
	var discount_percentage = 0;
	var discount_total = 0;
	var totalAmount = 0;
	
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{
		var xmlDoc=xmlHttp.responseXML;
		var discountList=xmlDoc.getElementsByTagName("discount");

		for (var j = 0; j < document.getElementById('amountProducts').value; j++)
		{		
		
		deleted = "deleted"+j;
		if (document.getElementById(deleted).value == 'false')
			{
				for (var i=0; i<discountList.length; i++)
				{
					amountInput = "amountRow"+j;
					productPriceInput = "productPrice"+j;
					amount = document.getElementById(amountInput).innerHTML;				
					productPrice = document.getElementById(productPriceInput).value;
					
					totalAmount = amount * productPrice;
					if(parseInt(document.getElementById(amountInput).innerHTML) >= parseInt(discountList[i].childNodes[0].firstChild.nodeValue))
					{	
						discount_percentage = discountList[i].childNodes[1].firstChild.nodeValue;
					}
				}
				discount = totalAmount * discount_percentage / 100;
				discount_total = discount + discount_total;
			}
			else
			{
				//discount_total = 0;
			}
		}
		if(discount_total != 0)
		{
			discount_total = Math.round(discount_total * 100);
			discount_total = discount_total.toString();
			discount_total = discount_total.substring(0, discount_total.length-2)+','+discount_total.substring(discount_total.length-2, discount_total.length);
		}
		else
		{
			discount_total = "0,00";
		}
		document.getElementById('discountPrice').innerHTML = discount_total;
		
		total_price = (changeShoppingCartTotalPrice() - parseFloat(discount_total.replace(',', '.'))) * 100;
		if(isNaN(total_price))
		{
			total_price = "0,00";
		}
		else
		{
		total_price = total_price.toString();
		total_price = total_price.substring(0, total_price.length-2)+','+total_price.substring(total_price.length-2, total_price.length);
		}
		document.getElementById('totalPrice').innerHTML = total_price;
	}
}

function changeDeliveryStatus()
{
	if(document.getElementById('delivery').checked == true)
	{
		document.getElementById('divDelivery').style.visibility = 'visible';
		document.getElementById('btnConfirm').style.visibility = 'hidden';
	}
	else
	{
		document.getElementById('divDelivery').style.visibility = 'hidden';
		document.getElementById('btnConfirm').style.visibility = 'visible';
	}
}

function delRow(rowId, node)
{
	document.getElementById('deletedRows').value = parseInt(document.getElementById('deletedRows').value) + 1;
	//alert(document.getElementById('deletedRows').value+" "+document.getElementById('amountProducts').value);
	deleted = "deleted"+rowId;
	document.getElementById(deleted).value = 'true';
	
	var tr = node.parentNode;
	while (tr.tagName.toLowerCase() != "tr")
	tr = tr.parentNode;
	
	// Remove the tr node and all children.
	tr.parentNode.removeChild(tr);
	if (document.getElementById('deletedRows').value >= document.getElementById('amountProducts').value)
	{
		document.getElementById('btnCheckOut').disabled = true;
	}
}

function confirm_delete(id, node)
{
	input_box=confirm("Weet je zeker dat je dit artikel uit de winkelwagen wilt verwijderen?");
	
	if (input_box==true)
	{ 
		deleteShoppingCartProduct(id);
		delRow(id, node);
	}
}

function changeInputChoice(choice)
{
	if(choice == "text")
	{	
		document.getElementById('image1').style.display = 'none';
		document.getElementById('text1').style.display = '';
		document.getElementById('text2').style.display = '';
		document.getElementById('text3').style.display = '';
		document.getElementById('text4').style.display = '';
		document.getElementById('text5').style.display = '';
		document.getElementById('text6').style.display = '';
		document.getElementById('text7').style.display = '';
	}
	else
	{	
		document.getElementById('text1').style.display = 'none';
		document.getElementById('text2').style.display = 'none';
		document.getElementById('text3').style.display = 'none';
		document.getElementById('text4').style.display = 'none';
		document.getElementById('text5').style.display = 'none';
		document.getElementById('text6').style.display = 'none';
		document.getElementById('text7').style.display = 'none';
		document.getElementById('image1').style.display = '';
	}
}

function emptyFieldsOnChange(choice)
{
	if(choice == "portret")
	{	
		document.getElementById('imageselect').value = "";
		document.getElementById('color').value = "";
		document.getElementById('size').value = "";
		
		document.getElementById('divAmount').style.visibility = 'hidden';
	}
	else
	{
		document.getElementById('site_category').value = "";
		document.getElementById('inputselect').value = "";
		document.getElementById('inputtext').value = "";
		document.getElementById('imageselect').value = "";
		document.getElementById('color').value = "";
		document.getElementById('printcolor').value = "";
		document.getElementById('size').value = "";
		
		document.getElementById('divAmount').style.visibility = 'hidden';
		
		document.getElementById('inputselect').disabled = true;
	}
}

function emailCheck (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

function checkFieldsCheckOut(form)
{
	var emptyFields = 0;
	
	if(form.customer_firstname.value){document.getElementById('check_customer_firstname').style.visibility = 'hidden'}else{document.getElementById('check_customer_firstname').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_lastname.value){document.getElementById('check_customer_lastname').style.visibility = 'hidden'}else{document.getElementById('check_customer_lastname').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_street.value){document.getElementById('check_customer_street').style.visibility = 'hidden'}else{document.getElementById('check_customer_street').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_number.value){document.getElementById('check_customer_number').style.visibility = 'hidden'}else{document.getElementById('check_customer_number').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_postal_code.value){document.getElementById('check_customer_postal_code').style.visibility = 'hidden'}else{document.getElementById('check_customer_postal_code').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_place.value){document.getElementById('check_customer_place').style.visibility = 'hidden'}else{document.getElementById('check_customer_place').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_country.value){document.getElementById('check_customer_country').style.visibility = 'hidden'}else{document.getElementById('check_customer_country').style.visibility = 'visible'; emptyFields++;}
	if(form.customer_telephone.value){document.getElementById('check_customer_telephone').style.visibility = 'hidden'}else{document.getElementById('check_customer_telephone').style.visibility = 'visible'; emptyFields++;}
	if(emailCheck(form.customer_email.value)){document.getElementById('check_customer_email').style.visibility = 'hidden'}else{document.getElementById('check_customer_email').style.visibility = 'visible'; emptyFields++;}
	
	if(document.getElementById('delivery').checked == true)
	{
		if(form.delivery_name.value){document.getElementById('check_delivery_name').style.visibility = 'hidden'}else{document.getElementById('check_delivery_name').style.visibility = 'visible'; emptyFields++;}
		if(form.delivery_street.value){document.getElementById('check_delivery_street').style.visibility = 'hidden'}else{document.getElementById('check_delivery_street').style.visibility = 'visible'; emptyFields++;}
		if(form.delivery_number.value){document.getElementById('check_delivery_number').style.visibility = 'hidden'}else{document.getElementById('check_delivery_number').style.visibility = 'visible'; emptyFields++;}
		if(form.delivery_postal_code.value){document.getElementById('check_delivery_postal_code').style.visibility = 'hidden'}else{document.getElementById('check_delivery_postal_code').style.visibility = 'visible'; emptyFields++;}
		if(form.delivery_place.value){document.getElementById('check_delivery_place').style.visibility = 'hidden'}else{document.getElementById('check_delivery_place').style.visibility = 'visible'; emptyFields++;}
		if(form.delivery_country.value){document.getElementById('check_delivery_country').style.visibility = 'hidden'}else{document.getElementById('check_delivery_country').style.visibility = 'visible'; emptyFields++;}
	}
	
	if(emptyFields == 0)
	{
		form.submit();
	}
}

function htmlspecialchars(string) 
{ 
	string = string.replace(/&/g, '&amp;');
    string = string.replace(/</g, '&lt;'); 
    string = string.replace(/>/g, '&gt;'); 
    string = string.replace(/"/g, '&quot;'); 
    string = string.replace(/'/g, '&apos;'); 
    return string; 
}