function trim(sString) {
   while (sString.substring(0,1) == ' ') {
      sString = sString.substring(1, sString.length);
   }
   while (sString.substring(sString.length-1, sString.length) == ' ') {
      sString = sString.substring(0,sString.length-1);
   }
   return sString;
}

function in_array(needle, haystack) {
   var n= haystack.length;
   for (var i=0;i<n;i++) {
      if (haystack[i]==needle) {
         return true;
      }
   }
   return false;
}

function getScreenWidthHeight() {
   var myWidth = 0, myHeight = 0;
   if (typeof(window.innerWidth) == 'number') {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
   }
   else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   }
   else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }
   return [myWidth,myHeight];
   //window.alert( 'Width = ' + myWidth );
   //window.alert( 'Height = ' + myHeight );
}

function getScrollXY() {
   var scrOfX = 0, scrOfY = 0;
   if (typeof(window.pageYOffset) == 'number') {
      //Netscape compliant
      scrOfY = window.pageYOffset;
      scrOfX = window.pageXOffset;
   }
   else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
      //DOM compliant
      scrOfY = document.body.scrollTop;
      scrOfX = document.body.scrollLeft;
   }
   else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
      //IE6 standards compliant mode
      scrOfY = document.documentElement.scrollTop;
      scrOfX = document.documentElement.scrollLeft;
   }
   return [scrOfX,scrOfY];
}

function select_move_rows(ss1,ss2,sortInd) {
   sortInd = sortInd || "NO";
   var selID='';
   var selText='';
   // Move rows from ss1 to ss2 from bottom to top
   //for (i=ss1.options.length-1;i>=0;i--) {
   // Move rows from ss1 to ss2 from top to buttom
   var ss1_length=ss1.options.length;
   for (i=0;i<ss1_length;i++) {
      if (ss1.options[i].selected == true) {
         selID=ss1.options[i].value;
         selText=ss1.options[i].text;
         var newRow = new Option(selText,selID);
         ss2.options[ss2.length]=newRow;
         ss1.options[i]=null;
         ss1_length--;
         i--;
      }
   }
   if (sortInd == "YES") {
      select_sort(ss2);
   }
}

function select_sort(selList) {
   var ID='';
   var text='';
   for (x=0;x<selList.length-1;x++) {
      for (y=x + 1; y < selList.length; y++) {
         if (selList[x].text > selList[y].text) {
            // Swap rows
            ID=selList[x].value;
            Text=selList[x].text;
            selList[x].value=selList[y].value;
            selList[x].text=selList[y].text;
            selList[y].value=ID;
            selList[y].text=Text;
         }
      }
   }
}

function checkNumber(inputValue) {
   var anum=/(^\d+$)|(^\d+\.\d+$)|(^-\d+$)|(^-\d+\.\d+$)/
   if (anum.test(inputValue)) {
      return true;
   }
   else {
      return false;
   }
}

function checkNumeric(controlID) {
   if (trim(document.getElementById(controlID).value) != "") {
      if (!checkNumber(trim(document.getElementById(controlID).value))) {
         document.getElementById(controlID).value = "";
      }
   }
}

function setMandatoryAlert(control_name) {
   var fieldtype = "";
   var obj = document.getElementById(control_name);
   if (document.getElementById(control_name) != null) {
      fieldtype = document.getElementById(control_name).type;
   }
   if (document.getElementById(control_name + '_0') != null) {
      fieldtype = document.getElementById(control_name + '_0').type;
   }
   switch (fieldtype) {
      case "select-one":
         obj.style.background = "red";
         try { obj.focus(); } catch (ignore) {}
         try { window.scrollBy(0,-30); } catch (ignore) {}
         break;
      case "select-multiple":
         obj.style.background = "red";
         try { obj.focus(); } catch (ignore) {}
         try { window.scrollBy(0,-30); } catch (ignore) {}
         break;
      case "text":
      case "textarea":
         obj.style.background = "red";
         try { obj.focus(); } catch (ignore) {}
         try { window.scrollBy(0,-30); } catch (ignore) {}
         break;
      case "checkbox":
         for (var j=0; j<100; j++) {
            if (document.getElementById(control_name + '_' + j) != null) {
               document.getElementById(control_name + '_' + j).style.background = "red";
            }
            else {
               break;
            }
         }
         try { document.getElementById(control_name + '_0').focus(); } catch (ignore) {}
         try { window.scrollBy(0,-30); } catch (ignore) {}
         break;
      case "radio":
         for (var j=0; j<document.all[control_name].length; j++) {
            document.all[control_name][j].style.background = "red";
         }
         try { document.all[control_name][0].focus(); } catch (ignore) {}
         try { window.scrollBy(0,-30); } catch (ignore) {}
         break;
      case "file":
         break;
      case "password":
         break;
      default:
   }
}

function showHideGroupDiv(div_id, div_array) {
   if (document.getElementById(div_id) == null) {
      return;
   }
   for (var i=0; i<div_array.length; i++){
      if (document.getElementById(div_array[i]) != null) {
         if (div_array[i] == div_id) {
            // Show
            if (document.layers && document.layers[div_array[i]]) {
               if (document.layers[div_array[i]].visibility == 'hidden') {
                  document.layers[div_array[i]].visibility = 'visible';
                  document.layers[div_array[i]].display = 'block';
               }
            }
            else {
               if (document.all[div_array[i]].style.visibility == 'hidden') {
                  document.all[div_array[i]].style.visibility='visible';
                  document.all[div_array[i]].style.display='block';
                  document.all[div_array[i]].style.zIndex = 200;
               }
            }
            if (document.getElementById("btn_" + div_array[i]) != null) {
               document.getElementById("btn_" + div_array[i]).style.background="#E2F0FF";
            }
         }
         else {
            // Hide
            if (document.layers && document.layers[div_array[i]]) {
               if (document.layers[div_array[i]].visibility != 'hidden') {
                  document.layers[div_array[i]].visibility = 'hidden';
                  document.layers[div_array[i]].display = 'none';
               }
            }
            else {
               if (document.all[div_array[i]].style.visibility != 'hidden') {
                  document.all[div_array[i]].style.visibility='hidden';
                  document.all[div_array[i]].style.display='none';
               }
            }
            if (document.getElementById("btn_" + div_array[i]) != null) {
               document.getElementById("btn_" + div_array[i]).style.background="#C2CFDE";
            }
         }
      }
   }
}

function showHideDiv(div_id) {
   if (document.getElementById(div_id) == null) {
      return;
   }
   if (document.layers && document.layers[div_id]) {
      if (document.layers[div_id].visibility == 'hidden') {
         document.layers[div_id].visibility = 'visible';
         document.layers[div_id].display = 'block';
         if (document.layers[div_id + "_img"] != null) {
            document.layers[div_id + "_img"].innerHTML = '<img src="img/arrowdown.gif" style="border:none;">';
         }
      }
      else {
         document.layers[div_id].visibility = 'hidden';
         document.layers[div_id].display = 'none';
         if (document.layers[div_id + "_img"] != null) {
            document.layers[div_id + "_img"].innerHTML = '<img src="img/arrowright.gif" style="border:none;">';
         }
      }
   }
   else {
      if (document.all[div_id].style.visibility == 'hidden') {
         document.all[div_id].style.visibility='visible';
         document.all[div_id].style.display='block';
         document.all[div_id].style.zIndex = 200;
         if (document.all[div_id + "_img"] != null) {
            document.all[div_id + "_img"].innerHTML = '<img src="img/arrowdown.gif" style="border:none;">';
         }
      }
      else {
         document.all[div_id].style.visibility='hidden';
         document.all[div_id].style.display='none';
         if (document.all[div_id + "_img"] != null) {
            document.all[div_id + "_img"].innerHTML = '<img src="img/arrowright.gif" style="border:none;">';
         }
      }
   }
}

function showDiv(div_id) {
   if (document.getElementById(div_id) == null) {
      return;
   }
   if (document.layers && document.layers[div_id]) {
      if (document.layers[div_id].visibility == 'hidden') {
         document.layers[div_id].visibility = 'visible';
         document.layers[div_id].display = 'block';
         if (document.layers[div_id + "_img"] != null) {
            document.layers[div_id + "_img"].innerHTML = '<img src="img/arrowdown.gif" style="border:none;">';
         }
      }
   }
   else {
      if (document.all[div_id].style.visibility == 'hidden') {
         document.all[div_id].style.visibility='visible';
         document.all[div_id].style.display='block';
         document.all[div_id].style.zIndex = 200;
         if (document.all[div_id + "_img"] != null) {
            document.all[div_id + "_img"].innerHTML = '<img src="img/arrowdown.gif" style="border:none;">';
         }
      }
   }
}

function hideDiv(div_id) {
   if (document.getElementById(div_id) == null) {
      return;
   }
   if (document.layers && document.layers[div_id]) {
      if (document.layers[div_id].visibility == 'visible') {
         document.layers[div_id].visibility = 'hidden';
         document.layers[div_id].display = 'none';
         if (document.layers[div_id + "_img"] != null) {
            document.layers[div_id + "_img"].innerHTML = '<img src="img/arrowright.gif" style="border:none;">';
         }
      }
   }
   else {
      if (document.all[div_id].style.visibility == 'visible') {
         document.all[div_id].style.visibility='hidden';
         document.all[div_id].style.display='none';
         if (document.all[div_id + "_img"] != null) {
            document.all[div_id + "_img"].innerHTML = '<img src="img/arrowright.gif" style="border:none;">';
         }
      }
   }
}

function getRadioValue(controlName) {
   var returnVal = "";
   for (var j=0; j<document.all[controlName].length; j++) {
      if (document.all[controlName][j].checked) {
         returnVal = document.all[controlName][j].value;
         break;
      }
   }
   return returnVal;
}

/////////////////// Business Function /////////////////////////
function showContent(targetDiv, pageName) {
   var poststr = "";
   var showContent = new ajaxObject(targetDiv, pageName);
   document.getElementById(targetDiv).innerHTML=loadstatustext;
   showContent.update(poststr);
}

function submitBooking() {
   var poststr = "form_action=SUBMIT_BOOKING";
   poststr += "&title=";
   poststr += encodeURIComponent(toEntity(document.getElementById("title").value));
   poststr += "&name=";
   poststr += encodeURIComponent(toEntity(document.getElementById("name").value));
   poststr += "&tel=";
   poststr += encodeURIComponent(toEntity(document.getElementById("tel").value));
   poststr += "&title=";
   poststr += encodeURIComponent(toEntity(document.getElementById("title").value));
   poststr += "&email=";
   poststr += encodeURIComponent(toEntity(document.getElementById("email").value));
   poststr += "&tel=";
   poststr += encodeURIComponent(toEntity(document.getElementById("tel").value));
   poststr += "&addr1=";
   poststr += encodeURIComponent(toEntity(document.getElementById("addr1").value));
   poststr += "&addr2=";
   poststr += encodeURIComponent(toEntity(document.getElementById("addr2").value));
   poststr += "&addr3=";
   poststr += encodeURIComponent(toEntity(document.getElementById("addr3").value));
   poststr += "&area=";
   poststr += encodeURIComponent(toEntity(document.getElementById("area").value));
   poststr += "&licenseno=";
   poststr += encodeURIComponent(toEntity(document.getElementById("licenseno").value));
   poststr += "&brand=";
   poststr += encodeURIComponent(toEntity(document.getElementById("brand").value));
   poststr += "&model=";
   poststr += encodeURIComponent(toEntity(document.getElementById("model").value));
   poststr += "&year=";
   poststr += encodeURIComponent(toEntity(document.getElementById("year").value));
   poststr += "&cc=";
   poststr += encodeURIComponent(toEntity(document.getElementById("cc").value));
   poststr += "&date=";
   poststr += encodeURIComponent(toEntity(document.getElementById("date").value));
   poststr += "&time=";
   poststr += encodeURIComponent(toEntity(document.getElementById("time").value));
   poststr += "&service1=";
   poststr += encodeURIComponent(toEntity(document.getElementById("service1").value));
   poststr += "&service2=";
   poststr += encodeURIComponent(toEntity(document.getElementById("service2").value));
   poststr += "&service3=";
   poststr += encodeURIComponent(toEntity(document.getElementById("service3").value));
   poststr += "&details=";
   poststr += encodeURIComponent(toEntity(document.getElementById("details").value));
   poststr += "&pickup=";
   poststr += encodeURIComponent(toEntity(document.getElementById("pickup").value));
   var submitBooking = new ajaxObject("div_booking_content", "booking_content.php");
   document.getElementById("div_booking_content").innerHTML=loadstatustext;
   submitBooking.update(poststr);
}

