function addOption(selectObj, optionId, optionName) {
   newOption = new Option(optionName, optionId);
   selectObj.options[selectObj.options.length] = newOption;
}

function removeOption(selectObj){
   while(selectObj.length>1){
      selectObj.options.remove(selectObj.length-1);
   }
}

function removeSelectOption(selectObj){

   for(i=selectObj.length-1;i>=0;i--){
      if(selectObj[i].selected == true){
      	 selectObj.options.remove(i);
      }
   }
}

function toCheckEmail(textObj){
   if(textObj.trim() == "" ){
      return 0;
   }else{
     if( textObj.indexOf('@') == -1
       ||textObj.indexOf('.') == -1){
        return 0;
     }
   }
   return 1;
}

function checkNUM(NUM){
 var i,j,strTemp;
 strTemp="0123456789";
 if ( NUM.length== 0)
   return 0
 for (i=0;i<NUM.length;i++) {
   j=strTemp.indexOf(NUM.charAt(i));
   if (j==-1){
     return 0;
   }
 }

 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 checkIsAdd(selectObj,value){
   for(i=selectObj.length-1;i>=0;i--){
      if(selectObj[i].value == value){
      	 return 1;
      }
   }

   return 0;
}

function selectAll(selectObj){
   for(i=selectObj.length-1;i>=0;i--){
      selectObj[i].selected = true;
   }
}
function isCheckAll(checkObj,isChecked){
	if(checkObj){
	   if(checkObj.length == undefined ){
	     checkObj.checked=isChecked;
	   }else{
	     for(i=0;i<checkObj.length;i++){
	        checkObj[i].checked=isChecked;
	     }
	   }
	}
}

function toggleShow(idName, bShow) {
  if(document.all[idName]) {
    if (bShow){
      document.all[idName].style.display = "inline";
    }else{
      document.all[idName].style.display = "none";
    }
  }
}

function disableAll(theForm){
	for(i=0;i<theForm.length;i++){
		theForm[i].disabled = true;
	}
}

function showCalender(obj){
   document.all.ADCalendar.Open(obj.name);
}

function HTMLEncode( text ){
   text = text.replace(/&/g, "&amp;") ;
   text = text.replace(/"/g, "&quot;") ;
   text = text.replace(/</g, "&lt;") ;
   text = text.replace(/>/g, "&gt;") ;
   text = text.replace(/'/g, "&#39;") ;

   return text ;
}

function sortMe(oSel){
	var ln = oSel.options.length;
	var arr = new Array();

	for (var i = 0; i < ln; i++){
	  arr[i] = oSel.options[i].value;

	}

	arr.sort();

	while (ln--)	{
	  oSel.options[ln] = null;
	}

	for (i = 0; i < arr.length; i++){
	  oSel.add (new Option(arr[i], arr[i]));
	}
}

function hasCheckItem(checkObj){
	var result = false;
	if(checkObj){
	   if(checkObj.length == undefined ){
	      if(checkObj.checked){
	      	 result = true;
	      }
	   }else{
	     for(i=0;i<checkObj.length;i++){
	        if(checkObj[i].checked){
	           result = true;
	           break;
	        }
	     }
	   }
	}
	return result;
}

function getCheckedValue(obj){
   if(obj){
      if(obj.length>1){
         for(var i=0;i<obj.length;i++){
           if(obj[i].checked){
              return obj[i].value;
           }
         }
      }else{
         if(obj.checked){
              return obj.value;
         }
      }
   }
}

function initChecked(obj,val){
   if(obj){
      if(obj.length>1){
         for(var i=0;i<obj.length;i++){
           if(obj[i].value == val){
              obj[i].checked = true;
           }
         }
      }else{
         if(obj.value == val){
            obj.checked = true;
         }
      }
   }
}

function initSelected(obj,val){
   if(obj){
      if(obj.length>1){
         for(var i=0;i<obj.length;i++){
           if(obj[i].value == val){
              obj[i].selected = true;
           }
         }
      }else{
         if(obj.value == val){
            obj.selected = true;
         }
      }
   }
}

function openAttachment(fileId){
	window.open("/do/adm/fileBlobManage?method=preload&objId="+fileId,"","");
}

function chkdate(datestr){
   var lthdatestr = 0;

   if (datestr != ""){
      lthdatestr = datestr.length ;
   }else{
      lthdatestr = 0;
   }

   var tmp_yyyy = "";
   var tmp_mm = "";
   var tmp_dd = "";

   var status;
   status = 0;

   if ( lthdatestr== 0 || lthdatestr < 10){
      //alert("length too short");
      return 0
   }

   tmp_yyyy = datestr.substring(0,4);
   //alert("tmp_yyyy==>"+tmp_yyyy);

   tmp_mm = datestr.substring(5,7);
   //alert("tmp_mm==>"+tmp_mm);

   tmp_dd = datestr.substring(8);
   //alert("tmp_dd==>"+tmp_dd);

   var year = tmp_yyyy;
   if(!checkNUM(year)){
      return 0;
   }
   var month = tmp_mm;
   if(!checkNUM(month)){
      return 0;
   }
   var day = tmp_dd;
   if(!checkNUM(day)){
      return 0;
   }

   if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) ){
      //alert ("Invalid month or day!");
      return 0;
   }
   if (!((year % 4)==0) && (month==2) && (day==29)){
      //alert ("This is not a leap year!");
      return 0;
   }
   if ((month<=7) && ((month % 2)==0) && (day>=31)){
      //alert ("This month is a small month!");
      return 0;
   }
   if ((month>=8) && ((month % 2)==1) && (day>=31)){
      //alert ("This month is a small month!");
      return 0;
   }
   if ((month==2) && (day==30)){
      //alert("The Febryary never has this day!");
      return 0;
   }

   return 1;
}

function simpleToResizeFrame(frameId){
  var frameObj = parent.document.getElementById(frameId);
  var defaultHeight = 410;
  frameObj.height = String(defaultHeight+"px");
  var frameObjHeight;
  if (document.body.scrollHeight){ // IE
    frameObjHeight = document.body.scrollHeight;
  }else if (document.body.offsetHeight){ // NetScape
    frameObjHeight = document.body.offsetHeight;
  }

  frameObj.height = String(frameObjHeight+"px");
}

