function survey_getCurrentYPos() { 
	if (document.body && document.body.scrollTop)
		return document.body.scrollTop;
	if (document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	if (window.pageYOffset)
		return window.pageYOffset; 
	return 0; 
}

function randOrd(){
	return (Math.round(Math.random())-0.5);
}
function findKeyInArray(targetArray,targetVar) {
	for(tmpI=0;tmpI<targetArray.length;tmpI++) {
		if(targetArray[tmpI] == targetVar) {
			return tmpI;
		}
	}
	return -1;
}


String.prototype.trim = function()
{
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this.replace(/(^[\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this.replace(/([\s]*$)/g, "");
}

function checkIsNotEmpty(str)
{
    if(str.trim() == "")
        return false;
    else
        return true;
}
/*--------------------------------- Empty --------------------------------------*/

function isArray(obj)
{
    return isObject(obj) && obj.constructor == Array;
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function strpos(str, ch) {
	for (var i = 0; i < str.length; i++)
		if (str.substring(i, i+ch.length) == ch) return i;
	return -1;
}

/********************************** Integer *************************************/
function checkIsInteger(str)
{
    if(str == "")
        return true;
    if(/^(\-?)(\d+)$/.test(str))
        return true;
    else
        return false;
}

function checkIntegerMinValue(str,val)
{
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10)>=parseInt(val,10))
            return true;
        else
            return false;
    }
    else
        return false;
}

function checkIntegerMaxValue(str,val)
{
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10)<=parseInt(val,10))
            return true;
        else
            return false;
    }
    else
        return false;
}

function isNotNegativeInteger(str)
{
    if(str == "")
        return true;
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}
/*--------------------------------- Integer --------------------------------------*/


/********************************** Double ****************************************/
function checkIsDouble(str)
{
    if(str == "")
        return true;
    if(str.indexOf(".") == -1)
    {
        if(checkIsInteger(str) == true)
            return true;
        else
            return false;
    }
    else
    {
        if(/^(\-?)(\d+)(.{1})(\d+)$/g.test(str))
            return true;
        else
            return false;
    }
}

function myParseFloat(str) 
{
	tempvar1 = parseFloat(str);
	if(!isNaN(tempvar1)) {
		return tempvar1;
	} else {
		return 0;
	}
}

function checkDoubleMinValue(str,val)
{
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str)>=parseFloat(val))
            return true;
        else
            return false;
    }
    else
        return false;
}

function checkDoubleMaxValue(str,val)
{
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str)<=parseFloat(val))
            return true;
        else
            return false;
    }
    else
        return false;
}

function isNotNegativeDouble(str)
{
    if(str == "")
        return true;
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}
/*--------------------------------- Double ---------------------------------------*/


/********************************** date ******************************************/
function checkIsValidDate(str)
{
    if(str == "")
        return true;
    var pattern = /^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g;
    if(!pattern.test(str))
        return false;
    var arrDate = str.split("-");
    if(parseInt(arrDate[0],10) < 100)
        arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
    var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
    if(date.getYear() == arrDate[0]
       && date.getMonth() == (parseInt(arrDate[1],10) -1)+""
       && date.getDate() == arrDate[2])
        return true;
    else
        return false;
}

function checkDateEarlier(strStart,strEnd)
{
    if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)
        return false;
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;
    var arr1 = strStart.split("-");
    var arr2 = strEnd.split("-");
    var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);
    var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);
    if(arr1[1].length == 1)
        arr1[1] = "0" + arr1[1];
    if(arr1[2].length == 1)
        arr1[2] = "0" + arr1[2];
    if(arr2[1].length == 1)
        arr2[1] = "0" + arr2[1];
    if(arr2[2].length == 1)
        arr2[2]="0" + arr2[2];
    var d1 = arr1[0] + arr1[1] + arr1[2];
    var d2 = arr2[0] + arr2[1] + arr2[2];
    if(parseInt(d1,10) > parseInt(d2,10))
       return false;
    else
       return true;
}
/*--------------------------------- date -----------------------------------------*/


/********************************** email *****************************************/
function checkEmail(str)
{
    if(str == "")
        return true;
    if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf('@', 0) == -1
        || str.indexOf('.', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)
        return false;
    else
        return true;
}
/*--------------------------------- email ----------------------------------------*/


/********************************** chinese ***************************************/
function checkIsChinese(str)
{
    if (str == "")
        return true;
    var pattern = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
    if (pattern.test(str))
        return true;
    else
        return false;
}

String.prototype.realLength = function()
{
  return this.replace(/[^\x00-\xff]/g,"**").length;
}
/*--------------------------------- chinese --------------------------------------*/


/********************************** mask ***************************************/
function checkMask(str,pat)
{
    if (str == "")
        return true;
    var pattern = new RegExp(pat,"gi")
    if (pattern.test(str))
        return true;
    else
        return false;
}
/*--------------------------------- mask --------------------------------------*/


/********************************** file ***************************************/
function getFilePostfix(oFile)
{
    if(oFile == null)
        return null;
    var pattern = /(.*)\.(.*)$/gi;
    if(typeof(oFile) == "object")
    {
        if(oFile.value == null || oFile.value == "")
            return null;
        var arr = pattern.exec(oFile.value);
        return RegExp.$2;
    }
    else if(typeof(oFile) == "string")
    {
        var arr = pattern.exec(oFile);
        return RegExp.$2;
    }
    else
        return null;
}
/*--------------------------------- file --------------------------------------*/

function ahaCountWords (this_field, show_word_count, show_char_count) {
	if (show_word_count == null) {
		show_word_count = true;
	}
	if (show_char_count == null) {
		show_char_count = false;
	}
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var newfullStr="";

	// Add Space before Chinese
	for(var i=0;i<fullStr.length;i++){
		var codes=fullStr.charCodeAt(i);
		if(codes<255){
			newfullStr += fullStr.charAt(i);
		} else {
			newfullStr = newfullStr + " " + fullStr.charAt(i) + " ";
		}
	}
	fullStr = newfullStr;
	// End of Add Space

	//var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	//var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	//var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	//var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");

	fullStr = fullStr.replace("\r", " ");
	fullStr = fullStr.replace("\n", " ");

	var initial_whitespace_rExp = /\s+/g;
	fullStr = fullStr.replace(initial_whitespace_rExp, " ");

	var initial_whitespace_rExp = /^\s*|\s*$/g;
	var cleanedStr = fullStr.replace(initial_whitespace_rExp, "");

	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length;
	if (fullStr.length <2) {
		word_count = 0;
	}
	if (word_count == 1) {
		wordOrWords = " word";
	} else {
		wordOrWords = " words";
	}
	if (char_count == 1) {
		charOrChars = " character";
	} else {
		charOrChars = " characters";
	}
	if (show_word_count & show_char_count) {
		alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
	} else {
		if (show_word_count) {
			alert ("Word Count:  " + word_count + wordOrWords);
		} else {
			if (show_char_count) {
				alert ("Character Count:  " + char_count + charOrChars);
			}
		}
	}
	return word_count;
}


