function search() {
    var el = document.ssearch.elements;
   if( isEmpty( el['keyword'].value ) ) {
      alert("Please input a keyword");
      el['keyword'].focus();
      return false;
   }
}	

function check_advanced_seach() {
    var el = document.asearch.elements;
   if( isEmpty( el['keyword'].value ) ) {
      alert("Please select a keyword");
      el['keyword'].focus();
      return false;
   }
}	

function check_gcash_terms() {
    var el = document.gcash.elements;
      if (!el['terms'].checked) {
         alert("You must accept the Terms and Conditions before you can buy an item");
         return false;
      }    
}

function loadsubcat(obj, frmname) {
        //alert('hello ' + obj.options.selectedIndex);
        var subcat_array = eval('subcats_' + obj.options.selectedIndex);
        var count = 0;

   //for (i = 0; i < document.postad.subcats.options.length; i++) {
      eval('document.' + frmname + '.subcats.options.length = 0');
   //}
        while (subcat_array[count]) {
                opt = new Option(subcat_array[count][1], subcat_array[count][0]);
                eval('document.' + frmname + '.subcats.add(opt)');
                count++;
        }
        if (eval('document.' + frmname + '.subcats.options.length == 0')) {
           eval('document.' + frmname + '.subcats.disabled = true');
        } else {
                eval('document.' + frmname + '.subcats.disabled = false');
        }
}

function check_main_search() {
        var el = document.frm_search.elements;
   if( isEmpty( el[0].value ) ){
      alert("Please select a category");
      el[0].focus();
      return false;
   }
   if( isEmpty( el[4].value ) ){
      alert("Please enter a search keyword");
      el[4].focus();
      return false;
   }
   return true;
}

function add_comment() {
   var el = document.add_comment.elements;

   if( isEmpty( el['comments'].value ) ) {
      alert("Please type your comment!");
      el['comments'].focus();
      return false;
   }
   
   return true;
   
}

function check_new_post() {
   var el = document.postad.elements;

   
   /*
   for (i=0; i < el.length; i++)
   {
      alert(el[i].name + ' = ' + el[i].value);
   }
   alert(el['adinfo[service_category]'][0].checked);
   
   return false;
   */

   if( isEmpty( el['adinfo[title]'].value ) ) {
      alert("Please select a title");
      el['adinfo[title]'].focus();
      return false;
   }

   //if( isEmpty( el['adinfo[description]'].value ) ) {
   //   alert("Please type your description");
   //   el['adinfo[description]'].focus();
   //   return false;
   //}

   if( ( el['adinfo[cat_id]'].value  == 0) ){
      alert("Please select a category");
      el['adinfo[cat_id]'].focus();
      return false;
   }

   if( ( el['adinfo[sub_cat_id]'].value == 0) ){
      alert("Please select a subcategory");
      el['adinfo[sub_cat_id]'].focus();
      return false;
   }

   if( isEmpty( el['adinfo[title]'].value ) ) {
      alert("Please select a title");
      el['adinfo[title]'].focus();
      return false;
   }

   if ( (el['adinfo[service_category]'][0].selected)) {

      if (( isEmpty ( el['adinfo[bid_increment]'].value ) || !isNumeric( el['adinfo[bid_increment]'].value ) || el['adinfo[bid_increment]'].value == 0 )) {
         alert("Please select a bid increment");
         el['adinfo[bid_increment]'].focus();
         return false;
      }

      if( isEmpty( el['adinfo[start_price]'].value ) || (parseFloat(el['adinfo[start_price]'].value) != el['adinfo[start_price]'].value) || ( el['adinfo[start_price]'].value == 0 )) {
         alert("Please enter a numeric value for starting price");
         el['adinfo[start_price]'].focus();
         return false;
      }
   } else if ( (el['adinfo[service_category]'][1].selected)) {
      if( isEmpty( el['adinfo[pprice]'].value ) || (parseFloat(el['adinfo[pprice]'].value) != el['adinfo[pprice]'].value) || ( el['adinfo[pprice]'].value == 0 )) {
         alert("Please enter a numeric value for starting price");
         el['adinfo[pprice]'].focus();
         return false;
      }
   }
   if( ( el['adinfo[location1]'].value  == 0) ) {
      alert("Please select a province");
      el['adinfo[location1]'].focus();
      return false;
   }

   if (!el['terms'].checked) {
      alert("You must accept the Terms and Conditions before you can post an item");
      return false;
   }

   if( leftTrim( el['adinfo[title]'].value ) == '' ) {
      alert("Please select a title");
      el['adinfo[title]'].focus();
      return false;
   }   
   
   return true;
}

function leftTrim(sString) {
   while (sString.substring(0,1) == ' ') {
      sString = sString.substring(1, sString.length);
   }
   return sString;
}

function check_lf() {
        var el = document.lf.elements;

   if( isEmpty( el[2].value ) ) {
      alert("Please type your item description");
      el[2].focus();
      return false;
   }

   if( isEmpty( el[4].value ) ) {
      alert("Please select a sub_category");
      el[4].focus();
      return false;
   }

   if (!el['terms'].checked) {
      alert("You must accept the Terms and Conditions before you can post an item");
      return false;
   }
   return true;
}


function check_main_search1() {
        var el = document.frm_search.elements;
   if( isEmpty( el[0].value ) ){
      alert("Please enter a search keyword");
      el[0].focus();
      return false;
   }
   return true;
}

function quick_message(adid, message){
        window.open(
           'quickmessage.php?adid=' + adid + '&message=' + message,
           'editad',
           'directories=no,top=50,left=80,width=501,height=320,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,status=yes'
        );
}

function init_msg_counter() {
   document.FormMessage.txtcount.value = 160 - (document.FormMessage.message.value.length.valueOf());
}


function send_message(adid, owner){
        window.open(
           'quickmessage.php?adid=' + adid + '&owner=' + owner,
           'viewer',
           'directories=no,top=60,left=80,width=500,height=310,location=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,status=yes'
        );
}

function bid_item(adid, owner){
        window.open(
           'bid.php?adid=' + adid + '&owner=' + owner,
           'viewer',
           'directories=no,top=60,left=80,width=500,height=310,location=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,status=yes'
        );
}

function buy_now(adid, owner, buynow_price){
        window.open(
           'buynow.php?adid=' + adid + '&owner=' + owner + '&buynow_price=' + buynow_price,
           'viewer',
           'directories=no,top=60,left=80,width=500,height=310,location=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,status=yes'
        );
}

function view_feedback(owner){
        window.open(
           'viewpopfeedback.php?owner=' + owner,
           'viewer',
           'directories=no,top=60,left=80,width=658,height=310,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,status=yes'
        );
}

function edit_ads(adid){
        window.open(
           'editpopads.php?adid=' + adid,
           'viewer',
           'directories=no,top=60,left=80,width=610,height=310,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,status=yes'
        );
}

function view_ads(adid, userid){
        window.open(
           'view_ads.php?adid=' + adid + '&userid=' + userid,
           'viewer',
           'directories=no,top=60,left=80,width=610,height=310,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,status=yes'
        );
}

function ClearAll() {
        var ml = document.forms[2];
        var len = ml.elements.length;
        for (var i = 0; i < len; i++) {
           var e = ml.elements[i];
           if (e.type == "checkbox") {
                   Clear(e);
           }
        }
        ml.checkall.checked = false;
}

function AllChecked() {
        ml = document.forms[2];
        len = ml.elements.length;
        for(var i = 0 ; i < len ; i++) {
           if (ml.elements[i].type == "checkbox" && !ml.elements[i].checked) {
                   return false;
           }
        }
        return true;
}

function ToggleAll(e) {
        if (e.checked)
            CheckAll();
        else
            ClearAll();
}

function Check(e) {
        e.checked = true;
}

function check_checks(formobj) {
        var num = numChecks(formobj);
        if (num < 1) {
           alert('Use the checkboxes to select ad items.');
           return false;
        } else {
           return true;
        }
}
