/**********************************************************/
/* Function: valContactUs()												        */
/*																			                  */
/* Desc: Validation functions for Contact us form         */
/*																			                  */
/*																			                  */
/* Date: 23/01/2007														            */
/**********************************************************/

function valContactUs(theForm) 
{
	/*****************/
	/* Name validation */
	/*****************/
	if (theForm.fullname.value == "")
	{
		theForm.fullname.focus();
		return "fullnameEmpty";
	}
	
	var allValid = validateName(theForm.fullname);

	if (!allValid) { 		 
		theForm.fullname.focus(); 
		return "fullnameSpecialChars"; 
	}   

   /*****************/
	/* E-Mail validation*/
	/*****************/
	if (theForm.email.value == "")
	{		 
		theForm.email.focus();
		return "emailEmpty";
	}
		
	if (! checkmail(theForm.email.value)) { 
		theForm.email.focus();
		return "emailInvalid"; 
	}
	
	/*********************/
	/* Message validation */
	/*********************/
	if (theForm.message.value == "")
	{		 
		theForm.message.focus();
		return ("messageEmpty");
	}

	 if (theForm.message.value.length > 1000) {		 
	  theForm.message.focus();
	  return "messageTooLong";
	}
	
	return ""; 	
}

function validateName(widget)
{
    var validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + " " + ".,-Ρραινσϊ";
	return validateString(widget, validCharacters);
}

function validateUserName(widget)
{
    var validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"+ "._-@[]";
	return validateString(widget, validCharacters);
}

function validateCode(widget)
{
    var validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789";
	return validateString(widget, validCharacters);
}

function validateString(widget, validCharacters)
{
	var checkStr = widget.value;
	var allValid = true; 
	for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i); 
		for (j = 0; j < validCharacters.length; j++)
		  if (ch == validCharacters.charAt(j))
			break;
		if (j == validCharacters.length) { 
		  allValid = false; 
		  break; 
		}
	}

	return allValid;

}


function valSignUp(theForm) 
{
   /*****************/
	/* E-Mail validation*/
	/*****************/
	if (theForm.email.value == "")
	{		 
		theForm.email.focus();
		return "emailEmpty";
	}
		
	if (! checkmail(theForm.email.value)) {
		theForm.email.focus();
		return "emailInvalid"; 
	}

	if (theForm.email.value.length > 60) {		 
	  theForm.email.focus();
	  return "emailTooLong";
	}

	/*****************/
	/* Password validation */
	/*****************/
	if (theForm.password.value == "")
	{
		theForm.password.focus();
		return "passwordEmpty";
	}
	
	if (! validateCode(theForm.password)) { 		 
		theForm.password.focus(); 
		return "passwordSpecialChars"; 
	}   

	if (theForm.password.value.length > 50) {		 
	  theForm.password.focus();
	  return "passwordTooLong";
	}

	if (theForm.password.value != theForm.confirmPassword.value) {		 
	  theForm.password.focus();
	  return "passwordsDontMatch";
	}

	/*****************/
	/* Account Code validation */
	/*****************/
	if (theForm.accountCode.value == "")
	{
		theForm.accountCode.focus();
		return "accountCodeEmpty";
	}

	if (! validateCode(theForm.accountCode) ) { 		 
		theForm.accountCode.focus(); 
		return "accountCodeSpecialChars"; 
	}   

	if (theForm.accountCode.value.length > 50) {		 
	  theForm.accountCode.focus();
	  return "accountCodeTooLong";
	}

	/*********************/
	/* Terms validation */
	/*********************/
	if (!theForm.termsAccept.checked)
	{		 
		theForm.termsAccept.focus();
		return ("termsAcceptNotChecked");
	}
	
	return ""; 	
}

function checkmail(str) {
	var at="@";
	var dot=".";

	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var ok ="1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	if (str.indexOf(at)==-1) {
		return false;
	}

	for(i=0; i < str.length ;i++) {
		if(ok.indexOf(str.charAt(i))<0) {
			return false;
		}
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1) {
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1) {
		return false;
	}

	if (str.indexOf(" ")!=-1) {
		return false;
	}

	return true;
}

function valNewsletterSignUp(theForm) 
{
    /********************/
	/* E-Mail validation*/
	/********************/
	if (theForm.email.value == "")
	{		 
		theForm.email.focus();
		return "emailEmpty";
	}
	
	if (!checkmail(theForm.email.value)) {
		theForm.email.focus();
		return "emailInvalid"; 
	}

	if (theForm.email.value.length > 60) {		 
	  theForm.email.focus();
	  return "emailTooLong";
	}
	
	theForm.email.focus();
	return ""; 	
}




function valLogin(theForm) 
{
	/*****************/
	/* Account Code validation */
	/*****************/
	if (theForm.accountCode.value == "")
	{
		theForm.accountCode.focus();
		return "accountCodeEmpty";
	}

	if (! validateCode(theForm.accountCode) ) { 		 
		theForm.accountCode.focus(); 
		return "accountCodeSpecialChars"; 
	}   

	if (theForm.accountCode.value.length > 50) {		 
	  theForm.accountCode.focus();
	  return "accountCodeTooLong";
	}

	/*****************/
	/* Username validation */
	/*****************/
	if (theForm.username.value == "")
	{
		theForm.username.focus();
		return "usernameEmpty";
	}
	
	if (! validateUserName(theForm.username)) { 		 
		theForm.username.focus(); 
		return "usernameSpecialChars"; 
	}   

	if (theForm.username.value.length > 50) {		 
	  theForm.username.focus();
	  return "usernameTooLong";
	}

	/*****************/
	/* Password validation */
	/*****************/
	if (theForm.password.value == "")
	{
		theForm.password.focus();
		return "passwordEmpty";
	}
	
	if (! validateUserName(theForm.password)) { 		 
		theForm.password.focus(); 
		return "passwordSpecialChars"; 
	}   

	if (theForm.password.value.length > 50) {		 
	  theForm.password.focus();
	  return "passwordTooLong";
	}

	
	return ""; 	
}
