//**********************************************************************************//
Functions.changeProductNormalImage = function(imageName, mediaID){
  imgSrc   = "media/product/normal/" + imageName;
  linkTemp = "index.php?_room=media&_spAction=zoomImage&media_id=" + mediaID;
  linkSrc  = "javascript:UtilWindow.openWindow(linkTemp,'1010','800')";
  document.getElementById("productImage").src = imgSrc;
  document.getElementById("largeImage").href  = linkSrc;
}

//**********************************************************************************//
Functions.changeCategoryNormalImage = function(imageName, mediaID){
  imgSrc   = "media/category/normal/" + imageName;
  linkTemp = "index.php?_room=media&_spAction=zoomImage&media_id=" + mediaID;
  linkSrc  = "javascript:UtilWindow.openWindow(linkTemp,'1010','800')";
  document.getElementById("categoryImage").src = imgSrc;
  document.getElementById("largeImage").href  = linkSrc;
}

//**********************************************************************************//
Functions.popUp = function(windowURL,windowName,windowWidth,windowHeight) {

   window.name = 'parentWnd';
   newWindow = window.open(windowURL,windowName,'width='+windowWidth+',height='+windowHeight+',toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1');
}

//=====================================================//
Functions.addWishList = function(product_id, record_type){

   var divObj = document.getElementById("wishListID");

   var url = "index.php?_spAction=wishListStatus" + "&product_id=" + product_id
             + "&record_type=" + record_type + "&showHTML=0" ;

   var formContent = 'xyz=1';

   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.addWishListHandler(product_id);});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.addWishListHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      var divObj         = document.getElementById("wishListID");
      divObj.innerHTML = responseText;
   }
}

//=====================================================//
Functions.askForQuote = function(record_type, mail_type){


   var url = "index.php?_spAction=askForQuote" + "&record_type=" + record_type
             + "&mail_type=" + mail_type   + "&showHTML=0" ;

   var formContent = 'xyz=1';

   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.askForQuoteHandler(mail_type);});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.askForQuoteHandler = function(mail_type){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      if (mail_type == "quote"){
         var divObj         = document.getElementById("askForQuoteID");
      }
      else{
         var divObj         = document.getElementById("sendMailID");
      }
      divObj.innerHTML = responseText;
   }
}
//=====================================================//
Functions.removeWishList = function(wish_list_id, record_type){


   var url = "index.php?_spAction=removeWishList" + "&wish_list_id=" + wish_list_id
              + "&record_type=" + record_type  + "&showHTML=0" ;

   var formContent = 'xyz=1';


   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.removeWishListHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.removeWishListHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      var divObj         = document.getElementById("wishListTable");
      divObj.innerHTML = responseText;
   }
}
//=====================================================//
Functions.addProductsQuantity = function(product_id, qtyObj, record_type){
   var divObj = document.getElementById("thumbListBoxMiddleRight");
   var qty = qtyObj.value;
   //var imgSrc = '<div align="center"><img src="images/loading.gif" border="0" align="center"></div>';
   //divObj.innerHTML = imgSrc;

   var url = "index.php?_spAction=addProductsQuantityUpdate" + "&quantity=" + qty
              + "&record_type=" + record_type   + "&product_id=" + product_id  + "&showHTML=0" ;

   var formContent = 'xyz=1';


   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.AddProductsQuantityHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.AddProductsQuantityHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      var divObj         = document.getElementById("wishListTable");
      divObj.innerHTML   = responseText;
   }
}

//=====================================================//
Functions.updateCategoryComplete = function(sub_category_id, checkBoxObj){
   var checkedState = checkBoxObj.checked;
   //alert (checkedState);
   //return;
   var divObj         = document.getElementById("completeCheckBox");
   var imgSrc = '<div align="center"><img src="images/loading.gif" border="0" align="center"></div>';
   divObj.innerHTML = imgSrc;

   var url = "index.php?_spAction=updateCategoryComplete" + "&checked=" + checkedState
              + "&sub_category_id=" + sub_category_id  + "&showHTML=0" ;

   var formContent = 'xyz=1';


   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.UpdateCategoryCompleteHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.UpdateCategoryCompleteHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      var divObj         = document.getElementById("completeCheckBox");
      divObj.innerHTML   = responseText;
   }
}

//=====================================================//
Functions.shortTermProducts = function(sub_category_id){

   var divObj = document.getElementById("thumbListBoxMiddleRight");
   var imgSrc = '<div align="center"><img src="images/loading.gif" border="0" align="center"></div>';
   divObj.innerHTML = imgSrc;

   var url = "index.php?_spAction=shortTermProducts" + "&sub_category_id=" + sub_category_id + "&showHTML=0" ;

   var formContent = 'xyz=1';


   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.shortTermProductsHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.shortTermProductsHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      var divObj         = document.getElementById("thumbListBoxMiddleRight");
      divObj.innerHTML = responseText;
   }
}
//=====================================================//
Functions.shortTermProductsQuantity = function(short_term_rental_id, qtyObj, sub_category_id){

   var divObj = document.getElementById("thumbListBoxMiddleRight");
   var qty = qtyObj.value;
   var imgSrc = '<div align="center"><img src="images/loading.gif" border="0" align="center"></div>';
   divObj.innerHTML = imgSrc;

   var url = "index.php?_spAction=shortTermQuantityUpdate" + "&quantity=" + qty  + "&short_term_rental_id=" + short_term_rental_id + "&sub_category_id=" + sub_category_id + "&showHTML=0" ;

   var formContent = 'xyz=1';


   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.shortTermProductsQuantityHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//=====================================================//
Functions.shortTermProductsQuantityHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText   = XMLHTTP.xmlHttpObj.responseText;
      var divObj         = document.getElementById("thumbListBoxMiddleRight");
      divObj.innerHTML   = responseText;
   }
}


//=====================================================//
Functions.login = function(){

   var url = "index.php?_spAction=loginSubmit&_room=member&showHTML=0";

   var frmObj     = document.forms['frmLogin'];
   var formContent = UtilAjax.getFormValues(frmObj);

   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {Functions.loginHandler();});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);

}

//=====================================================//
Functions.loginHandler = function(){

   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") {

      var responseText = XMLHTTP.xmlHttpObj.responseText;
      responseText = responseText.replace(/^\s+/, '');
      var xmlDoc  = XMLHTTP.loadXML(UtilString.trimAll(responseText, 1));
      var root    = xmlDoc.documentElement;
      var headerTag  = xmlDoc.getElementsByTagName("header");
      var errorCount = headerTag[0].childNodes[0].firstChild.nodeValue;
      var site2      = headerTag[0].childNodes[1].firstChild.nodeValue;
      var returnUrl  = headerTag[0].childNodes[2].firstChild.nodeValue;

      if(errorCount == 0){
         if (site2 != 'NA'){
            var url = site2 + 'index.php?_spAction=loginSubmitAuto&_room=member&showHTML=0';

            var frmObj     = document.forms['frmLogin'];
            var utilFrame = document.getElementById("utilFrame");
            frmObj.returnUrl.value = returnUrl;
            
            frmObj.action = url;
            frmObj.target = "utilFrame";
            frmObj.submit();
            
            $.blockUI({ message: '<h3 style="text-align:center">Please wait...</h3>' });

            setTimeout(function(){
               /*
               var iframeResponse = $(utilFrame.contentWindow.document.body).html();
               if (iframeResponse == "success"){
               }
               */
               document.location = returnUrl;

            },2000);

         } else {
            document.location = returnUrl;
         }

      } else {
         alert('please enter a valid username and password');
      }

   }
}

$(function(){
   $('a.menuTitle').click(function(e){
      var link     = $(this);
      var parent   = $(link).closest('.menu');
      var selObjId = $(parent).attr('id');
      var commentsBox = $('.subMenu', parent);
      $(commentsBox).slideToggle('slow');
      $(link).toggleClass('ic-arr-down');

      $('.menu').each(function(){
         var menuID  = $(this).attr('id');
         
         if (menuID != selObjId){
            var subMenu = $('.subMenu', $(this));
            if ($(subMenu).css('display') == 'block'){
               $('.menuTitle', $(this)).removeClass('ic-arr-down').addClass('ic-arr-right');
               $(subMenu).hide();
            }
         }
      });
   });

    $('a[href^="http://"]')
    .not('a[href^="http://indigo-living.com/"]')
    .not('a[href^="http://www.indigo-living.com/"]')
    .not('a[href^="http://indigo-living.com.hk/"]')
    .not('a[href^="http://www.indigo-living.com.hk/"]')
    .not('a[href^="http://www2.indigo-living.com/"]')
    .not('a[href^="http://rental.indigo-living.com/"]')
    .not('a[href^="http://design.indigo-living.com/"]')
    .not('a[href^="http://indigoshopping.testpilotweb.com/"]')
    .not('a[href^="http://indigodesigns.testpilotweb.com/"]')
    .not('a[href^="http://indigorental.testpilotweb.com/"]')
    .attr('target', '_blank');

});
