function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}
// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

function DoSelectUserType(type){
   var myFomr = document.getElementById("RegisterForm");

   myFomr.userType.value=type;
   myFomr.submit();
}


function AddAll(){
 var tableForm = document.getElementById("tableListForm");
 if(tableForm != null){
  tableForm.action="/AddToList.do";
  tableForm.submit();
 }
}
function DoSelectAll(){
 var eles = document.getElementsByName("selected");
 if(eles != null){
  for(var i=0;i<eles.length;i++){
    var ele = eles[i];
    ele.checked=true;
  }
 }
}

function DoUnSelectAll(){
 var eles = document.getElementsByName("selected");
 if(eles != null){
  for(var i=0;i<eles.length;i++){
    var ele = eles[i];
    ele.checked=false;
  }
 }

}

function doDeleteSelect(){
    var myform = document.getElementById("DeleteForm");
    if(myform != null){
      if(confirm("Delete All Selected ?")){
        myform.submit();
      }
    }
}

function doInquireSelect(){
    var myform = document.getElementById("DeleteForm");
    if(myform != null){
      myform.action ="newMessage.do";
      myform.submit();
    }
}

function changeCategoryPath(id,name,currentID,rootID){
    var url = "SelectCategory.do?idName="+id+"&pathName="+name+"&currentID="+currentID+"&rootID="+rootID;
    window.open(url, "CategoryTree", "status=yes,scrollbars=yes,toolbar=no,menubar=no,width=500,height=600,resizable=1");
}


function SelectCategory(id,name,rootID){
    var currentID = document.getElementById(id).value;
    var url = "SelectCategory.do?idName="+id+"&pathName="+name+"&currentID="+currentID+"&rootID="+rootID;
    window.open(url, "SelectCategory", "status=yes,scrollbars=auto,toolbar=no,menubar=no,width=700,height=300,resizable=1");
}

function SelectCategoryMuti(id,name,rootID){
    var currentID = document.getElementById(id).value;
    var url = "SelectCategoryMuti.do?idName="+id+"&pathName="+name+"&currentID="+currentID+"&rootID="+rootID;
    //window.open(url, "SelectCategoryMuti", "status=yes,scrollbars=auto,toolbar=no,menubar=no,width=700,height=300,resizable=1");
    newWindow(url,"selectcategory",820,300);
}

function SelectCategoryMutiForAd(id,name,rootID,closeFunction){
    var currentID = document.getElementById(id).value;
    var url = "SelectCategoryMuti.do?idName="+id+"&pathName="+name+"&currentID="+currentID+"&rootID="+rootID+"&closeFunction="+closeFunction+"&showRoot=true";
//    window.open(url, "SelectCategoryMuti", "status=yes,scrollbars=auto,toolbar=no,menubar=no,width=700,height=300,resizable=1");
    newWindow(url,"selectcategory",820,300);
}
function SelectContact(id,name){
    var currentID = document.getElementById(id).value;
    var url = "SelectContact.do?id="+id+"&name="+name+"&currentID="+currentID;
//    window.open(url, "SelectCategory", "status=yes,scrollbars=auto,toolbar=no,menubar=no,left=100,top=100,width=500,height=600,resizable=1");
    newWindow(url,"selectcategory",700,400);
}

function addAttach(attType,tempID){
    var url = "upload.do?type="+attType+"&ID="+tempID;
    window.open(url, "attachFile", "status=yes,scrollbars=auto,toolbar=no,menubar=no,left=100,top=100,width=500,height=600");
}

var win = null;
function newWindow(mypage,myname,w,h) {
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;

  var settings = 'height='+h+',menubar=no,toolbar=no,scrollbars=1,resizable=1,';
  settings  = settings+'width='+w+',';
  settings  = settings+'top='+wint+',';
  settings  = settings+'left='+winl+',';

  win = window.open(mypage,myname,settings);
  win.window.focus();
}

function SelectCompany(id,name){
    var currentID = document.getElementById(id).value;
    var url = "SelectCompany.do?idName="+id+"&pathName="+name+"&currentID="+currentID;
  //  window.open(url, "SelectCompany", "status=yes,scrollbars=auto,toolbar=no,menubar=no,left=100,top=100,width=500,height=600,resizable=1");
   newWindow(url,"selectcategory",500,600);
}


function doModify(){
  var myform = document.getElementById("editForm");
  myform.admAction.value = "edit";
  myform.submit();
}

function doSubmit(){
  var myform = document.getElementById("editForm");
  myform.admAction.value = "live";
  myform.submit();
}


function doReSubmit(){
  var myform = document.getElementById("editForm");
  myform.admAction.value = "resubmit";
  myform.submit();
}

function doADSubmit(){
 countTotal();
  var myform = document.getElementById("editForm");
  myform.submit();
}
function doADReSubmit(){
 countTotal();
  var myform = document.getElementById("editForm");
  myform.admAction.value = "resubmit";
  myform.submit();
}
function doDelete(){
  var myform = document.getElementById("editForm");
  myform.admAction.value = "delete";
  myform.submit();
}

function doReject(){
  var myform = document.getElementById("editForm");
  myform.admAction.value = "reject";
  myform.submit();
}

function showDetail(comID){

 var comList = document.body.getElementsByTagName("div");

  for( var i=0;i<comList.length;i++){
      var com = comList[i];

      if(com != null && com.id == comID){
        if("" ==com.style.display){
          com.style.display = "none";
        }else{
          com.style.display = "";
        }
      }
  }
}
function showDetailTR(comID){

 var comList = document.body.getElementsByTagName("tr");

  for( var i=0;i<comList.length;i++){
      var com = comList[i];
      if(com != null && com.id == comID){
        if("" ==com.style.display){
          com.style.display = "none";
        }else{
          com.style.display = "";
        }
      }
  }

 var comList = document.body.getElementsByTagName("button");

  for( var i=0;i<comList.length;i++){
      var com = comList[i];
      if(com != null && com.id == comID){
        if("" ==com.style.display){
          com.style.display = "none";
        }else{
          com.style.display = "";
        }
      }
  }
  var eledown = document.getElementById(comID+"down");
  var eleclose = document.getElementById(comID+"close");
  if("" ==eleclose.style.display){
     eleclose.style.display="none";
  }else{
     eleclose.style.display = "";
  }

  if("" ==eledown.style.display){
     eledown.style.display="none";
  }else{
     eledown.style.display = "";
  }

}

function checkIsAddOption(objectID,id){
   var isAdd = false;
   var selectedObj = document.getElementById(objectID)
   var slength = selectedObj.options.length;//from select value
   for(var j=0 ; j< slength ; j++){

      if(id == selectedObj.options[j].value){
        isAdd = true;
        break;
      }
   }
   return isAdd;
}

function addOption(objectID,id,name){
         if(!checkIsAddOption(objectID,id)){
           var selectedObj = document.getElementById(objectID);
           if(selectedObj != null){
             var myoption = new Option(name,id);
             selectedObj.add(myoption,"-1");
           }
        }
}

function removeSelectedOption(objectID){
   var selectedObj = document.getElementById(objectID)
   var slength=selectedObj.options.length;//from select value
   for(var j=slength-1 ; j>=0 ; j--){
     if(selectedObj.options[j].selected){
       selectedObj.remove(j);
     }
   }
}


function doSaveSelectValue(objectID,saveID,SaveName){

  var idObj = opener.document.getElementById(saveID);
  var pathObj = opener.document.getElementById(SaveName);
  var selectedObj = document.getElementById(objectID)
   var slength=selectedObj.options.length;//from select value
   var id="";
   var name ="";
   for(var j=0;j<slength;j++){
     var text = selectedObj.options[j].text;
     var value = selectedObj.options[j].value;
     id= id+value+";";
     name= name+text+"\n";
   }
   idObj.value = id;
   pathObj.value = name;

}

    function openDateWindowSpecified (date_string, recall_method) {
        var url = "MonthlyCalendar.do?";
        var date = date_string.split("/");
        var year = date[0];
        var month = date[1];
        var day = date[2];

        url = url + "year=" + year + "&month=" + month + "&date=" + day + "&recallMethod=" + recall_method;

        var calFrame = document.getElementById("MCalendar");
        calFrame.style.display = "block";
        calFrame.src = url;
    }


    function openDateWindow(recall_method) {
        var url = "MonthlyCalendar.do?";

        url = url + "recallMethod=" + recall_method;
        var calFrame = document.getElementById("MCalendar");
        calFrame.src = url;
        calFrame.style.display = "block";
    }











    function subString(orgString, endindex) {
        var newString = "";

        var result = 0;
        for (var i = 0 ; i < orgString.length ; i++) {
            if ( orgString.charCodeAt(i) >= 128 ) {
                 // Non 7-bit char, count 2 bytes
                 result += 2;
            } else {
                 // 7-bit char
                 result++;
            }

            if (result < endindex) {
                newString = newString + orgString.charAt(i);
            }
        }

        return newString;

    }


    function byte_length(st) {
       // For single byte browser, string length = no. of byte
       if ('??'.length == 2)  return st.length;

       // For double byte browser
       var result = 0;
       for (var i = 0 ; i < st.length ; i++) {
          if ( st.charCodeAt(i) >= 128 ) {
             // Non 7-bit char, count 2 bytes
             result += 2;
          } else {
             // 7-bit char
             result++;
          }
       }

       return result;
    }

    function disableSubmitButtonClick() {
        return ;
    }

    function disableSubmitButton(){
        document.onmousedown = disableSubmitButtonClick;
        document.onkeydown = disableSubmitButtonClick;
    }



    /*******************************************************************/
    /***                                                             ***/
    /***   Tokenizer.js - JavaScript String Tokenizer Function       ***/
    /***                                                             ***/
    /***   Version   : 0.2                                           ***/
    /***   Date      : 01.05.2005                                    ***/
    /***   Copyright : 2005 Adrian Zentner                           ***/
    /***   Website   : http://www.adrian.zentner.name/               ***/
    /***                                                             ***/
    /***   This library is free software. It can be freely used as   ***/
    /***   long as this this copyright notice is not removed.        ***/
    /***                                                             ***/
    /*******************************************************************/

    String.prototype.tokenize = tokenize;

    function tokenize() {
        var input             = "";
        var separator         = " ";
        var trim              = "";
        var ignoreEmptyTokens = true;

        try {
            String(this.toLowerCase());
        } catch(e) {
            window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);");
            return;
        }

        if(typeof(this) != "undefined") {
            input = String(this);
        }

        if (typeof(tokenize.arguments[0]) != "undefined") {
            separator = String(tokenize.arguments[0]);
        }

        if (typeof(tokenize.arguments[1]) != "undefined") {
            trim = String(tokenize.arguments[1]);
        }

        if (typeof(tokenize.arguments[2]) != "undefined") {
            if (!tokenize.arguments[2]) {
                ignoreEmptyTokens = false;
            }
        }

        var array = input.split(separator);

        if (trim) {
            for(var i=0; i<array.length; i++) {
                while (array[i].slice(0, trim.length) == trim) {
                    array[i] = array[i].slice(trim.length);
                }
                while (array[i].slice(array[i].length-trim.length) == trim) {
                    array[i] = array[i].slice(0, array[i].length - trim.length);
                }
            }
        }

        var token = new Array();
        if (ignoreEmptyTokens) {
            for(var i=0; i<array.length; i++) {
               if (array[i] != "") {
                   token.push(array[i]);
               }
            }
        } else {
             token = array;
        }

        return token;
    }

    /*******************************************************************/
    /***  above is tokenizer.                                        ***/
    /*******************************************************************/



    function clearValue(field1, field2) {
        var field1 = document.getElementById(field1);
        var field2 = document.getElementById(field2);
        field1.value = "";
        field2.value = "";
    }

   function highlightTableRows(tableId) {
       var previousClass = null;

       var table = document.getElementById(tableId);
       if(table != null){
                  //border="0" cellpadding="0" cellspacing="0"

              var tbody = table.getElementsByTagName("tbody")[0];
              if (tbody == null) {
                 var rows = table.getElementsByTagName("tr");
              } else {
                 var rows = tbody.getElementsByTagName("tr");
              }
               if(rows != null){
                   for (i=0; i < rows.length; i++) {
                     rows[i].onmouseover = function() {this.style.backgroundColor="#eaeaea";

                     };
                     rows[i].onmouseout = function() {this.style.backgroundColor="";

                      };
                   }
               }
      }
 }

 function checkLoginDefautl(myText){
  if(myText.value =="Your ID"){
    myText.value ="";
  }
 }
 function checkEmailDefault(myText){
  if(myText.value =="Your email address"){
    myText.value ="";
  }
 }
  function DisableEnableForm(xForm,xHow){
    objElems = xForm.elements;
    for(i=0;i<objElems.length;i++){
      objElems[i].disabled = xHow;
    }
  }

  function NewMessage(value){
    var url = "/newMessage.do?selected="+value;
    location.href=url;
  }

  function PrintMsg(){
  window.print();
  }

function showHidden(id){
   var hiddenObj = document.getElementById(id);
   hiddenObj.style.display="";
  }

function GotoPage(toPage){
location.href=toPage;
}
/*
    <textarea cols="60" rows="5" id="toSellName" name="toSellName" readonly="readonly" class="form_textarea_field" ></textarea><button  onclick="SelectCategoryMuti('toSell','toSellName','CC_ROOT')" type="button" >Select</button>
<input type="hidden" name="toSell" value="" id="toSell" >
      <textarea cols="60" rows="5" id="toBuyName" name="toBuyName" readonly="readonly" class="form_textarea_field" ></textarea><button  onclick="SelectCategoryMuti('toBuy','toBuyName','CC_ROOT')" type="button" >Select</button>
<input type="hidden" name="toBuy" value="" id="toBuy" >
*/
function DoTheSame(myCheck){
  if(myCheck.checked){
    var toSellName = document.getElementById("toSellName").value;
    var toSell = document.getElementById("toSell").value;
    document.getElementById("toBuyName").value=toSellName;
    document.getElementById("toBuy").value=toSell;

  //copy
  }else{
  //clean
  }
}

function SubmitCategoryForm(formID){
  var myForm = document.getElementById(formID);
  if(myForm!=null){
    myForm.submit();
  }
}

function UpgradeToPro(){
  if(confirm("I agree to upgrade as Pro member for US$1500 per year. ")){
      GotoPage("/UpgradeToPro.do");
  }
}
function DeleteAttach(id){
  var mylink = document.getElementById(id+"link");
  if(mylink != null){
    mylink.style.display="none";
    var hiddenValue = document.getElementById(id);
    hiddenValue.value="";
  }

}


function onclickPop() {
	var popUp = window.open(
		'/termsofuse_simple.do',
		'valvebiz_popup',
		'dependent=yes,innerWidth=622,location=no,resizable=yes,scrollbars=yes,width=642'
	);
	popUp.popUp = true;
	//return false;
}


function ResendCompanyEmail(companyID){
    var url ="AdmSendConfirmEmail.do?ID="+companyID;
    window.open(url,'email_popup','dependent=yes,innerWidth=100,location=no,resizable=no,scrollbars=yes,width=200,height=100');
}

