var whitespace = " \t\n\r";
var defaultEmptyOK = false;

function isAlphabet(s) {
    var numofchinese = num_of_chinese(s);
	if (numofchinese>0) {
	    return false;
	} else {
    	rExp = /^[A-Za-z]+$/
    	return  rExp.test(s);
    }
}
function isNumeric(s) {
    var numofchinese = num_of_chinese(s);
	if (numofchinese>0) {
	    return false;
	} else {
    	rExp = /^[0-9]+$/
    	return rExp.test(s);
    }
}
function isAmount(s) {
    var numofchinese = num_of_chinese(s);
	if (numofchinese>0) {
	    return false;
	} else {
    	rExp = /^[0-9]+(.[0-9]{1,2})?$/
    	return rExp.test(s);
    }
}
function isAlphaNumeric(s) {
    var numofchinese = num_of_chinese(s);
	if (numofchinese>0) {
	    return false;
	} else {
    	rExp = /^[A-Za-z0-9]+$/
    	return  rExp.test(s);
    }
}
function isValidStringList(s) {
	rExp = /^[A-Za-z0-9]+(,[A-Za-z0-9]+)*$/
	return  rExp.test(s);
}
function isValidNumberList(s) {
	rExp = /^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$/
	return  rExp.test(s);
}
function isValidEmail(s) {
    var numofchinese = num_of_chinese(s);
	if (numofchinese>0) {
	    return false;
	} else {
    	rExp = /^[A-Za-z0-9.-_]+@([A-Za-z0-9]+[-_]?[A-Za-z0-9]+)(.[A-Za-z0-9]+[-_]?[A-Za-z0-9]+)+$/
    	return  rExp.test(s);
    }
}
function isContainSpace(strPass) {
	rExp = / /gi;
	var org = strPass;
	
	if (strPass==null)
		return false;
	else {
		if (strPass.replace(rExp,"")==org)
			return false;
		else
			return true;
	}
}
function trim(s) {
    var r=/\b(.*)('*)\b/.exec(s);
    return (r==null)?"":r[1];
    return s;
}
function lefttrim(s) {
    var r=/\b(.*)('*)/.exec(s);
    return (r==null)?"":r[1];
    return s;
}
function isEmptyString(strPass) {
    var numofchinese = num_of_chinese(strPass);
	if (numofchinese>0) {
	    return false;
	} else {
    	rExp = / /gi;
    	if (strPass==null)
    		return true;
    	else {
    	    strPass = trim(strPass);
    		if (strPass.replace(rExp,"")=="") {
    			return true;
    		} else {
    			return false;
    	    }
    	}
    }
}

function isEmptyChiString(strPass) {
	if (strPass.length<=0) {
		return true;
	} else {
		return false;
	}
}

// Checking Date
var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

/*****************************/
// isValidDate ('YYYY-MM-DD', 0)
// isValidDate ('DD/MM/YYYY', 1)
// isValidDate ('DD-MM-YYYY', 2)
/*****************************/
function isValidDate (inpDate, formatId) {
    var year;
    var month;
    var day;
    
	switch (formatId) {
    case 1:
	    rExp = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/;
        break;
    case 2:
	    rExp = /^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$/;
        break;
    default:
	    rExp = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
	    break;
	}

	if (rExp.test(inpDate)) {
    	switch (formatId) {
        case 1:
            // DD/MM/YYYY
    	    year = parseInt(inpDate.substring(inpDate.lastIndexOf("/")+1),10).toString();
    	    month = parseInt(inpDate.substring(inpDate.indexOf("/")+1,inpDate.lastIndexOf("/")),10).toString();
    	    day = parseInt(inpDate.substring(0,inpDate.indexOf("/")),10).toString();
            break;
        case 2:
            // DD-MM-YYYY
    	    year = parseInt(inpDate.substring(inpDate.lastIndexOf("-")+1),10).toString();
    	    month = parseInt(inpDate.substring(inpDate.indexOf("-")+1,inpDate.lastIndexOf("-")),10).toString();
    	    day = parseInt(inpDate.substring(0,inpDate.indexOf("-")),10).toString();
            break;
        default:
            // YYYY-MM-DD
    	    year = parseInt(inpDate.substring(0,inpDate.indexOf("-")),10).toString();
    	    month = parseInt(inpDate.substring(inpDate.indexOf("-")+1,inpDate.lastIndexOf("-")),10).toString();
    	    day = parseInt(inpDate.substring(inpDate.lastIndexOf("-")+1),10).toString();
    	    break;
    	}
	    return isDate(year, month, day);
	} else {
	    return false;
	}
}

function isDate (year, month, day) {   
    // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isYear (s) {   
    if (isEmptyString(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s) {   
    if (isEmptyString(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (s) {   
    if (isEmptyString(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year) {   
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isNonnegativeInteger (s) {  
    var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmptyString(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isSignedInteger (s) {   
    if (isEmptyString(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isNumeric(s.substring(startPos, s.length), secondArg))
    }
}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

function isIntegerInRange (s, a, b) {   
    if (isEmptyString(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isNumeric(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

/*****************************/
// DateCompare ('YYYY-MM-DD', 'YYYY-MM-DD', 0)
// DateCompare ('DD/MM/YYYY', 'yyyymmdd',1)
// DateCompare ('DD-MM-YYYY', 'yyyymmdd',2)
// DateCompare ('DD-MM-YYYY', 'DD-MM-YYYY',3)
// Return:
// 0 - inpDate = compareToDate
// 1 - inpDate > compareToDate
// 2 - inpDate < compareToDate
// -1 - inpDate format incorrect
/*****************************/
function DateCompare (inpDate, compareToDate, formatId) {
	var tempDate;
	
	switch (formatId) {
    case 1:
	    rExp = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/;
        break;
    case 2:
	    rExp = /^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$/;
        break;
    case 3:
	    rExp = /^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$/;
        break;
    default:
	    rExp = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
	    break;
	}

	if (rExp.test(inpDate)) {
    	switch (formatId) {
        case 1:
            // DD/MM/YYYY && yyyymmdd
            tempDate = inpDate.substring(inpDate.lastIndexOf("/")+1) + inpDate.substring(inpDate.indexOf("/")+1,inpDate.lastIndexOf("/")) + inpDate.substring(0,inpDate.indexOf("/"));
            break;
        case 2:
            // DD-MM-YYYY && yyyymmdd
    	    tempDate = inpDate.substring(inpDate.lastIndexOf("-")+1) + inpDate.substring(inpDate.indexOf("-")+1,inpDate.lastIndexOf("-")) + inpDate.substring(0,inpDate.indexOf("-"));
            break;
        case 3:
            // DD-MM-YYYY && DD-MM-YYYY
    	    tempDate = inpDate.substring(inpDate.lastIndexOf("-")+1) + inpDate.substring(inpDate.indexOf("-")+1,inpDate.lastIndexOf("-")) + inpDate.substring(0,inpDate.indexOf("-"));
    	    compareToDate = compareToDate.substring(compareToDate.lastIndexOf("-")+1) + compareToDate.substring(compareToDate.indexOf("-")+1,compareToDate.lastIndexOf("-")) + compareToDate.substring(0,compareToDate.indexOf("-"));
            break;
        default:
            // YYYY-MM-DD && yyyymmdd
    	    tempDate = inpDate.substring(0,inpDate.indexOf("-")) + inpDate.substring(inpDate.indexOf("-")+1,inpDate.lastIndexOf("-")) + inpDate.substring(inpDate.lastIndexOf("-")+1);
    	    compareToDate = compareToDate.substring(0,compareToDate.indexOf("-")) + compareToDate.substring(compareToDate.indexOf("-")+1,compareToDate.lastIndexOf("-")) + compareToDate.substring(compareToDate.lastIndexOf("-")+1);
    	    break;
    	}
	    if (tempDate > compareToDate) { 
	        return 1;
	    } else if (tempDate < compareToDate) { 
	        return 2;
	    } else { 
	        return 0;
	    }
	} else {
	    return -1;
	}
}
function check_eng_chi_length(input_element,DatebaseFieldLength){
	var okFlag = false;
	var totLen = input_element.length; 
	var chineseLen = num_of_chinese(input_element);
	var nonChineseLen = parseInt(DatebaseFieldLength)-(parseInt(chineseLen)*2);

	if (parseInt(nonChineseLen)>=0) {
		if ((parseInt(totLen) - parseInt(chineseLen)) <= parseInt(nonChineseLen)) {
			okFlag = true;
		}
	}
	return okFlag;
}

function num_of_chinese(input_element){	
	TheLength = input_element.length;
	noOfChinese = 0;
	for (i = 0; i <= TheLength-1; i++) {
  	    if ((input_element.charCodeAt(i) >255)) {
  	    	noOfChinese++;
  	    }
	}
	return noOfChinese;
}

function non_chinese(input_element){
	TheLength = input_element.length;
	for (i = 0; i <= TheLength-1; i++) {
  	    if ((input_element.charCodeAt(i) >255)) {
  	    	return false;
  	    }
	}
	return true;
}
function trimNonChinese(s) {
    var chineseLen = num_of_chinese(s);
    if (parseInt(chineseLen)==0) {
        return lefttrim(s);
    } else {
        return s;
    }        
}    
