$(document).ready(function() {
        loadingMask(false);
});



function loadingMask(isShow)
{
    if(isShow)
     {
            $('#loadingMask').css("width", $("#rootBody").width());
            $('#loadingMask').css("height", getWindowHeight());
            $('#loadingMask').css("top", window.pageYOffset);
            $('#loadingMask').css("left", window.pageXOffset);
            $('#loadingMask').show();
        }
        else
        {
            $('#loadingMask').hide();
        }
    }

 /**
    compute the current viewable window height
    **/
    function getWindowHeight()
    {
        var winHeight, d=document;
        if (typeof window.innerWidth!='undefined') {
         winHeight = window.innerHeight;
        } else {
         if (d.documentElement &&
          typeof d.documentElement.clientWidth!='undefined' &&
           d.documentElement.clientWidth!==0) {
          winHeight = d.documentElement.clientHeight;
         } else {
          if (d.body &&
           typeof d.body.clientWidth!='undefined') {
           winHeight = d.body.clientHeight;
          }
         }
        }
        return winHeight;
    }



$(function() {

    $('#searchForm').click(function() {
            loadingMask(true);
    });

   $('#category_id').change(function()
    {
            //loadingMask(true);
                val = this.value;
                name = this.name;

               	if (name == "category_id" && val != "") { // i.e. category changed
                    urlLoc = "http://"+location.host+"/processor.php?act=1&key="+val;
                    jQuery.ajax(
                    {
                        url: urlLoc,
                        type: "GET",
                        dataType: "json",
                        success: populateSubcats,
                        error:failure
                    });
                }
        //loadingMask(false);
   });


   $('#catid').change(function()
    {
                val = this.value;
                name = this.name;

           	if (name == "catid" && val != "") { // i.e. category changed
                  urlLoc = "http://"+location.host+"/processor.php?act=1&key="+val;
                  jQuery.ajax(
                   {

                        url: urlLoc,
                        type: "GET",
                        dataType: "json",
                        success: populatePostAdSubcats,
                        error:failure
                    });
                }
   });

});


function populateSubcats(response)
{
  	    // get the json data
		var subCats = eval(response);
        var subcatSelect = document.getElementById("subcat_id")
         // remove all items except first from the list
         while (subcatSelect.options.length > 1) {
           subcatSelect.remove(1);
         }

	  // add the new elements to the list
     for (var n=0;n < subCats.length; n++) {
       	addToDroplist(subcatSelect, subCats[n].desc,subCats[n].key);
     }
}


function populatePostAdSubcats(response)
{
  	    // get the json data
		var subCats = eval(response);
        var subcatSelect = document.getElementById("subcatid")
         // remove all items except first from the list
         while (subcatSelect.options.length > 1) {
           subcatSelect.remove(1);
         }

	  // add the new elements to the list
     for (var n=0;n < subCats.length; n++) {
       	addToDroplist(subcatSelect, subCats[n].desc,subCats[n].key);
     }
}

function failure (request) {
    alert ("failure: "+request);
    console.log(request);
    loadingMask(false);
}


function addToDroplist(droplist, desc, code )
{
	try
	{
		droplist.add(new Option(desc,code), null);
	}
	catch (err)
	{
		droplist.add(new Option(desc,code));
	}
}


