$(document).ready(dialogForms);

function dialogForms() {
  $('a.dialog-form').click(function() {
    var a = $(this);
    $.get(a.attr('href'),function(resp){
      var dialog = $('<div>').attr('id','formDialog').html($(resp).find('form:first').parent('div').html());
      $('body').append(dialog);
      dialog.find(':submit').hide();
      dialog.dialog({
        title: a.attr('title') ? a.attr('title') : '',
        modal: true,resizable:false,
        buttons: {
          'Subscribe': function() {submitFormWithAjax($(this).find('form'));$(this).dialog('close');}
        },
        close: function() {$(this).remove();},
        width: '400px'
      });
    }, 'html');
    return false;
  });
 
}

function submitFormWithAjax(form) {
 var urlParams = document.getElementById("urlparam").value;
 var response = $.ajax({
    url: form.attr('action') + urlParams,
    async: false,
    data: form.serialize(),
    type: (form.attr('method')),
    dataType: 'script'
  });
  jQuery('#dialog').dialog('open');
  //alert('Successfully Subscribed');
  return true;
}

/*//Hover Function
function isMouseOver(id){
	
	//alert(document.getElementById('enhancedAdDescription'+id).className);
	
	if(document.getElementById('jobContainer'+id) != null) {
		//alert("jobContainer::"+document.getElementById('jobContainer'+id).className);
		document.getElementById('jobContainer'+id).className ='jobContainerMouseOver';
	}
	if(document.getElementById('enhancedAdDescription'+id) != null) {
		//alert("enhancedAdDescription::"+document.getElementById('enhancedAdDescription'+id).className);
		document.getElementById('enhancedAdDescription'+id).className ='enhancedAdDescriptionMouseOver';
	}

	if(document.getElementById('enhancedAd'+id) != null) {
		//alert("enhancedAd::"+document.getElementById('enhancedAd'+id).className);
		document.getElementById('enhancedAd'+id).className ='enhancedAdMouseOver';
	}

	if(document.getElementById('enhancedAdWithLogo'+id) != null) {
		//alert("enhancedAdWithLogo::"+document.getElementById('enhancedAdWithLogo'+id).className);
		document.getElementById('enhancedAdWithLogo'+id).className ='enhancedAdWithLogoMouseOver';
	}

	if(document.getElementById('normal'+id) != null) {
		//alert("normal::"+document.getElementById('normal'+id).className);
		document.getElementById('normal'+id).className ='descriptionMouseOver';
	}


	//alert("5");
}

//Hover Function
function isMouseOut(id){
	if(document.getElementById('jobContainer'+id) != null) {
		document.getElementById('jobContainer'+id).className ='jobContainer';
	}
	if(document.getElementById('enhancedAd'+id) != null) {
			document.getElementById('enhancedAd'+id).className ='enhancedAd';
	}
	if(document.getElementById('enhancedAdWithLogo'+id) != null) {
			document.getElementById('enhancedAdWithLogo'+id).className ='enhancedAdWithLogo';
	}
	if(document.getElementById('normal'+id) != null) {
			document.getElementById('normal'+id).className ='description';
	}
	if(document.getElementById('enhancedAdDescription'+id) != null) {
			document.getElementById('enhancedAdDescription'+id).className ='enhancedAdDescription';
	}
	

}*/

//Pop up RSS FEED list page
function popUp(urlToOpen){

	var winParms = "toolbar=0,Status=1,resizable=yes,scrollbars=yes,height=900,width=1300,left=0,top=0";
	var newwindow = window.open(urlToOpen,"_blank",winParms);
	newwindow.focus();
}

//Set RSS Feed Link
function setLink(){
	var rsslink = document.getElementById("rss").value;
	document.getElementById("RssLink").value = rsslink;
	var rsslinkEdited = rsslink + '&mock=1'; 
	var newParam = "javascript:popUp(\"" + rsslinkEdited + "\");";
	document.getElementById("linkToFeed").innerHTML = "<center><a href = " + newParam  + ">View Feed<a><center>";
	jQuery("#rssFeed").dialog("open");
}


//Pop up dialog for RSS feed and alert submit success status
jQuery(document).ready(function() {
	
	jQuery("#dialog").dialog({
        dialogClass:"transparent",bgiframe: true, autoOpen: false, height: 180,width : 500, modal: true,resizable:false,
        buttons: {
		       "OK..": function() {
				  	$( this ).dialog( "close" );
				  }
			}

	});

	jQuery("#rssFeed").dialog({
			bgiframe: true, autoOpen: false, height: 210,width : 660, modal: true,resizable:false,
		});

});



//Ajax functions for Search Jobs, List all Jobs and Pagination
function handleHttpResponse() {
        if (http.readyState == 4) {
			  if(http.status==200) {
                  //alert(http.responseText);
                  var results=http.responseText;
              	  document.getElementById('refreshJobList').innerHTML = results;
              	  }
              }else
        	{
				document.getElementById('refreshJobList').innerHTML = '<img src="/JobSearchForm/images/loading.gif" width="60" height="60" style = "padding-top:215px;padding-left:280px;" border="0" alt="Loading, please wait..." />';
        	}
        }

function getSearchResultAjax() {
        var indId = "";
        if( selectedndustry != null )
  	indId = selectedndustry;

	var locId = "";
   	if( selectedLocation != null )
   	locId = selectedLocation;

   	var workTypeId = "";
  	if( selectedWorkType != null )
        workTypeId = selectedWorkType;

  	  	
  	var keywordText = encodeURI(keywordField.getValue());

        var sliderObj = Ext.getCmp("salary_slider_id");
  	var salMin1   = sliderObj.getValue(0);
  	var salMax1   = sliderObj.getValue(1);
  	if( perHourOrPerYear == 'AnnualPackage' )
  	{
  		salMin1 *= 1000;
	   	salMax1 *= 1000;
      	}

  	var url = "/JobSearchForm/displayAllJobsTempl.php?" +
  	"indId=" + indId +
        "&locId=" + locId +
        "&wtId=" + workTypeId +
        "&keyword=" + keywordText +
        "&salaryType=" + perHourOrPerYear +
        "&salaryMin=" + salMin1 +
       "&salaryMax=" + salMax1;
      //show_progressbar("refreshJobList");
      
      http.open("GET", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.send(null);
}     

function getSearchResultClassifAjax(url) {
      http.open("GET", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.send(null);
}        

function getAllJobsAjax(){
      var url = "/JobSearchForm/displayAllJobsTempl.php?" +
  	  	"indId=" +
        	"&locId=" +
        	"&wtId=" +
        	"&keyword=" +
        	"&salaryType=" +
        	"&salaryMin=" +
        	"&salaryMax=";

      http.open("GET", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.send(null);
}

function getPagination(url){
      urlArray = url.split("?");
      //alert(urlArray[0]);
      // alert(urlArray[1]);
      var url = "/JobSearchForm/displayAllJobsTempl.php?" + urlArray[1];
      http.open("GET", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.send(null);
}

function getHTTPObject() {
      var xmlhttp;
      
      if(window.XMLHttpRequest){
      		xmlhttp = new XMLHttpRequest();
	}else if (window.ActiveXObject){
		    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	if (!xmlhttp){
		        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}

      }
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object

