/**
  * common javascript util for jctalentbank
  */

//added by Shirly, 20 July 2006

//checking string value,
//return "" if value is null
//return trim(value) if value is not empty
function checkValue(strValue){
	if(strValue != null && trim(strValue).length > 0) return trim(strValue);
	else return "";
}

//checking combo value
//return "" if value = "[select one]"
//return trim(value) if value != "[select one]"
//return null if value is null
function checkComboValue(strComboValue){
	if(strComboValue != null && trim(strComboValue).length > 0) {
		if( strComboValue == "[select one]" ) return "";
		else return trim(strComboValue);
	}
	else return null;
}

//checking checkbox value,
//return boolean true if checked
//return boolean false if unchecked
function checkChxbox(o){
	if(o.checked == true) return true;
	else return false;
}

//set checkbox value,
//if value = "true" || value = true then checked
//if value = "false" || value = false then unchecked
function setChxbox(bValue, o){
	if(trim( bValue ) == "true" || bValue.toString() == "true" ) o.checked = true;
	else o.checked = false;
}


//checking whether a value is "present" or not
//return "null" if value = "present" or value = ""
//return trim(value) if value is not empty
function setValue(strValue){
    if (trim(strValue).length > 0){
        if (trim(strValue) == "present") return "null";
        else return trim(strValue);
    }
    else return "null";
}

//checking a checkbox
//return String "present" if checked
function checkPresent(oPresentCheckbox, strValue){
    if( checkChxbox(oPresentCheckbox) ) return "present";
    else return strValue;
}

//checking object
//return true if object != null
//return false if object is null
function isNull(o){
    if (o == null) return true;
    else return false;
}

//checking string value
//return true if value != null && trim(strValue) != ""
//return false if value is null
function isNotNull(strValue){
    if(strValue != null && trim(strValue).length > 0) return true;
    else return false;
}

//for enabling/disabling field(s) if a checkbox is unchecked/checked
function doEnableDisableField(oCheckbox, arrObjFields){
	if ( oCheckbox!= null) {
        if( checkChxbox(oCheckbox) ){
            disableField( arrObjFields );
        } else {
            enableField( arrObjFields )
        }
    } else {
        alert("Null object");
    }
}

//enable field text, checkbox & combobox
// @TODO redundant with disable field, make them one function with additional param of type boolean to determine disabling or enabling
function enableField( arr )
{
  if( arr != null && arr.length > 0 ) {
    var oField = new Object();
    for( i = 0; i<arr.length; i++ ) {
      oField = arr[i];
      if( oField.type == 'text' ) {
        oField.readOnly = false;
      }
//      if( oField.type == 'checkbox' || oField.type == 'combo' ) {
        else{
        oField.disabled = false;
      }
    }
  }
}

//disable field text, checkbox & combobox
function disableField( arr )
{
  if( arr != null && arr.length > 0 ) {
    var oField = new Object();
    for( i = 0; i<arr.length; i++ ) {
      oField = arr[i];
      if( oField.type == 'text' ) {
        oField.readOnly = true;
      }
//      if( oField.type == 'checkbox' || oField.type == 'combo' ) {
        else{
            oField.disabled = true;
      }
    }
  }
}

function createLoopCombo(oSelect, loop, value, defaultValue){
    var oOption = document.createElement("OPTION");
    oOption.value = "";
    oOption.text = defaultValue;
    oSelect.options.add(oOption);
    for(i = value; i >= value-loop; i--)
    {
        oOption = document.createElement("OPTION");
        oOption.value = i;
        oOption.text = i;
        oSelect.options.add(oOption);
    }
}

// used when press ENTER in input form.
// example: <input type="text" name="skillName" size="40" maxlength="200" onkeypress="getKeyEvent(event, this.form.add)">
// this.form.add --> button yang akan di click ketika press ENTER.
function getKeyEvent(evt, o)
{
  key = evt.keyCode;
  if(key == 13)
  {
    o.click();
    if (document.all) // if Internet Explorer
    {
      window.event.returnValue = false;
    }
    else // other browsers
    {
      evt.cancel = true;
      evt.preventDefault();
      return false;
    }
  }
}

function isValidCompletedYear(strValue,birthDate) {
        var yy = birthDate.substring(0,4);
        var days = new Date();
        var gyear = days.getFullYear();

//        alert("parseInt(strValue) = " + parseInt(strValue) + "\nparseInt(yy) = " + parseInt(yy) + "\nparseInt(gyear) = " + parseInt(gyear))
        if((parseInt(strValue) > parseInt(yy)) && (parseInt(strValue) <= parseInt(gyear)))
            return true;
        else
            return false;
}

function getAge(o) {
    if(o.birthDate.value == '' || o.birthDate.value == null) return 0;
    var strarr = o.birthDate.value.split('/',3);
    // strip the leading '0'
    if(strarr[0].substring(0,1)=='0') {
        strarr[0] = strarr[0].substring(1);
    }
    if(strarr[1].substring(0,1)=='0') {
        strarr[1] = strarr[1].substring(1);
    }

    var yy = parseInt(strarr[2]);// getYear of birth
    var mm = parseInt(strarr[1]);// getMonth  of birth
    var dd = parseInt(strarr[0]);
    var days = new Date();
    var gdate = parseInt(days.getDate());
    var gmonth = parseInt(days.getMonth())+1;
    var gyear = parseInt(days.getFullYear());
    var age = gyear-yy;

    if(age <= 0) {
        alert("Birth Date must be smaller than this year.");
        age = 0;
    } else {
        if((gmonth < mm) || (gmonth == mm && gdate < dd)) {
          age = age - 1;
        }
    }
    o.age.value = age;
   }
/**
 * add by harir (29-sept-06)
 * dapatkan usia berdasarkan tanggal lahir.
 */
//function getAge(birthdate)
//{
//    if (birthdate == null)
//        return 0;
//    var iCek = 0;
//    Calendar oCalSekarang = Calendar.getInstance();
//    oCalSekarang.setTime(new Date());
//    Calendar oCalLahir = Calendar.getInstance();
//    oCalLahir.setTime(birthdate);
//    var iTahunSekarang = oCalSekarang.get(Calendar.YEAR);
//    var iTahunLahir = oCalLahir.get(Calendar.YEAR);
//    var iHasil = iTahunSekarang - iTahunLahir;
//    oCalLahir.add(oCalLahir.YEAR, iHasil);
//
//    if(oCalSekarang.before(oCalLahir))
//    // tanggal lahir lebih besar dari sekarang
//        iCek = 1;
//    if(iCek == 1)
//    {
//                iHasil = iHasil - 1;
//                oCalSekarang.add(Calendar.MONTH,6);
//                if(oCalSekarang.after(oCalLahir))
//                    iHasil = iHasil + 1;
//                if(oCalSekarang.equals(oCalLahir))
//                    iHasil = iHasil + 1;
//    }
//    if(iHasil == 0)
//        iHasil = 1;
//    return iHasil;
//}
