//variable to hold all error messages....
var errorMessages = "";
//Front end Java Script File
function validateRegistrationFormElements(oForm,errorMsgBox,whichForm)
{
	errorMessages = "";
	//make an array of all the mandatory element ids
		var formIds = Array();
		formIds[0]= 'customer_firstname';
		formIds[1]= 'customer_lastname';
		formIds[2]= 'customer_billing_street_name';
		formIds[3]= 'customer_billing_house_num';
		formIds[4]= 'customer_billing_zipcode';
		formIds[5]= 'customer_billing_city';
		formIds[6]= 'customer_dob';
		formIds[7]= 'customer_username';
	//if the form is edit then don't check for password...
		if(whichForm!='edit')
		{
		formIds[8]='customer_password';
		}
	var requiredFields = "";
	//loop through all the elements to check if all the mandatory fields are filled in
		for(var i=0;i<formIds.length;i++)
		 {
			if($(formIds[i]).value=="")
			{
				$(formIds[i]).style.borderColor = "#ff6600";
				//if the requiredFields error variable is empty then set it
				if(requiredFields=="")
				{
					requiredFields = errorMessage;
				}
			}
			else
			{
				$(formIds[i]).style.borderColor = "#949494";
			}
		 }

	if(whichForm!='edit')
	{
		//check if the email entered is valid of not
		var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
	
		//proceed ondly if email is entered..
		if(document.getElementById("customer_username").value!="")
		{
			if(!document.getElementById("customer_username").value.toUpperCase().match(regEmail))
			{
				errorMessages = errorEmail;
				document.getElementById("customer_username").style.borderColor = "#ff6600";
			}
			else
			{
				document.getElementById("customer_username").style.borderColor = "#949494";
				//if entered a valid email, check for uniqueness
				checkUserName(document.getElementById("customer_username").value,errorMsgBox,'submit');
			}
		}
	}
	
	//if all the mandatory fields are filled in submit the form 
	if(requiredFields=="" && errorMessages =="")
	{
		return true;
	}
	//else do not submit the form
	else
	{
		//display the errors
		document.getElementById(errorMsgBox).innerHTML = errorMessages+requiredFields;

		document.getElementById(errorMsgBox).style.display = "block";
		
		return false;
	}
}

//check the uniqueness of the e-mail entered...
function checkUserName(username,errorMsgBox,type)
{
	errorMessages = "";
	//alert(username+"+"+type);
	var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
	//proceed only if the username is not equal to null..
	if(username!="")
	{	//check if the username entered is a valid email...
		if(username.toUpperCase().match(regEmail))
		{
			//send the request
			new Ajax("../_includes/validate.php",
			 {
				 method:'post',
				 data: {email:username},
				 onComplete: function(response)
				 {
					//check if the mail sent successfully.. and display messages accordingly..
					if(response==true)
					{
						document.getElementById("customer_username").style.borderColor = "#ff6600";
						errorMessages += errorEmailExists;
					}
					else
					{
						document.getElementById("customer_username").style.borderColor = "#949494";
					}
				 }
			}).request();
		}
		//append the error messages only if the request is from on key-up...
		else if(type=='form')
		{
			errorMessages += errorEmail;
			document.getElementById("customer_username").style.borderColor = "#ff6600";
		}
	}
	else if(type=='form')
	{
		errorMessages += errorEmailEmpty;
		document.getElementById("customer_username").style.borderColor = "#ff6600";
	}

//display error messages here if the check is from form on key up...
	if(errorMessages!="" && type=='form')
	{
		document.getElementById(errorMsgBox).innerHTML = errorMessages;
	
		document.getElementById(errorMsgBox).style.display = "block";
	
	}

}

//when checkout is clicked on the mijn-tas page the shipping address is showed....
function showDeliveryBox()
{
	document.getElementById("shop-delivery-address-box").style.display = "block";
}

//This function is defined to use for the forms, when focus is set on any field it will take the default text if any...
//this will take form element reference as the only parameter....
//internally we have to provide the default text for any particular text field..... 
function setFieldValue(el,value)
{
	elId = el.id;
	elValue = el.value;
	//alert(elId+" "+elValue);
//if id = friend-name
	if(elId=="friend_name" && elValue == value)
	{
		el.value = "";
	}
//if id = friend-mail	
	if(elId=="friend_mail" && elValue == value)
	{
		el.value = "";
	}
//if id = customer-name	
	if(elId=="customer_name" && elValue == value)
	{
		el.value = "";
	}
//if id = customer-username
	if(elId=="customer_username" && elValue == value)
	{
		el.value = "";
	}
}


function getQuantity(product_id,qty)
	{
		//alert(product_id+" "+qty);
		document.getElementById("updateQuantity").value = qty;
		document.getElementById("updateProductId").value = product_id;
		document.getElementById('overlay_container').style.visibility = 'visible';
	}
	
/*Key board monitor function that closes the overlays if at all any is open */
document.onkeydown = function(oEvent)
{
	oEvent = oEvent || window.event;
	keyPressedCode = oEvent.keyCode;
	if(keyPressedCode==27)
	{
		$$('.overlay_bg_container').each(function(el){ $(el.id).style.visibility = 'hidden'; });
		//overlay_container
		$$('.overlay_container').each(function(el){ $(el.id).style.visibility = 'hidden'; });
	}
}

function getOverlay(id)
{
	if(window.navigator.appName == 'Microsoft Internet Explorer')
	{
		$(id).style.background = '#707070';
		$(id).style.color = '#fff';
	}
}

function removeOverlay(id)
{
	if(window.navigator.appName == 'Microsoft Internet Explorer')
	{
		$(id).style.background = '#f0f0f0';
		$(id).style.color = '#707070';
	}
}

function getMeasure(id)
{
	$(id).style.visibility = 'visible';
	if(window.navigator.appName == 'Microsoft Internet Explorer')
	{
		var selectArr = document.getElementsByTagName('select');
		for(var i=0; i<selectArr.length; i++)
		{
			document.getElementById(selectArr[i].id).style.visibility = 'hidden';
		}
	}
}

function removeMeasure(id)
{
	$(id).style.visibility = 'hidden';
	if(window.navigator.appName == 'Microsoft Internet Explorer')
	{
		var selectArr = document.getElementsByTagName('select');
		for(var i=0; i<selectArr.length; i++)
		{
			document.getElementById(selectArr[i].id).style.visibility = 'visible';
		}
	}
}

function changeClass(element,setClass)
	{
	element.className = setClass;
	}
/*
GIVEN THE ID OF THE ELEMENT, THIS FUNCTION SETS ITS DISPLAY TO NONE
	*/
function hideElement(elId)
	{
		document.getElementById(elId).style.display="none";
	}
/*
GIVEN THE ID OF THE ELEMENT, THIS FUNCTION SETS ITS DISPLAY TO BLOCK
	*/
function showElement(elId)
	{
		document.getElementById(elId).style.display="block";	
	}
//browser detectio script
<!--
/*var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

window.onload = function()
{
	//var browserName=navigator.appName; 
	//document.write('<p class="accent">You\'re using ' + BrowserDetect.browser + ' ' + BrowserDetect.version + ' on ' + BrowserDetect.OS + '!</p>');
	var browserName = BrowserDetect.browser;
	//alert(browserName);
}*/
