// error message of request form
var error_title = "ERROR : Please select the title";
var error_firstname = "ERROR : Please enter your firstname";
var error_lastname = "ERROR : Please enter your lastname";
var error_email = "ERROR : Please enter your email";
var error_email_format = "ERROR : Please enter a valid Email address";
var error_language = "ERROR : Please enter your language";
var error_position = "ERROR : Please enter your position/occupation";
var error_compname = "ERROR : Please enter your company name";
var error_street = "ERROR : Please enter your street and number";
var error_zipcode = "ERROR : Please enter your zipcode";
var error_city = "ERROR : Please enter your city";
var error_country = "ERROR : Please select your country";
var error_countrycode = "ERROR : Please select your country code";
var error_countrycodefax = "ERROR : Please select your country code of fax";
var error_phonenr = "ERROR : Please enter your phone number";
var error_faxnr = "ERROR : Please enter your fax number";
var error_vatnr = "ERROR : Please enter your vat number";
var error_kvknr = "ERROR : Please enter your registration number of the chamber of commerce";
var error_businessline = "ERROR : Please enter your business line";

// error message of login
var error_username = "ERROR : Please enter your username";
var error_password = "ERROR : Please enter your password";

// error message when change password
var error_newpassword = "ERROR : Please enter your new password";
var error_confirmpassword = "ERROR : Please confirm your password";
var error_password_char = "ERROR : Your password needs to be at least 6 characters long and maximum 8 characters long.";
var error_not_match = "ERROR : Your password is not match";

// error message for remove current order
var confirm_remove_order = "You really want to remove this selected items?";

function Trim(s){var temp = " ";var i = 0;while ((temp == " ") && (i <= s.length)) {temp = s.charAt(i);i++;}s = s.substring(i - 1, s.length);return(s);}

function IsEmpty(s){if (Trim(s) == ""){return(true);} else {return(false);}}

function IsEmail(email) {var pos;email = Trim(email);pos = email.indexOf("@");if ((pos < 3) || (email.indexOf(".", pos + 2) == -1)) {return(false);} else {return(true);}}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// clear default value from field when selected
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
} 



// Function to check input value from request
function checkRequest () 
{
	if (IsEmpty(document.request.firstName.value)) {
		alert(error_firstname);
		document.request.firstName.focus();	
	} else if (IsEmpty(document.request.lastName.value)) {
		alert(error_lastname);
		document.request.lastName.focus();
	} else if (IsEmpty(document.request.emailAddress.value)) {
		alert(error_email);
		document.request.emailAddress.focus();	
	} else if (!IsEmail(document.request.emailAddress.value)) {
		alert(error_email_format);
		document.request.emailAddress.focus();	
	} else if (IsEmpty(document.request.position.value)) {
		alert(error_position);
		document.request.position.focus();
	} else if (IsEmpty(document.request.compName.value)) {
		alert(error_compname);
		document.request.compName.focus();
	} else if (IsEmpty(document.request.street.value)) {
		alert(error_street);
		document.request.street.focus();
	} else if (IsEmpty(document.request.zipcode.value)) {
		alert(error_zipcode);
		document.request.zipcode.focus();
	} else if (IsEmpty(document.request.city.value)) {
		alert(error_city);
		document.request.city.focus();
	} else if (document.request.countryID.value == "-1") {
		alert(error_country);
		document.request.countryID.focus();
	} else if (document.request.countryCodeComp.value == "-1") {
		alert(error_countrycode);
		document.request.countryCodeComp.focus();
	} else if (IsEmpty(document.request.phonenrComp.value)) {
		alert(error_phonenr);
		document.request.phonenrComp.focus();
	} else if (IsEmpty(document.request.vatnr.value)) {
		alert(error_vatnr);
		document.request.vatnr.focus();
	} else {
		self.document.request.submit();
	}
}

function checkLogin () {
	if (IsEmpty(document.login.username.value)) {
		alert(error_username);
		document.login.username.focus();	
	} else if (document.login.username.value == "enter e-mail address") {
		alert(error_username);
		document.login.username.focus();	
	} else if (!IsEmail(document.login.username.value)) {
		alert(error_email_format);
		document.login.username.focus();	
	} else if (IsEmpty(document.login.password.value)) {
		alert(error_password);
		document.login.password.focus();
	} else if (document.login.password.value == "enter password") {
		alert(error_password);
		document.login.password.focus();
	} else {
		self.document.login.submit();
	}
}

// Function to check input value from edit profile - admin
function editRequest() 
{
	if (IsEmpty(document.edit_profile.firstName.value)) {
		alert(error_firstname);
		document.edit_profile.firstName.focus();	
	} else if (IsEmpty(document.edit_profile.lastName.value)) {
		alert(error_lastname);
		document.edit_profile.lastName.focus();	
	} else if (IsEmpty(document.edit_profile.position.value)) {
		alert(error_position);
		document.edit_profile.position.focus();
	} else if ((!IsEmpty(document.edit_profile.newPassword.value)) && (!IsEmpty(document.edit_profile.confirmPassword.value))) {
		// check password
		if ((document.edit_profile.newPassword.value.length < 6) || (document.edit_profile.newPassword.value.length > 10)) {
			alert(error_password_char);
			document.edit_profile.newPassword.focus();	
		} else if ((document.edit_profile.confirmPassword.value.length < 6) || (document.edit_profile.confirmPassword.value.length > 10)) {
			alert(error_password_char);
			document.edit_profile.confirmPassword.focus();	
		} else if (document.edit_profile.newPassword.value != document.edit_profile.confirmPassword.value) {
			alert(error_not_match);
			document.edit_profile.confirmPassword.focus();	
		} else {
			if (IsEmpty(document.edit_profile.compName.value)) {
				alert(error_compname);
				document.edit_profile.compName.focus();
			} else if (IsEmpty(document.edit_profile.street.value)) {
				alert(error_street);
				document.edit_profile.street.focus();
			} else if (IsEmpty(document.edit_profile.zipcode.value)) {
				alert(error_zipcode);
				document.edit_profile.zipcode.focus();
			} else if (IsEmpty(document.edit_profile.city.value)) {
				alert(error_city);
				document.edit_profile.city.focus();
			} else if (document.edit_profile.countryID.value == "-1") {
				alert(error_country);
				document.edit_profile.countryID.focus();
			} else if (document.edit_profile.countryCodeComp.value == "-1") {
				alert(error_countrycode);
				document.edit_profile.countryCodeComp.focus();
			} else if (IsEmpty(document.edit_profile.phonenrComp.value)) {
				alert(error_phonenr);
				document.edit_profile.phonenrComp.focus();
			} else if (IsEmpty(document.edit_profile.vatnr.value)) {
				alert(error_vatnr);
				document.edit_profile.vatnr.focus();
			} else {
				self.document.edit_profile.submit();
			}
		}
	} else if (IsEmpty(document.edit_profile.compName.value)) {
		alert(error_compname);
		document.edit_profile.compName.focus();
	} else if (IsEmpty(document.edit_profile.street.value)) {
		alert(error_street);
		document.edit_profile.street.focus();
	} else if (IsEmpty(document.edit_profile.zipcode.value)) {
		alert(error_zipcode);
		document.edit_profile.zipcode.focus();
	} else if (IsEmpty(document.edit_profile.city.value)) {
		alert(error_city);
		document.edit_profile.city.focus();
	} else if (document.edit_profile.countryID.value == "-1") {
		alert(error_country);
		document.edit_profile.countryID.focus();
	} else if (document.edit_profile.countryCodeComp.value == "-1") {
		alert(error_countrycode);
		document.edit_profile.countryCodeComp.focus();
	} else if (IsEmpty(document.edit_profile.phonenrComp.value)) {
		alert(error_phonenr);
		document.edit_profile.phonenrComp.focus();
	} else if (IsEmpty(document.edit_profile.vatnr.value)) {
		alert(error_vatnr);
		document.edit_profile.vatnr.focus();
	} else {
		self.document.edit_profile.submit();
	}
}

// Function to check input email from forget password form
function checkForget () {
	if (IsEmpty(document.forgot.username.value)) {
		alert(error_username);
		document.forgot.username.focus();	
	} else if (!IsEmail(document.forgot.username.value)) {
		alert(error_email_format);
		document.forgot.username.focus();	
	} else {
		self.document.forgot.submit();
	}	
}

// Function to check input password from change password form
function changePassword () {
	if (IsEmpty(document.changePwd.newPassword.value)) {
		alert(error_newpassword);
		document.changePwd.newPassword.focus();	
	} else if ((document.changePwd.newPassword.value.length < 6) || (document.changePwd.newPassword.value.length > 10)) {
		alert(error_password_char);
		document.changePwd.newPassword.focus();	
	} else if (IsEmpty(document.changePwd.confirmPassword.value)) {
		alert(error_confirmpassword);
		document.changePwd.confirmPassword.focus();	
	} else if ((document.changePwd.confirmPassword.value.length < 6) || (document.changePwd.confirmPassword.value.length > 10)) {
		alert(error_password_char);
		document.changePwd.confirmPassword.focus();	
	} else if (document.changePwd.newPassword.value != document.changePwd.confirmPassword.value) {
		alert(error_not_match);
		document.changePwd.confirmPassword.focus();	
	} else {
		self.document.changePwd.submit();
	}
}

function resubmitCountry () {
	document.request.action = "./request.php?resubmit=1";
	document.request.submit();
}

function resubmitCountryAdmin () {
	document.edit_profile.action = "./changeProfile.php?resubmit=1";
	document.edit_profile.submit();
}

function resubmitCountryClient () {
	document.edit_profile.action = "./changeProfile.php?resubmit=1";
	document.edit_profile.submit();
}

function checkRemoveOrder () {
	question = confirm (confirm_remove_order);
	if (question != "0") {
		document.currentOrderMain.action = "./manageOrder.php?checkStatus=2";
		document.currentOrderMain.submit();
	}
}

function resubmitOrder () {
	document.currentOrderMain.action = "./currentOrder.php?selectMore=1";
	document.currentOrderMain.submit();
}
