
function isAlphaNumeric(sCheckStr) {
	sValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_ "
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		//tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0) {
			return false;
		}
	}
	return true;
}

function isAlphaNumericNoSpace(sCheckStr) {
	sValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-"
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		//tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0) {
			return false;
		}
	}
	return true;
}

function isNumeric(sCheckStr) {
	sValidChars = "1234567890.,-"
	dotCount = 0;
	
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		tempChr = tempChr.toLowerCase();
		if (tempChr == '.')
		  dotCount++;
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
		else if (tempChr == '-' && sCheckStr.indexOf(tempChr) != 0) //the minus sign must be the first
		  return false;
	}
	if (dotCount > 1)//can only has one dot sign
	  return false;
	return true;
}

function isPositiveInt(sCheckStr) {
	sValidChars = "1234567890"
	if (trim(sCheckStr) == "0")
	  return false;
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
		
	}
	return true;
}

function isPostCode(sCheckStr) {
	sValidChars = "1234567890"
	
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
		
	}
	return true;
}

function isAllSpace(sChkStr) {
	var i, tempChar, bAllSpace;
	sChkStr = String(sChkStr);
	bAllSpace = true;

	for (i=0; i < sChkStr.length; i++) {
		tempChar = sChkStr.substring(i, i+1);
		if (tempChar != " ") {
			bAllSpace = false;
			break;
		}
	}
	return bAllSpace;
}

function checkRequiredField(formName, fieldName)
{
    //alert(formName)
    //alert(fieldName)
	//alert(document.forms[formName].elements[fieldName])
	var iMissing = 0;
	//text
	var sType;
	if (document.forms[formName].elements[fieldName] != null)
	   sType = document.forms[formName].elements[fieldName].type;
	else
	   return true;
  var sElementType;
  if( sType == null ) sType = "";
  if (( sType == "text" ) || ( sType == "textarea" ) || ( sType == "password" ) || (sType == "hidden") || (sType == "file"))
	{
		//check text
		//alpha
		//alpha-numeric
		//etc
  	if (isAllSpace(document.forms[formName].elements[fieldName].value))
		  return false;
		else
			return true;
	}
	//select
	if (sType.indexOf("select") != -1)
	{
      //dropdown list box
		if (document.forms[formName].elements[fieldName].length == 0 &&
		document.forms[formName].elements[fieldName].selectedIndex == 0)
		{
			return false;
		}

		if (document.forms[formName].elements[fieldName].length > 0 &&
		document.forms[formName].elements[fieldName].selectedIndex == -1)
		{
			return false;
		}
		
		//add by Erik Wu, if there is nothing in the drop down list,
		//the selectedIndex is -1
		if (document.forms[formName].elements[fieldName].length == 0 &&
		document.forms[formName].elements[fieldName].selectedIndex == -1)
		{
			return false;
		}
		
		//if the empty value is selected, also return false
		if (document.forms[formName].elements[fieldName].length > 0 &&
		document.forms[formName].elements[fieldName].value == '')
		{
			return false;
		}
		return true;
	}
	if (sType == "")//potentially multiple value fields
	{
      inputArray = document.forms[formName].elements[fieldName];
      sElementType = inputArray[0].type;
      //radio button and checkbox
		if (( sElementType == "checkbox" )|| (sElementType == "radio" ))
		{
			var i = 0;
			var bChecked = false;
			while(inputArray[i]!=null)
			{
				if (inputArray[i].checked)
				{
					bChecked = true;
					return true;
				}
				i++;
			}
			if (bChecked == false)
			{
				return false;
			}
			return true;
		}
		if (sElementType == "text" || sElementType == "textarea")
		{
			  var i = 0;
  			for(i=0;i<inputArray.length;i++)
  			{
  				if(inputArray[i]==null)
  				{
  				  return false;
  				}
  				if (isAllSpace(inputArray[i].value))
  				{
  				  return false;
  				}
  			}
  			return true;
		}
		if (sElementType.indexOf("select") != -1)
    {
      var i = 0;
  		for(i=0;i<inputArray.length;i++)
  		{
  			if(inputArray[i].length == 0 && inputArray[i].selectedIndex == 0)
  			  return false;
  			if(inputArray[i].length > 0 &&	 inputArray[i].selectedIndex == -1)
  			  return false;
  			if(inputArray[i].length == 0 && inputArray[i].selectedIndex == -1)
  			  return false;
  			if(inputArray[i].length > 0 && inputArray[i].value == '')
  			  return false;	 
  		}
  		return true;
    }
	}
	return true;
}
function isAlpha(sCheckStr) {
	sValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz- "
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		//tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
	}
	return true;
}

function isDate(sChkStr) {
	var i, tempChar;
	sChkStr = String(sChkStr);

	for (i=0; i < sChkStr.length; i++) {
		tempChar = sChkStr.substring(i, i+1);
		if (tempChar == ",") {
			return false;
		}
	}
	return true;
}

function hasInvalidIdCharacter(sCheckStr) {
	sInValidChars = ".#*:"
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		if (tempChr == "\\") return true;  // special handling for black slash
		tempChr = tempChr.toLowerCase();
		if (sInValidChars.indexOf(tempChr) >= 0) {
			if (tempChr == ":") { // check double colon
				if (sCheckStr.substr(i+1, 1) == ":") return true;
			}
			else {
				return true;
			}
		}
	}
	return false;
}

function isIPAddress(sIPAdd) {
	var sIPAddPart = "";
	for (var i=0; i<3; i++) {
		if (sIPAdd.indexOf(".") > 0) {
			sIPAddPart = sIPAdd.substr(0, sIPAdd.indexOf("."));
			if ((sIPAdd.length == 0) || !isNumeric(sIPAddPart) || (sIPAddPart > 255))
				return false;
			sIPAdd = sIPAdd.substr(sIPAdd.indexOf("."));
			if (sIPAdd.length > 0);
				sIPAdd = sIPAdd.substr(1);
		} else {
			return false;
		}
	}
	if ((sIPAdd.length == 0) || !isNumeric(sIPAdd) || (sIPAdd > 255))
		return false;
	return true;
}

function isEmail(email) {
    var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.@-,"
	var ok = 1; // 1 for success .. 0 for invalid
	var temp;
	var atCount = 0; // should be 1 at the end of check for valid Email.

	if(email.length > 0 ) {
		for (var i=0; i<email.length; i++) {
			temp = "" + email.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				ok = 0;
				break;
			}
			if(temp == '@')
				atCount++;
		} // for
		if(atCount != 1)
			ok = 0;
		positionOfAt = email.indexOf( '@') ;
		if (!(( positionOfAt > 0 ) && ( positionOfAt < (email.length - 1) ))) {
		   ok = 0 ; // @ symbol is at beginning or end of string.
		}
      // check if there's a period after @ (#18696)
      positionOfDot = email.indexOf('.', positionOfAt+2);
      if (!((positionOfDot > 0) && (positionOfDot < (email.length - 1)))) {
         ok = 0; // '.' dot is not after @ or at the end of string
      }
		if (ok == 0) {
			return false;
		} // if
	} // if (email.length > 0)
	return true;
} // method

function trim(str) {
	var strToTrim = String(str);
	while (strToTrim.substring(0, 1) == " ")
		strToTrim = strToTrim.substring(1, strToTrim.length);
	while (strToTrim.substring(strToTrim.length - 1, strToTrim.length) == " ")
		strToTrim = strToTrim.substring(0, strToTrim.length - 1);
	return strToTrim;
}


function multiply(sValue1,sValue2,vParam1,vParam2)
{
	var str1 = String(sValue1);
	var str2 = String(sValue2);
	var len1,len2,divide,result;
	len1=len2=divide=result=0;
	
	divide = "100000000";
	if(str1.indexOf(".")!=-1)
	{
		len1=str1.length-str1.indexOf(".")-1;
		str1=replace(str1,".","");
	}
	else
	{
		tmp1=str1;
	}
	
	if(str2.indexOf(".")!=-1)
	{
		len2=str2.length-str2.indexOf(".")-1;
		str2=replace(str2,".","");
	}
	else
	{
		tmp2=str2;
	}
	
	divide=divide.substring(0,len1+len2+1);
	result = parseInt(str1)*parseInt(str2)/parseInt(divide);
	
	return result;
	
}


function moveFormSelectedItem(srcSelect, desSelect)
{
  for (i=srcSelect.length-1; i>=0; i--)
  {
    if (srcSelect.options[i].selected)
    {
      if ((srcSelect.options[i].value == null) || (srcSelect.options[i].value == ""))
        srcSelect.options[i].selected = false;
      else
      {
        desSelect.options[desSelect.length] = new Option(srcSelect.options[i].text, srcSelect.options[i].value);
        srcSelect.options[i] = null;
      }
    }
  }
}

function replace(str, original, replacement) {
	var result;
	result = "";
	while(str.indexOf(original) != -1) {
	if (str.indexOf(original) > 0)
		result = result + str.substring(0, str.indexOf(original)) + replacement;
	else
		result = result + replacement;
	str = str.substring(str.indexOf(original) + original.length, str.length);
	}
	return result + str;
}


function AlertWindow(msg)
{
	alert(msg);
}


function check_date(field)
{
  if (field.length == 0)
    return true;
//Modified by Erik Wu, date should be in the format of yyyy-mm-dd
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField;
   //Modified by Erik Wu, date should be in the format of yyyy-mm-dd
   
   var dashIndex = DateValue.indexOf("-");
   if (dashIndex != 4)
     return false;
   else
     DateValue = DateValue.substr(0,dashIndex) + DateValue.substr(dashIndex+1);
   
   dashIndex = DateValue.indexOf("-");
   if (dashIndex != 6)
     return false;
   else
     DateValue = DateValue.substr(0,dashIndex) + DateValue.substr(dashIndex+1);
     
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(0,4); 
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(4,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(6,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
      return true;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!");
      return false;
   }
}

//password validation
//should be 6-12 number and digits, no special characters
function isValidPassword(sCheckStr)
{
  if (sCheckStr.length<6 || sCheckStr.length>12)
    return false;
  sValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		//tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
	}
	return true;
}

function isMoney(sCheckStr) {
	sValidChars = "1234567890."
	dotCount = 0;
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		tempChr = tempChr.toLowerCase();
		if (tempChr == '.')
		  dotCount++;
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
		else if (tempChr == '-' && sCheckStr.indexOf(tempChr) != 0) //the minus sign must be the first
		  return false;
	}
	if (dotCount > 1)//can only has one dot sign
	  return false;
	return true;
}
function isPositiveMoney(sCheckStr)
{
	if (!isMoney(sCheckStr))
	  return false;
	if (parseFloat(sCheckStr) == 0)
	  return false;
	
	return true;
}
function isDiscount(sCheckStr) {
    sValidChars = "1234567890."
	dotCount = 0;
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		tempChr = tempChr.toLowerCase();
		if (tempChr == '.')
		  dotCount++;
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
		else if (tempChr == '-' && sCheckStr.indexOf(tempChr) != 0) //the minus sign must be the first
		  return false;
	}
	if (dotCount > 1)//can only has one dot sign
	  return false;
	
	//must less than or eaqual to 1
	if (parseFloat(sCheckStr) >1)
	  return false;
	return true;
}

function isValidCellphone(sCheckStr) {
	sValidChars = "1234567890"
	
	for (i=0; i<sCheckStr.length; i++) {
		tempChr = sCheckStr.substr(i, 1);
		tempChr = tempChr.toLowerCase();
		if (sValidChars.indexOf(tempChr) < 0)
			return false;
		
	}
	
	if(sCheckStr.length != 11)
	  return false;
	  
	if(sCheckStr.indexOf("13") != 0 && sCheckStr.indexOf("15") != 0 && sCheckStr.indexOf("18") != 0)
	  return false;
	return true;
}

//the following three functions get the year,month and day 
//of a date string with the format yyyy-mm-dd
function year(strDate)
{
    return strDate.substr(0,4)
}
function month(strDate)
{
    var firstDashPos = strDate.indexOf('-')
    var lastDashPos = strDate.lastIndexOf('-')
	return strDate.substr(firstDashPos+1,lastDashPos-firstDashPos-1)
}
function day(strDate)
{
    var lastDashPos = strDate.lastIndexOf('-')
	return strDate.substr(lastDashPos+1,strDate.length-lastDashPos)
}

//if date1>date2, return 1,date1 is late than date2
//if date1<date2,return -1,date1 is early than date2
//if date1=date2,return 0
function compareDate(strDate1,strDate2)
{
	var ret;
	var date1 = new Date(year(strDate1),month(strDate1),day(strDate1))
	var date2 = new Date(year(strDate2),month(strDate2),day(strDate2))
	if(date1 == date2)
	  ret = 0;
	if(date1 > date2)
	  ret = 1;
	if(date1 < date2)
	  ret = -1;
	return ret;
}
