var apiURL="/api.php";
function addFavorite(jobid,stitle){
	$.get(apiURL, { jobid: jobid,title:stitle, action: "addfavorite" },
  			 function(data){
				 	jAlert(data,"Add To Favorite");
				//alert(data);
		   });
}

function sendCodeEmail(){
	
	jPrompt('Enter Email Address:', '', 'Resend Activation Link', function(r) {
    	if(r){resendLink(r);}
	
	});
	
}

function getPhone(skey){
	$.get(apiURL, { skey: skey,action: "getphone" },
  			 function(data){
				 document.getElementById("myphonespan").innerHTML=data;
		   });
}

function removeJob(jobid){
		
	jConfirm("Are you sure you want to remove this job posting?", 'My Jobs', function(r) {
    		if(r){
				$.get(apiURL, {  jobid: jobid, action: "removejob"  },
				 function(data){
					window.location=window.location;
			   });
			}
	});

	
}

function removeFavorite(favid){
	
	
	jConfirm("Are you sure you want to remove this job from your favorite list?", 'Favorites', function(r) {
    		if(r){
				$.get(apiURL, { favid: favid, action: "removeFavorite" },
				 function(data){
					window.location=window.location;
			   });
			}
	});

	
}


function removeAlerts(aid){
	
	
	jConfirm("Are you sure you want to remove this from your alerts list?", 'Job Alerts', function(r) {
    		if(r){
				$.get(apiURL, { aid: aid, action: "removeAlerts" },
				 function(data){
					window.location=window.location;
			   });
			}
	});

	
}



function submitRes(fobj){
	if(validateForm(fobj)){
			document.getElementById("resumebutton").value="Please Wait...";
			return true;
	}
	
	return false;
}

function submitResume(jobid,skey){

	if(validateForm(document.getElementById("jobform"))){
			var fname=document.getElementById("cfname").value;
			var email=document.getElementById("cemail").value;
			var resume=document.getElementById("cresume").value;
				var joburl=document.getElementById("joburl").value;
			var jobtitle=document.getElementById("jobtitle").value;
			var ishuman=document.getElementById("chuman").value;
			document.getElementById("resumebutton").value="Please Wait...";
			$.get(apiURL, { jobid: jobid,fullname: fname,femail: email,fresume: resume,human: ishuman,skey:skey,joburl: joburl, jobtitle:jobtitle, action: "submitResume" },
  			 function(data){
				//alert(data);
				jAlert(data,"Info");
				document.getElementById("resumebutton").value="SUBMIT";
				closeTab();
		   });
	}
}


function validateAlert(sform){
		if(sform.req_search.value=="" && sform.jobcat.value=="" && sform.jobloc.value=="" ){
			jAlert("Please select at least a criteria to add job alerts","Job Alerts");
			return false;
		}else{
			return true;	
		}
}


function sendEmail(){
	
	if(validateForm(document.getElementById("contactForm"))){
			var fname=document.getElementById("sfname").value;
			var email=document.getElementById("semail").value;
			var msgbody=document.getElementById("msgbody").value;
			var ishuman=document.getElementById("shuman").value;
			$.get(apiURL, {fullname: fname,femail: email,msgtext: msgbody,human: ishuman, action: "sendFriendEmail" },
  			 function(data){
					jAlert(data,"Send to friend");
				document.getElementById("sfname").value="";
				document.getElementById("semail").value="";
				document.getElementById("shuman").value="";
				closeTab();
		   });
	}
}

function reportJob(jid){
	
	jConfirm('Report this job as Spam, Scam or Expired?', 'Report', function(r) {
    		if(r){
				$.get(apiURL, { jobid: jid, action: "reportJob", nc: Math.random() },
				 function(data){
					jAlert(data,"Report");
			   });
			}
	});

	
	
}

function showTab(scontent){
	if($('#myBox').css("display")!="none"){
		//toggle first
		 $('#myBox').toggle('fast');
	}
	$('#myBox').html(document.getElementById(scontent).innerHTML);

  $('#myBox').toggle('fast', function() {
    // Animation complete.
		window.scroll(0,getHeight()+400);
  });




}

function closeTab(){
  $('#myBox').toggle('fast', function() {
    // Animation complete.
		window.scroll(0,getHeight()+400);
  });


}


function getHeight() {
  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 myHeight;
 // window.alert( 'Width = ' + myWidth );
 // window.alert( 'Height = ' + myHeight );
}

function goTo(url){
	document.location=url;
}
function checkText(txtObj,swt){
		if(swt==1){
			if(txtObj.value=="search jobs"){txtObj.value="";}
		}else{
				if(txtObj.value==""){txtObj.value="search jobs";}
		}
}

function validateForm(fObj){
	for(var i=0;i<fObj.elements.length;i++){
		var curObj=fObj.elements[i];
		//required
		//alert(curObj.type);
		if(curObj.type!="hidden" && curObj.type!="submit" && curObj.type!="button"){ 
		if(curObj.name.indexOf("req_")>-1){
			if(curObj.value==""){
				curObj.focus();
				return false;
			}
		}}
		
	}
	return true;
}


function changePassword(fobj,skey){
	var npass=fobj.password;
	var npassc=fobj.password_confirm;
	if(npass.value!="" & (npass.value==npassc.value)){
		
		$.get(apiURL, { username: skey,password:npass.value,passwordc:npassc.value, action: "changePassword", nc: Math.random() },
   		function(data){
			//alert(data);
			if(data.indexOf("success")>-1){
					jAlert("Your password was change.","Success",function(r){window.location='/'}); 
	
			}else{
				jAlert("Your password did not match! Please try again.","Reset Password"); 
				
			}
  		 });	
		
	}else{
		jAlert("Password does not match!","Reset Password");	
	}
	
	return false;
}
function resetPassword(email){
	if(email!=""){
		
		$.get(apiURL, { username: email, action: "resetpassword", nc: Math.random() },
   function(data){
		jAlert("Please check your email for the link to reset your password","Reset Password"); 
	
   });	
	}
	
}
function resendLink(email){
	
	if(email!=""){
		
		$.get(apiURL, { username: email, action: "resendlink", nc: Math.random() },
   function(data){
		jAlert("Please check your email for the link to activate your account. Please add no-reply@fullofjobs.com into your whitelist to ensure the email deliver to your inbox","Account Activation"); 
	
   });	
	}
	
}

function login(bObj){
//loadText("loginstatus");

var username=document.getElementById("username");
var pass=document.getElementById("password");

if(username.value==""){username.focus();return;}
if(pass.value==""){pass.focus(); return;}
bObj.value="Please Wait"
	$.get(apiURL, { username: username.value, action: "login",password: pass.value, nc: Math.random() },
   function(data){

	 if(data.indexOf("success")>-1){

		window.document.location=window.document.location;
	
	
	 }else{
		 	bObj.value="Login";
			pass.value="";
			if(data.indexOf("Incorrect")>-1){
				data="<strong style='color:red;'>"+data+"</strong><br><br />&raquo;forgot your password?: <a href=\"javascript:resetPassword('"+username.value+"');\">Reset Password</a><br />&raquo;didn't get activation link?: <a href=\"javascript:resendLink('"+username.value+"');\">Resend activation link</a>";	
			}
			
			jAlert(data,"Login"); 
	 }
   });

	
}

function getCities(stateStr,dispName){
	loadText("citiesresults");
	$.get(apiURL, { statename: stateStr, action: "getcities",displayname: dispName, nc: Math.random() },
   function(data){
     loadHTML("citiesresults",data);
   });
	window.scrollTo(0,800);
}

function loadHTML(divName,strdata){
		document.getElementById(divName).innerHTML=strdata;
}

function loadText(divName){

		document.getElementById(divName).innerHTML="<img src='images/loader.gif'><br>Loading...";
	
}

        function toggle_username(userid) { 
                if (window.XMLHttpRequest) { 
                    http = new XMLHttpRequest(); 
                } else if (window.ActiveXObject) { 
                    http = new ActiveXObject("Microsoft.XMLHTTP"); 
                } 
                handle = document.getElementById(userid); 
                var url = 'ajax.php?'; 
                if(handle.value.length > 0) { 
                    var fullurl = url + 'do=check_username_exists&username=' + encodeURIComponent(handle.value);
                    http.open("GET", fullurl, true); 
                    http.send(null); 
                    http.onreadystatechange = statechange_username; 
                }else{ 
                    document.getElementById('username').className = ''; 
                } 
            } 
            
            function statechange_username() { 
                if (http.readyState == 4) { 
                    var xmlObj = http.responseXML; 
                    var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data; 
                    document.getElementById('username').className = html; 
                } 
            } 
