function Get_Cookie( name ) {

		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )
		{
		return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
function Set_Cookie( name, value, expires, path, domain, secure ){

var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function urlencode (str) {
 	str = (str+'').toString();
	 return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
function gup(name)
{
  var namex = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+namex+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
  var submitLogin = function()
{

        $.post("/profiles/index.php/CheckLogin",{ user_name:$('#email').val(),password:$('#pass').val(),rand:Math.random() } ,function(data)
        {data=$.parseJSON(data);
			
          if(data.authentication=='yes') //if correct login detail
          {
                $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                {
                  //add message and change the class of the box and start fading
                  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                  function()
                  {

					 Set_Cookie( 'u_name', data.user_name, 0, '/', '', '' );
					 Set_Cookie( 'u_panname', data.user_panname, 0, '/', '', '' );
					 Set_Cookie( 'u_comname', data.user_comname, 0, '/', '', '' );
					 Set_Cookie( 'u_type', data.user_type, 0, '/', '', '' );
					 Set_Cookie( 'u_jtype', data.user_jtype, 0, '/', '', '' );
					 Set_Cookie( 'u_id', data.user_id, 0, '/', '', '' );
  					
                     if(data.user_jtype=='JEmployer'){
                     document.location='/employers.html';}
					 else if(data.user_jtype=='JCandidate'){
					 document.location='/candidates.html';
					 }else if(data.user_jtype=='JEmployerSpanish'){
					 document.location='/es/empleadores.html';
					 }else if(data.user_jtype=='JCandidateSpanish'){
					 document.location='/es/candidatos.html';
					 }else{
					 document.location='/index.html';}
                  });
                });
          }
          else
          {
                $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
                {
                  //add message and change the class of the box and start fading
                  $(this).html('Your login is incorrect...').addClass('messageboxerror').fadeTo(900,1);
                });
          }
       });
       return false;//not to post the  form physically
};
var tryLogin = function(form){
	  submitLogin();
}

  $("#password").blur(function()
{
        submitLogin();
});
  var logout = function(){
	var redirect = Get_Cookie('u_jtype');
	Delete_Cookie('u_name','/');
	Delete_Cookie('u_type','/');
	Delete_Cookie('u_jtype','/');
	Delete_Cookie('u_id','/');
	Delete_Cookie('u_panname','/');
	Delete_Cookie('u_comname','/');
	
	if(redirect=='JEmployer'){
	document.location='/employers.html';}
	else if(redirect=='JCandidate'){
	document.location='/candidates.html';
	}else if(redirect=='JEmployerSpanish'){
	document.location='/es/empleadores.html';
	}else if(redirect=='JCandidateSpanish'){
	document.location='/es/candidatos.html';
	}else{
	document.location='/index.html';
	}
  }

