/*
 * Loads the customer user information
 * for the WSNavLoginUser component.
 */
function loadUserInfo(id, id2, url) {
	if(id != '' && url != '') {
		var ajax = new Ajax.Request(
		url,
		{
			method: 'get',
			onSuccess: function(transport){xmlPopulate(transport, id, id2);},
			onFailure:function(transport){alert('An error occured during the result set retrieval. HTTP Status: ' + transport.status)}
		}
		);
	}
	else if(id == '') {
		alert('No ID provided.');
	}
}

/*
 * Displays the customer information
 * in the WSNavLoginUser component.
 */
function xmlPopulate(transport, id, id2) {
	var xmlDoc = transport.responseXML;
	if(document.getElementById(id)) {
		var output = '<b>';
		if(xmlDoc.getElementsByTagName("customername1")[0].childNodes[0])
			output+=xmlDoc.getElementsByTagName("customername1")[0].childNodes[0].nodeValue;
		if(xmlDoc.getElementsByTagName("customername2")[0].childNodes[0])
			output+=" " + xmlDoc.getElementsByTagName("customername2")[0].childNodes[0].nodeValue;
		output+='</b>';
		document.getElementById(id).innerHTML = output;
	}

	if(document.getElementById(id2)) {
		document.getElementById(id2).innerHTML = xmlDoc.getElementsByTagName("customernumber")[0].childNodes[0].nodeValue;
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(/; */);
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		if (c.substring(0, nameEQ.length) == nameEQ) {
			return c.substring(nameEQ.length);
		}
	}
	return null;
}

function deleteCookie(name, path) {
    document.cookie = name + "=" + (path ? "; path=" + path : "");
}

/**
 * Displays any errors in
 * the general WSErrors div element.
 *
 * @param type
 * @param message
 */
function showError(type, message){
	var errorDiv = document.createElement("div");
	var classAttribute = document.createAttribute("class");
	classAttribute.nodeValue = type;
	errorDiv.setAttributeNode(classAttribute);
	errorDiv.innerHTML = message;
	document.getElementById("WSErrors").appendChild(errorDiv);
}

/**
 * Checks wether the validation of the form
 * should be started or not.
 * If a user presses the 'back'-button, then
 * the form validation must not be executed.
 *
 * @param submitButtonId
 * @param formValidationObject
 * @return boolean True if the form shouldn't be validated or the form validation has passed correctly
 */
function doFormValidation(submitButtonId, formValidationObject) {
	if (submitButtonId == 0) {
		return true;
	} else if (submitButtonId == 1) {
		return formValidationObject.validate();
	} else {
		return false;
	}
}
