//
//  check whether str is number
//
function isNumber(field, errorMsg) {
  str = String(field.value);

  if (str.length == 0) {
    if (errorMsg != null && errorMsg != '') alert(errorMsg);
    return false;
  }

  refString="1234567890";

  for (i = 0; i < str.length; i++)  {
    tempChar = str.substring(i, i + 1);
    if (refString.indexOf(tempChar) == -1) {
      if (errorMsg != null && errorMsg != '') alert(errorMsg);
      field.focus();
      return false;
    }
  }
  return true;
}

function isSpace(str, pos) {
    curChar = str.charAt(pos);
    return ( curChar == ' ' || curChar == '\x09' || curChar == '\xA0' ||  // xA0 - 160 - code for &nbsp;
             curChar == '\x0D' || curChar == '\x0A' );
}

//
//  removes leading and trailing spaces/tabs/CRs/LFs from string strToTrim
//
function strTrim(strToTrim) {
    strToTrim = String(strToTrim);
    if (strToTrim == "")
        return "";
    iStart = 0;
    while ((isSpace(strToTrim, iStart)) && 
            iStart < strToTrim.length - 1)
        iStart++;
    iEnd = strToTrim.length - 1;
    while ((isSpace(strToTrim, iStart)) &&
            iEnd >= iStart)
        iEnd--;
    return strToTrim.substr(iStart, iEnd - iStart + 1);
}


function isEmpty(field, errorMsg) {
    value = field.value;
    if (String(field.type).indexOf('select') != -1)
        if ((value == "-1") || (value == "All") || (value == "0"))
            value = "";

    if (strTrim(value) == "") {
        if (errorMsg != '')
            alert(errorMsg);
        field.focus();
        return true;
    }
    else
        return false;
}

function roundNumber(number, places) {

	var newNumber = Math.round(number * Math.pow(10, places)) / Math.pow(10, places);
	var strNumber = newNumber.toString();
	var add;
	var pos = strNumber.indexOf(".");
	if (pos == -1) {
	    strNumber += ".";
		add = places;
	} else {
		add = places + pos - strNumber.length + 1;
	}
	//alert(strNumber + 'pos =' + pos + ' add=' + add);
	for (i = 0; i < add; i++) {
		strNumber += "0";
	}
	return strNumber;
}

var focusInputField;
focusInputField = null;
function pageLoaded() {
	if (focusInputField != null) {
		focusInputField.focus();
	}
}

function setFocusField(field) {
	focusInputField = field;
}

function isEmail(field, errorMsg) {
  var value = field.value;
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(value)) {
      if (errorMsg != null && errorMsg != '') alert(errorMsg);
      field.focus();
      return false;
  }
  return true;
}


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedRadioBoxValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function isRadioBoxChecked(radioObj, errorMsg) {

    if (getCheckedRadioBoxValue(radioObj) == "") {
        if (errorMsg != '')
            alert(errorMsg);
	    return false;
    }
    return true;

}

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)};

function isCheckboxByTypeChecked(form, type, errorMsg) {
    for(i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].type == "checkbox") {
        	if (form.elements[i].name.startsWith(type)) {
        		if (form.elements[i].checked) return true;
        	}
        }
	}

    if (errorMsg != '')
        alert(errorMsg);

	return false;
}


