function PRIVCredentialsCheck(language)
{
	if ($('field1').value.length == 0 ||
	    $('field2').value.length == 0)
	    return false;
	    
	if (isNaN(language))
		language = 1;

	var postBody = "license="+encodeURIComponent($('field1').value)+"&key="+encodeURIComponent($('field2').value)+"&language="+language;
	
	if ($('internalLoginState').checked)
		postBody += "&internalLoginState=TRUE";
		
	new Ajax.Request(
		"/home/exec/ajax/credentials.php", {
		    postBody: postBody,
			method: "POST",
			onSuccess: function(h) {
				var doc = h.responseXML;
				var message = doc.getElementsByTagName("message")[0].firstChild.nodeValue;
				var id  = new Number(
				   parseInt(doc.getElementsByTagName("code")[0].firstChild.nodeValue)
				);
				
				if (id == 1)
					top.location = '/home/extranet/start.html';
				else
				{
					var obj = $('loginMessage');	
					obj.innerHTML = message;
					Element.setStyle(obj, {display:'block'});
					$("field1").value = $("field2").value = "";
				}
			}
		}
	);
	
	return OK;
}

function CheckEmail(email)
{
	return email.length >= 6 && /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/.test(email);
}

function PRIVCredentialsRegister(_this)
{
	if (!encodeURIComponent)
	{
		alert("Please update your browser.\nIt does not support encodeURIComponent.");
		return OK;
	}

	// POST-body
	var body = new String();
	
	// All required fields, and their exception arrows
	var errors = {
		name: 'arrowName',
		adress: 'arrowAdress',
		zipcode: 'arrowZipcode',
		locality: 'arrowLocality',
		country: 'arrowCountry',
		company: 'arrowCompany',
		registrationNumber: 'arrowRegistrationNumber',
		phone: 'arrowPhone',
		email: 'arrowEmail',
//		securityCode: 'arrowSecurityCode',
		branch: 'arrowBranch',
		exceptionMessage: 'errorReqMsgException',
//		securityCodeExcp: 'errorSecCodeException'
	};
	
	// Reset all error messages
	$(errors.name, errors.adress, errors.zipcode, errors.country, errors.company, errors.registrationNumber, 
	  errors.phone, errors.email, /*errors.securityCode,*/ errors.branch, errors.exceptionMessage, /*errors.securityCodeExcp,*/
	  errors.locality).invoke("hide");
	
	// This array will contain all erroneous fields
	var error = new Array();

	if (_this.name.value.length < 3)
	{
		error[0] = errors.name;
	}
	body = "name=" + encodeURIComponent(_this.name.value);
	
	if (_this.adress.value.length < 6)
	{
		error[error.length] = errors.adress;
	}
	body += "&adress=" + encodeURIComponent(_this.adress.value);
	
	if (!_this.zipcode.value.length)
	{
		error[error.length] = errors.zipcode;
	}
	body += "&zipcode=" + encodeURIComponent(_this.zipcode.value);
	
	if (_this.locality.value.length < 2)
	{
		error[error.length] = errors.locality;
	}
	body += "&locality=" + encodeURIComponent(_this.locality.value);
	
	if (_this.country.value.length < 3)
	{
		error[error.length] = errors.country;
	}
	body += "&country=" + encodeURIComponent(_this.country.value);
	
	if (_this.company.value.length < 2)
	{
		error[error.length] = errors.company;
	}
	body += "&company=" + encodeURIComponent(_this.company.value);
	
	/*if (_this.registrationNumber.value.length < 8)
	{
		error[error.length] = errors.registrationNumber;
	}*/
	body += "&registrationNumber=" + encodeURIComponent(_this.registrationNumber.value);
	
	if (_this.phone.value.length < 8)
	{
		error[error.length] = errors.phone;
	}
	body += "&phone=" + encodeURIComponent(_this.phone.value);

	// Regular expressions to distinguish wrongly formatted e-mail addresses.
	if (!CheckEmail(_this.email.value))
	{
		error[error.length] = errors.email;
	}
	body += "&email=" + encodeURIComponent(_this.email.value);
	
	body += "&branch=";
	
	if (!_this.branch.length)
	{
		if (!_this.branch.checked)
		{
			error[error.length] = errors.branch;
		}
		body += _this.branch.value;
	}
	else
	{
		var j = 0;
		for (var i = 0; i < _this.branch.length; i++)
		{
			if (_this.branch[i].checked)
			{
				j++;
			
				if (j == 1)
					body += _this.branch[i].value;
				else
				body += "," + _this.branch[i].value;
			}
		}	
	
		if (!j)
		{
			error[error.length] = errors.branch;
		}
	}
		
	if (error.length > 0)
	{
		$(errors.exceptionMessage).show();
		for (var i = 0; i < error.length; i++)
		{
			$(error[i]).show();
		}
		
		return OK;
	}
	
	/*if (_this.securityCode.value.length != 6 || isNaN(_this.securityCode.value))
	{
		$(errors.securityCodeExcp).show();
		$(errors.securityCode).show();
		return false;
	}
	body += "&securityCode=" + _this.securityCode.value;
	*/
	if (isNaN(_this.language.value)) // <~~~ just in case
	{
		return OK;
	}

	body += "&website=" + encodeURIComponent(_this.website.value) + "&misc=" + encodeURIComponent(_this.misc.value) + "&fax=" + encodeURIComponent(_this.fax.value) + "&language=" + _this.language.value;

	new Ajax.Request( 
	    "/home/exec/ajax/credentials.register.php?language=" + _this.language.value, {
	    	postBody: body,
	    	method: "post",
	    	onSuccess: function(h) 
	    	{
	    		var response = h.responseXML.getElementsByTagName("message");
	    		
	    		if (!response.length)
	    		{
	    			alert("Invalid response. Please contact Scandi Facade administrators.");
	    		}
	    		else
	    		{
	    			switch (parseInt(response[0].getAttribute("id")))
	    			{
	    				case 1:
	    					top.location.reload();
	    					break;
	    				case 2:
	    					$('errorSecCodeException').show();
	    					break;
	    				case 3: // questionable?
	    				case 4:
	    					$('errorReqMsgException').show();
	    					
	    					if (response[0].getAttribute("field"))
	    					{
	    						var id = new String( response[0].getAttribute("field") );
	    						id = "arrow" + id.substr(0,1).toUpperCase() + id.substr(1);
	    						var obj = $(id);
	    						if (obj)
	    							Element.show(obj);
	    					}
	    					break;
	    				case 5:
	    					$('errorQuotaExceeded').show();
	    					break;
	    				case 6:
	    					$('errorEmailAlreadyInUse').show();
	    					$('arrowEmail').show();
	    					break;
	    				case 7:
	    					top.location.replace("/home/page-" + response[0].getAttribute("goto") + ".html");
	    					break;
	    				default:
	    			}
	    		}
	    	},
	    	onFailure: function(h) 
	    	{
	    		alert(h.statusText);
	    	}
	    }
	);
	
	return OK;
}

function PRIVResetPassword(_this, string_ref)
{
	if (string_ref && !confirm(string_ref))
		return;
	
	$("errorInvalidEmail", "errorActCodeExists", "success").invoke("hide");

	new Ajax.Request(
	    "/home/exec/user.password.php", {
	        postBody: "email=" + encodeURIComponent(_this.email.value),
	        method: "post",
	        onSuccess: function(h) {
	        	var response = h.responseXML.getElementsByTagName("response")[0];
	        	
	        	switch (parseInt(response.firstChild.nodeValue))
	        	{
	        		case 0:
	        			$("errorInvalidEmail").show();
	        			break;
	        		case 1:
	        			$("errorActCodeExists").show();
	        			break;
	        		case 2:
	        			$("success").show();
	        			break;
	        	}
	        },
	        onFailure: function(h) {alert(h.status);}
	    }
	);
	
	return OK;
}

function PRIVNewsletterRecipientRegistration(email)
{
	if (!CheckEmail(email))
	{
		$('newsletterMessage').setStyle({display: 'block'});
		return OK;
	}

	new Ajax.Request(
		"/home/exec/ajax/newsletter.register.php", {
			postBody: "email=" + encodeURIComponent(email),
			method: "post",
			onSuccess: function (h) {
				$('newsletterMessage').update(h.responseText).setStyle({display: 'block'});
			}
		}
	);

	return OK;
}
