$(document).ready(function(){
    
        $('.phonenumberinput').focus(function(e) {
        if($(this).hasClass('phonecss')) { // not remove if is currently removed
                $(this).removeClass('phonecss'); // change
        } }); 
    
        $('.phonenumberinput').blur(function(e) {
        if($(this).val() == '') { 
                $(this).addClass('phonecss'); 
        } });
     

	if($('#featured_companies').length > 0){
		$.getJSON("/services/getFeaturedCompanies",
		        function(data){
		          $.each(data, function(Id,company){
		            $("#featured_companies").append('<li><a href="/company/profile/'+company.CompanyId+'/'+company.CompanyName+'"><img border="0" src="/img/logos/'+company.CompanyId+'.png"/></a></li>');
		          });
		        });
	}

	$('#PostalCode').click(function(){

		if(!IsNumeric($('#PostalCode').val())){
			$('#PostalCode').val('');
		}
	})

	$('#PostalCode').blur(function(){

		if(!IsNumeric($('#PostalCode').val()) || $('#PostalCode').val()==''){
			$('#PostalCode').val('Postal Code');
		}
	});

	//$('.jobresult:odd').addClass('jobresult alt');
	$('.listtable tr:odd').addClass('alt');

	$('a').tooltip({
        showURL: false
    });

    $( "input:submit, input:button").not('.regular-button').button();


    $('.datep').bind('keydown',function(e){
        if(e.keyCode == 8 || e.keyCode == 46) {
            $(this).val('');
            return false;
        }
    });


    helpDlg = {
        	    	autoOpen: false,
        	    	height: 400,
        	    	width: 600,
        	    	modal: true,
                        title: 'About this section',
                        resizable: false,
        	    	draggable:false,
                        buttons: {
        	    		Close: function() {
                                	$(this).dialog('close');
        	    		}
        	    	}
        	    }

   $('#helpdlg').dialog(helpDlg);

   if($('#expiredJobAlert').length > 0){
       ExpiredJobDlg = {
                            autoOpen: false,
                            modal: true,
                            title: 'Inactive Job',
                            resizable: false,
                            draggable:false,
                            buttons: {
                                    Close: function() {
                                            $(this).dialog('close');
                                    }
                            }
                        }

       $('#expiredJobAlert').dialog(ExpiredJobDlg);
       $("#expiredJobAlert").dialog("open");
   }

   if(isdefined('bShowTabNavigation') && bShowTabNavigation == true){
       /*$.slidenav({
        animspeed: 'medium',
        hideonout: 'true' 
        });*/
   }

   $('#countryChanger').click(function(){

      countryDlg = {
        	    	autoOpen: false,
        	    	height: 400,
        	    	width: 700,
        	    	modal: true,
                        title: 'Select a Country',
                        resizable: false,
        	    	draggable:false,
                        buttons: {
        	    		Close: function() {
                                	$(this).dialog('close');
        	    		}
        	    	}
        	    }

      $('#countrydlg').dialog(countryDlg);

      $.ajax({
        url: '/services/getCountries',
        type: 'GET',

            success: function(data){
                 $('#countrydlg').html(data);
                 $('#countrydlg').dialog("open");
            }
        });

   });
   
   $("#browsebymenu").mouseenter(function() {
        $('#browsejobbysulid').css('left', 'auto');
    });
    
    $("#browsebymenu").mouseleave(function() {
        $('#browsejobbysulid').css('left', '-999em');
    });
    
    $("#browsejobbysulid").mouseenter(function() {
        $(this).css('left', 'auto');
    });
    
    $("#browsejobbysulid").mouseleave(function() {
        $(this).css('left', '-999em');
    });

});


    function setCountry(strCountryCode){
        $('#hiddenCountry').val(strCountryCode);
        $('#selectedCountry').html('Country: ' + strCountryCode);
        $('#countrydlg').dialog("close");
    }

    function getCities(){

        $.ajax({
        url: '/services/getCities',
        type: 'GET',
        data: 'state='+$('#StateCombo').val(),
            success: function(data){
                 $('#CityCombo').html(data);
                 $('#ZipCombo').html('<option value="">[ Select a Zip code ]</option>');
            }
        });

    }

    function getCityZips(){

        $.ajax({
        url: '/services/getCityZips',
        type: 'GET',
        data: 'state='+$('#StateCombo').val()+'&city='+$('#CityCombo').val(),
            success: function(data){
                 $('#ZipCombo').html(data);
            }
        });

    }

function uploadLogoFileDialog() {

             $("#uploadlogodialog").dialog({
              autoOpen: false,
              width: 300,
              modal: true
            });

           $("#uploadlogodialog").dialog({
              buttons : {
                "Upload" : function() {
                    $('#uploadLogoFileOverlayForm').submit();
                },
                "Cancel" : function() {
                    $("#uploadlogodialog").dialog("destroy");
                }
              }
            });

             $("#uploadlogodialog").dialog("open");
             return false;
}


function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

}

function rawurlencode (str) {
    str = (str + '').toString();
    str = str.replace(/\//g,'%2F');
    if(str.indexOf('%') != -1) {
          return encodeURIComponent(encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
          replace(/\)/g, '%29').replace(/\*/g, '%2A'));
    } 
       return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
          replace(/\)/g, '%29').replace(/\*/g, '%2A');
    
}

function cleanBlanks(){
	if($('.jobSearch #searchkw').val() == 'Type keywords (e.g. Pilot, Technician)'){
		$('.jobSearch #searchkw').val('');
	}

	if($('.jobSearch #location').val() == 'Type Location (City, State or Zip Code)'){
		$('.jobSearch #location').val('');
	}

        strAction = '/search/';

        if($('#hiddenCountry').val() != ''){
            strAction+=$('#hiddenCountry').val() + '/';
        }

        if($('#searchkw').val() != ''){
            strAction+= 'q-'+ rawurlencode($('#searchkw').val()) + '/';
        }

        if($('#location').val() != ''){
            strAction+= 'l-' + rawurlencode($('#location').val()) + '/';
        }

        if($('#RadiusDistance').val() != ''){
            strAction+= '?RadiusDistance=' + $('#RadiusDistance').val();
        }

        $('#searchform').attr('action',strAction);
}

function ajaxform(selector) {
    var action =  $(selector).attr('action');
    var frmData = $(selector).serialize();
    $.ajax({
        url: action,
        type: 'POST',
        data: frmData,
        success: function(){
             alert("saved");
        }
    });

    return false;
}
function showContactMethod(){
    var txtType= $('#PositionOpeningApplyType').val();
    if ('email'==txtType){
        $('#url').hide();
        $('#email').show();
    } else if ('url'==txtType){
        $('#url').show();
        $('#email').hide();
    } else {
        $('#url').hide();
        $('#email').hide();
    }
}

function disableEnterKey(e) {
     var key;
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox
     return (key != 13);
}

function insertErrors(data, divId) {
  if(data.length > 0) {
                 errorhtml = '<div class="fielderror"><ul>';
                 $.each(data, function(i,item) {
                    errorhtml += '<li>' + item + '</li>';
                 });
                 errorhtml += '</ul>';
                 $('#'+divId).html(errorhtml);
  }
}
function cleanErrors(divId) {
        $('#'+divId).html('');
}

$(function() {
	$("#searchkw").autocomplete({
		source: "/search/jobSuggest",
		minLength: 2
	});

    $("#location").autocomplete({
		source:
            function( request,  respond) {
             $.post( "/search/locationSuggest", {term: request.term},
                function( response ) {
                respond( response );
            }, 'json' );
        },
		minLength: 3,
        delay: 100
	});
});

function helpDialog(id){
    if(!id){
        var id = '';
    }

    if($('#helpdlg').text() == ''){
        $.ajax({
            url: '/services/aboutSection',
            type: 'POST',
            success: function(html){
                 $('#helpdlg').html(html);
                 $('#helpdlg').dialog('open');
                 if(id != ''){
                    document.location.href = id;
                 }
            }
        });
    } else {
        $('#helpdlg').dialog('open');
        if(id != ''){
            document.location.href = id;
        }
    }
}

function stripHTML(oldString) {

   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {

        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              if(oldString.charAt(i+1)=="<")
              {
              		//dont do anything
	}
	else
	{
		inTag = false;
		i++;
	}
        }

        if(!inTag) newString += oldString.charAt(i);

   }

   return newString;
}


function confirmDialog(strTitle, strhtmlMessage, action, actionCancel) {
    var element = document.createElement('div');
    $(element).html(strhtmlMessage);
    $(element).dialog({
              autoOpen: true,
              width: 400,
              modal: true,
              title: strTitle,
               buttons : {
                "Ok" : function() {
                  $(element).dialog('destroy');
                  action();
                }
                ,
                 "Cancel" : function() {
                  $(element).dialog('destroy');
                  if(actionCancel != undefined) {
                       actionCancel();
                  }
                }
              }
  });
}

/* Checks if variable is defined */
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function imageAdRedirect(id){

    $.ajax({
            url: '/company/saveImageAdClick',
            type: 'POST',
            data: 'id=' + id,
            success: function(res){

                    document.location.href = '/company/profile/' + id;
                    
                 }
            
        });

}
/**
 * Override javascript confirm() and wrap it into a jQuery-UI Dialog box
 *
 * @depends $.getOrCreateDialog
 *
 * @param { String } the alert message
 * @param { String/Object } the confirm callback
 * @param { Object } jQuery Dialog box options
 */
//function confirmDialog(strTitle, strMessage, callback) {
//    //alert("entro aki");
//	var element = document.createElement('div');
//    $(element).html(strhtmlMessage);
//
//
//    $(element).dialog({
//              autoOpen: false,
//              width: 400,
//              modal: true,
//              title: strTitle,
//               buttons : {
//                "Ok" : function() {
//                  $(element).dialog('destroy');
//                  //return (typeof callback == 'string') ?
//				  //window.location.href = callback :
//				  //callback();
//                }
//                ,
//                 "Cancel" : function() {
//                  $(element).dialog('destroy');
//                }
//              }
//  });
//
//  $(element).dialog('open');
//
//}
