/******************************************************************************************
Constants
*******************************************************************************************/
var sErrEmpty = "is required";
var sErrTextMaxLength = "can be at most";
var sErrTextMinLength = "must be atlest";
var sErrNotChosen = "not chosen";
var sErrNotNumber = "must only contain a number";
var sErrMinValue = "Minimum value is ";
var sErrMaxValue = "Maximum value is ";
var sErrPasswordMismatch = "Password Entries don't match";
var sErrEmail = " is not a valid e-mail address";
var sErrZipCode = " is not a valid U.S.A. zip code";	//added by humayun on 4/3/2007
var sErrMessage = "";

var sErrValidateFloatDot        = " decimalsign must be a period (.)";
var sErrValidateFloatComma      = " decimalsign must be a comma (,)";
var sErrValidateFloatNrOfDec1    = " must have at most ";
var sErrValidateFloatNrOfDec2    = " decimals";

//var sErrNotNumber          = " must only contain a number.\n";
//var sErrMinValue       = " Minimum value is ";
//var sErrMaxValue       = " Maximum value is ";



var sInvalidSelectionValue = "-1";
var bSilentMode = false;


/******************************************************************************************
Validation Functions
*******************************************************************************************/
function CheckText(objText, sName, bAllowEmpty, nMinLength, nMaxLength)
{

	var sText = objText.value
	sText = trim(sText);

   // If empty
   if(!bAllowEmpty && sText == "")
   {
		sErrMessage = sName + " " + sErrEmpty;
		ShowErrorMessage(sErrMessage);
		objText.focus();
		return false;
   }
   else if(sText != "") // else if to short or to long string
   {
		if(nMinLength != null && sText.length < nMinLength)
		{
			sErrMessage = sName + " " + sErrTextMinLength + " " + nMinLength + " characters";
			ShowErrorMessage(sErrMessage);
			objText.focus();
			return false;
		}
		if(nMaxLength != null && sText.length > nMaxLength)
		{
			sErrMessage = sName + " " + sErrTextMaxLength + " " + nMaxLength + " characters";
			ShowErrorMessage(sErrMessage);
			objText.focus();
			return false;
		}
   }

   return true;
}

function CheckNumber(objNumber, sName, bAllowEmpty, nMinValue, nMaxValue, bAllowNegative)
{
	var sNumber = objNumber.value
	sNumber = trim(sNumber);

	if(!bAllowEmpty && sNumber == "") // If empty
	{
		sErrMessage = sName + " " + sErrEmpty;
		ShowErrorMessage(sErrMessage);
		objNumber.focus();
		return false;
	}
	else if(sNumber != "") // else if number - to low or to high
	{
		if(bAllowNegative && sNumber.charAt(0) == "-")
			sNumber = sNumber.slice(1);

		var objRegExp   = new RegExp("[^0-9]", "g"); // Search for everything except 0-9
		var nInvalidPos = sNumber.search(objRegExp);

		if(nInvalidPos != -1)
		{
			sErrMessage = sName + " " + sErrNotNumber;
			ShowErrorMessage(sErrMessage);
			objNumber.focus();
			return false;
		}
		else // Only number characters
		{
			if(nMinValue != null && eval(sNumber) < nMinValue)
			{
				sErrMessage = sName + " " + sErrMinValue + "" + nMinValue;
				ShowErrorMessage(sErrMessage);
				objNumber.focus();
				return false;
			}
			if(nMaxValue != null && eval(sNumber) > nMaxValue)
			{
				sErrMessage = sName + " " + sErrMaxValue + "" + nMaxValue;
				ShowErrorMessage(sErrMessage);
				objNumber.focus();
				return false;
			}
		}
	}

	return true;
}

function validateFloat(objNumber, sName, bAllowEmpty, iMinValue, iMaxValue, bUseDot, iNrOfDecimals)
{
	var sNumber = objNumber.value
	sNumber = trim(sNumber);

   var sErrorMsg = "";
   sNumber = trim(sNumber);

   if(!bAllowEmpty && sNumber == "") // If empty
   {
		sErrorMsg = sName + " " + sErrEmpty;
		ShowErrorMessage(sErrorMsg);
		objNumber.focus();
		return false;
   }
   else if(sNumber != "") // else if number - to low or to high
   {
      // 1-n numbers followed by av comma or dot and 1-n decimals
      var objRegExp   = new RegExp("^(-)?[0-9]{1,}[\.,]{0,1}[0-9]{0,}$", "i");
      //var objRegExpCo = new RegExp("[,]");
      var iInvalidPos = sNumber.search(objRegExp);

      if(iInvalidPos == -1) 
      {
 		sErrorMsg += sName + " " + sErrNotNumber;
		ShowErrorMessage(sErrorMsg);
		objNumber.focus();
		return false;

      }
      else // Only number characters
      {
         if((iNrOfDecimals != null && sNumber.indexOf(",") != -1 && sNumber.split(",")[1].length > iNrOfDecimals) ||
            (iNrOfDecimals != null && sNumber.indexOf(".") != -1 && sNumber.split(".")[1].length > iNrOfDecimals))
		  {
			 sErrorMsg += sName + " " +  sErrValidateFloatNrOfDec1 + iNrOfDecimals + sErrValidateFloatNrOfDec2 + ".\n";
			ShowErrorMessage(sErrorMsg);
			objNumber.focus();
			return false;
		  }
         if(bUseDot && sNumber.indexOf(",") != -1)
		  {
            sErrorMsg += sName + " " +  sErrValidateFloatDot + ".\n";
			ShowErrorMessage(sErrorMsg);
			objNumber.focus();
			return false;
		  }
         if(!bUseDot && sNumber.indexOf(".") != -1)
		  {
            sErrorMsg += sName + " " +  sErrValidateFloatComma + ".\n";
			ShowErrorMessage(sErrorMsg);
			objNumber.focus();
			return false;
		  }
         if(sNumber.indexOf(",") != -1)
            sNumber = sNumber.replace(new RegExp("[,]"), ".");
		 if(iMinValue != null && Math.round(sNumber) < iMinValue)
		  {
            sErrorMsg += sName + " " +  sErrMinValue + iMinValue + ".\n";
			ShowErrorMessage(sErrorMsg);
			objNumber.focus();
			return false;
		  }
         if(iMaxValue != null && Math.round(sNumber) > iMaxValue)
		  {
            sErrorMsg += sName + " " +  sErrMaxValue + iMaxValue + ".\n";
			ShowErrorMessage(sErrorMsg);
			objNumber.focus();
			return false;
		  }
      }
   }
   return true;
}

function CheckSelect(objSELECT, sName)
{
	if(objSELECT.selectedIndex == -1)
	{
		sErrMessage = sName + " " + sErrNotChosen;
		ShowErrorMessage(sErrMessage);
		objSELECT.focus();
		return false;
	}

	if(objSELECT.options[objSELECT.selectedIndex].value == sInvalidSelectionValue || objSELECT.options[objSELECT.selectedIndex].value == "")
	{
		sErrMessage = sName + " " + sErrNotChosen;
		ShowErrorMessage(sErrMessage);
		objSELECT.focus();
		return false;
	}
	return true;
}

function CheckPassword(strPassword, objRetypePassword)
{
	var strRetypePassword = objRetypePassword.value
	if(strPassword != strRetypePassword)
	{
		sErrMessage = sErrPasswordMismatch;
		ShowErrorMessage(sErrMessage);
		objRetypePassword.focus();
		return false;
	}

	return true;
}

function CheckEmail(objEmail, sName, bAllowEmpty)
{
	/* Written by Paolo Wales (paolo@taize.fr) starting on a basis by Samrat Sen.

	Notes:

	'exclude' checks 5 conditions:

	a) characters that should not be in the address
	b) characters that should not be at the start
	c) & d) characters that shouldn't be together
	e) there's not more than one '@'

	'check' checks there's at least one '@', later followed by at least one '.'
	'checkend' checks the address ends with a period followed by 2 or 3 alpha characters.
	N.B. Javascript 1.2 only works with version 4 browsers and higher. */
   
	var exclude   =/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check     =/@[\w\-]+\./;
	var checkend  =/\.[a-zA-Z]{2,3}$/;   
	var sErrorMsg = "";
	
	var sEmail = objEmail.value;
	sEmail = trim(sEmail);

	if(!bAllowEmpty && sEmail == "")
	{
		sErrMessage = sName + " " + sErrEmpty;
		ShowErrorMessage(sErrMessage);
		objEmail.focus();
		return false;
	}
	else if(sEmail != "")
	{
		if(((sEmail.search(exclude) != -1) || (sEmail.search(check)) == -1) || (sEmail.search(checkend) == -1))
		{
			sErrMessage = sName + " " + sErrEmail;
			ShowErrorMessage(sErrMessage);
			objEmail.focus();
			return false;
		}
	}

	return true;
}


/*********************************************************************************
Following function is added by humayun on 4/3/2007
This function check whether a given U.S.A. zip code is valid or not.
**********************************************************************************/

function CheckUSAZip(objZipCode, sName, bAllowEmpty)
{
	//var sRegExp = /\d{5}(-\d{4})?/;
	var sRegExp = /^\d{5}(-\d{4})?$/; //12345 or 12345-1234

	var sZipCode = objZipCode.value;
	sZipCode = trim(sZipCode);

	if(!bAllowEmpty && sZipCode == "")
	{
		sErrMessage = sName + " " + sErrEmpty;
		ShowErrorMessage(sErrMessage);
		objZipCode.focus();
		return false;
	}
	else if (sZipCode != "")
	{
		if (sZipCode.search(sRegExp) == -1)
		{
			sErrMessage = sName + " " + sErrZipCode;
			ShowErrorMessage(sErrMessage);
			objZipCode.focus();
			return false;
		}
	}

	return true;
}

function CheckChkBox(objCheckBox, sName, bAllowEmpty)
{
	var c = false;
	var f = true;

	if(!objCheckBox.length)
	{
		c = objCheckBox.checked;
		f = false;
	}
	else
	{
		for(var i = 0; i < objCheckBox.length; i++) 
		{
			if(objCheckBox[i].checked) 
			{
				c = true;
				break;
			}
		}
	}
	
	if(!bAllowEmpty && !c) 
	{
		sErrMessage = sName + " " + sErrNotChosen;
		ShowErrorMessage(sErrMessage);
		if(!f)
			objCheckBox.focus();
		else
			objCheckBox[0].focus();
	}
		
	return c;
}

function CheckRadio(objChkRadio, sName, bAllowEmpty)
{
	var c = false;
	var f = true;

	if(!objChkRadio.length)
	{
		c = objChkRadio.checked;
		f = false;
	}
	else
	{
		for(var i = 0; i < objChkRadio.length; i++) 
		{
			if(objChkRadio[i].checked) 
			{
				c = true;
				break;
			}
		}
	}
	
	if(!bAllowEmpty && !c) 
	{
		sErrMessage = sName + " " + sErrNotChosen;
		ShowErrorMessage(sErrMessage);
		if(!f)
			objChkRadio.focus();
		else
			objChkRadio[0].focus();
	}
		
	return c;
}

/******************************************************************************************
Miscellaneous Utility Functions
*******************************************************************************************/
function ltrim(sValue)
{
   while(1)
   {
      if(sValue.substring(0, 1) != " ")
         break;
      sValue = sValue.substring(1, sValue.length);
   }
   return sValue;
}

function rtrim(sValue)
{
   while(1)
   {
      if(sValue.substring(sValue.length - 1, sValue.length) != " ")
         break;
      sValue = sValue.substring(0, sValue.length - 1);
   }
   return sValue;
}

function trim(sValue)
{
	if(sValue == null)
	{
		return null;
	}

	var sTemp = ltrim(sValue);
	return rtrim(sTemp);
}

function ShowErrorMessage(sMessage)
{
	if(!bSilentMode)
		alert(sMessage);
}

function SetSilentMode(bValue)
{
	bSilentMode = bValue;
}

function SetInvalidSelectionValue(sValue)
{
	sInvalidSelectionValue = sValue;
}

/* Commented temporarily Humayun on 20070220

function CheckFilepath(objImage)
{
	var ImageFile = objImage.value;
		
	ImageFile = trim(ImageFile);
	
	var sRegExp = "^(([a-zA-Z]:(\\\\w+)*\\\\[a-zA-Z0_9]+)).*$";

	var objRegExp = new RegExp(sRegExp);	

	var nInvalidPos = ImageFile.match(objRegExp);
	
	if (nInvalidPos)
	{
		return true;
	}
	else
	{
		return false;
	}

}

function CheckImage(objImage, sName, bAllowEmpty, simagetype)
{
	var ImageFile = objImage.value;
	var bValidFilepath = true;
	
	ImageFile = trim(ImageFile);
	
	if(!bAllowEmpty && ImageFile == "")
	{		
		sErrMessage = sName + " " + sErrEmpty;
		ShowErrorMessage(sErrMessage);
		objImage.focus();
		return false;
	}
	else if(ImageFile != "")
		{
			var sRegExp = "";
			bValidFilepath = CheckFilepath(objImage);
			
			if (!bValidFilepath)
			{
				ShowErrorMessage("Wrong File path");
				objImage.focus();
				return false;
			}

			switch(simagetype)
			{
				case "Flash":
					sRegExp = "^.+(.swf)$";	
					break;
				case "pdf":
					sRegExp = "^.+(.pdf)$";
					break
				case "Other":
					sRegExp = "^.+((.jpg)|(.gif)|(.jpeg)|(.bmp)|(.png))$";
					break;
			}
			
			var objRegExp = new RegExp(sRegExp, "i");
			///^.+\.(ext)$/i;
			//^.+\@.+\..+$
			var nInvalidPos = ImageFile.search(objRegExp);
			//var m = objRegExp.exec(ImageFile);
			//alert(nInvalidPos);	
			
			
			if (!bAllowEmpty && nInvalidPos == -1)
			{
				ShowErrorMessage(sName + " Invalid Format");
				objImage.focus();
				return false;
			}
			else
				if (bAllowEmpty && nInvalidPos == -1)
				{
					ShowErrorMessage(sName + " Invalid Format");
					objImage.focus();
					return false;
				}
				else
				{		
					return true;
				}
		}
		return true;
}
}*/