// JavaScript Document

function validateForgotPassword()
{
	 
	if(jQuery("#email").val()=='')
	{
		alert('Please enter email.');
		jQuery("#email").focus();
		return false;
	
	}
	else
	{
		if (echeck(jQuery("#email").val())==false){
			jQuery("#email").select();
			jQuery("#email").focus()
			return false;
		}
	} 
}



/////////////////////////////////////////////////////////////////////////////////////////
// Function to validate lodge a job 
/////////////////////////////////////////////////////////////////////////////////////////
function validateLodgeJob()
{   var dateObj = new Date();
	if(trimSpaces(jQuery("#position_title").val()) == "")
	{ 	alert("Please enter position title.");
		jQuery("#position_title").focus();
		jQuery("#position_title").attr("value","");
		return false;
	}
	if(jQuery("#job_employment_category_id").val() == "")
	{
		alert("Please select employment category.");
		jQuery("#job_employment_category_id").focus(); 
		return false;
	}
/*	if(jQuery("#job_industry_sector").val() == "")
	{
		alert("Please select industry sector.");
		jQuery("#job_industry_sector").focus(); 
		return false;
	}
	if(jQuery("#closingday").val()=="")
	{			
		alert("Please enter closing day.");
		jQuery("#closingday").focus(); 
		return false;
	}*/
	if(jQuery("#closingday").val()!="")
	{			
		if(checkNumeric(document.getElementById('closingday'),1,31,'','','') == false)  
		{
			 return false;
		}
	}
/*	if(jQuery("#closingmonth").val()=="")
	{	
		alert("Please enter closing month.");
		jQuery("#closingmonth").focus(); 
		return false;
	}*/
	if(jQuery("#closingmonth").val()!="")
	{			
		if(checkNumeric(document.getElementById('closingmonth'),1,12,'','','') == false)  
		{
			 return false;
		}
	}
/*	if(jQuery("#closingyear").val()=="")
	{	
		alert("Please enter closing year.");
		jQuery("#closingmonth").focus(); 
		return false;
	}*/
	if(jQuery("#closingyear").val()!="")
	{			
		if(checkNumeric(document.getElementById('closingyear'),dateObj.getFullYear(),parseInt(dateObj.getFullYear())+ parseInt(20),'','','') == false)  
		{
			 return false;
		}
	} 
	
	
	if(jQuery("#expiryday").val()=="")
	{			
		alert("Please enter expiry day.");
		jQuery("#expiryday").focus(); 
		return false;
	}
	else if(jQuery("#expiryday").val()!="")
	{			
		if(checkNumeric(document.getElementById('expiryday'),1,31,'','','') == false)  
		{
			 return false;
		}
	}
	if(jQuery("#expirymonth").val()=="")
	{	
		alert("Please enter expiry month.");
		jQuery("#expirymonth").focus(); 
		return false;
	}
	else if(jQuery("#expirymonth").val()!="")
	{			
		if(checkNumeric(document.getElementById('expirymonth'),1,12,'','','') == false)  
		{
			 return false;
		}
	}
	if(jQuery("#expiryyear").val()=="")
	{	
		alert("Please enter expiry year.");
		jQuery("#expiryyear").focus(); 
		return false;
	}
	else if(jQuery("#expiryyear").val()!="")
	{			
		if(checkNumeric(document.getElementById('expiryyear'),dateObj.getFullYear(),parseInt(dateObj.getFullYear())+ parseInt(20),'','','') == false)  
		{
			 return false;
		}
	} 	
	if(jQuery("#country_id").val()=="")
	{			
		alert("Please enter country.");
		jQuery("#country_id").focus(); 
		return false;
	}
	if(trimSpaces(jQuery("#short_description").val())=="")
	{			
		alert("Please enter short description.");
		jQuery("#short_description").focus(); 
		jQuery("#short_description").attr("value",""); 
		return false;
	}
	if(jQuery("#long_description").val()=="" && trimSpaces(jQuery("#external_desc_url").val())=="")
	{			
		alert("Please enter long description or external description url.");
		return false;
	}	
	
	if(trimSpaces(jQuery("#contact_infomation").val())=="")
	{			
		alert("Please enter contact infomation.");
		jQuery("#contact_infomation").focus(); 
		jQuery("#contact_infomation").attr("value",""); 
		return false;
	}
	if(jQuery("#code").val()=="")
	{			
		alert("Please enter code.");
		jQuery("#code").focus(); 		 
		return false;
	}
}

function validatePositionWanted()
{  
	if(trimSpaces(jQuery("#first_name").val()) == "")
	{ 	alert("Please enter first name.");
		jQuery("#first_name").focus();
		jQuery("#first_name").attr("value","");
		return false;
	}
	if(trimSpaces(jQuery("#last_name").val()) == "")
	{ 	alert("Please enter last name.");
		jQuery("#last_name").focus();
		jQuery("#last_name").attr("value","");
		return false;
	}
	if(jQuery("#email").val()=='')
	{
		alert('Please enter email.');
		jQuery("#email").focus();
		return false;
	
	}
	else
	{
		if (echeck(jQuery("#email").val())==false){
			jQuery("#email").select();
			jQuery("#email").focus()
			return false;
		}
	} 
	if(trimSpaces(jQuery("#position_title").val()) == "")
	{ 	alert("Please enter position title.");
		jQuery("#position_title").focus();
		jQuery("#position_title").attr("value","");
		return false;
	}
	if(jQuery("#job_employment_category_id").val() == "")
	{
		alert("Please select employment category.");
		jQuery("#job_employment_category_id").focus(); 
		return false;
	}
	if(jQuery("#country_id").val()=="")
	{			
		alert("Please enter country.");
		jQuery("#country_id").focus(); 
		return false;
	}
	if(trimSpaces(jQuery("#resume").val())=="")
	{			
		alert("Please enter your skills, job desires and CV.");
		jQuery("#resume").focus(); 
		jQuery("#resume").attr("value",""); 
		return false;
	}
	if(trimSpaces(jQuery("#code").val())=="")
	{			
		alert("Please enter code.");
		jQuery("#code").focus(); 
		jQuery("#code").attr("value",""); 
		return false;
	}
}
 jQuery(function()
        {
            jQuery('#resume').keyup(function(e)
            {
                var maxLength = 1500;
                var textlength = this.value.length;
                if (textlength >= maxLength)
                {
                    jQuery('#charStatus').html('You cannot write more then <span class="required">' + maxLength + '</span> characters!');
                    this.value = this.value.substring(0, maxLength);
                    e.preventDefault();
                }
                else
                {
                    jQuery('#charStatus').html('You have <span class="required">' + (maxLength - textlength) + '</span> characters left.');
                }
            });
        });


function validateSearchPositionWanted()
{
	if(trimSpaces(jQuery("#s_key").val())=="")
	{
		alert("Please enter keyword to search position.");
		jQuery("#s_key").focus(); 
		jQuery("#s_key").attr("value",""); 
		return false;
	}
	
}
function validateSearchJob()
{
	if(trimSpaces(jQuery("#s_key").val())=="")
	{
		alert("Please enter keyword to search job.");
		jQuery("#s_key").focus(); 
		jQuery("#s_key").attr("value",""); 
		return false;
	}
	
}
function validateRegLodger()
{
	if(trimSpaces(jQuery("#first_name").val())=="")
	{
		alert("Please enter first name.");
		jQuery("#first_name").focus(); 
		jQuery("#first_name").attr("value",""); 
		return false;
	}
	if(trimSpaces(jQuery("#last_name").val())=="")
	{
		alert("Please enter last name.");
		jQuery("#last_name").focus(); 
		jQuery("#last_name").attr("value",""); 
		return false;
	}
	if(trimSpaces(jQuery("#username").val())=="")
	{
		alert("Please enter username.");
		jQuery("#username").focus(); 
		jQuery("#username").attr("value",""); 
		return false;
	}else
	{
		if (echeck(jQuery("#username").val())==false){
			jQuery("#username").select();
			jQuery("#username").focus()
			return false;
		}
	}
	if(trimSpaces(jQuery("#password").val())=="")
	{
		alert("Please enter password.");
		jQuery("#password").focus(); 
		jQuery("#password").attr("value",""); 
		return false;
	}
	if(trimSpaces(jQuery("#c_password").val())!= trimSpaces(jQuery("#password").val()))
	{
		alert("Password mismatch.");
		jQuery("#c_password").focus(); 
		jQuery("#c_password").attr("value",""); 
		return false;
	}
	
}
function validateProfileLodger()
{
	if(trimSpaces(jQuery("#first_name").val())=="")
	{
		alert("Please enter first name.");
		jQuery("#first_name").focus(); 
		jQuery("#first_name").attr("value",""); 
		return false;
	}
	if(trimSpaces(jQuery("#last_name").val())=="")
	{
		alert("Please enter last name.");
		jQuery("#last_name").focus(); 
		jQuery("#last_name").attr("value",""); 
		return false;
	}
	 
	if(trimSpaces(jQuery("#password").val())!="")
	{		 
		if(trimSpaces(jQuery("#c_password").val())!= trimSpaces(jQuery("#password").val()))
		{
			alert("Password mismatch.");
			jQuery("#c_password").focus(); 
			jQuery("#c_password").attr("value",""); 
			return false;
		}
	}
}
function validateLoginLodger()
{
	if(trimSpaces(jQuery("#username").val())=="")
	{
		alert("Please enter username.");
		jQuery("#username").focus(); 
		jQuery("#username").attr("value",""); 
		return false;
	}
	if(trimSpaces(jQuery("#password").val())=="")
	{
		alert("Please enter password.");
		jQuery("#password").focus(); 
		jQuery("#password").attr("value",""); 
		return false;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function trimSpaces(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}

function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}



function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
// only allow 0-9 be entered, plus any values passed
// (can be in any order, and don't have to be comma, period, or hyphen)
// if all numbers allow commas, periods, hyphens or whatever,
// just hard code it here and take out the passed parameters
var checkOK = "0123456789" + comma + period + hyphen;
var checkStr = objName;
var allValid = true;
var decPoints = 0;
var allNum = "";

for (i = 0;  i < checkStr.value.length;  i++)
{
ch = checkStr.value.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{	
alertsay = "Please enter only these values \""
alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
alert(alertsay);
return (false);
}

// set the minimum and maximum
var chkVal = allNum;
var prsVal = parseInt(allNum);
if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
{
alertsay = "Please enter a value greater than or "
alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
alert(alertsay);
return (false);
}
}

/**************************************
EMAIL VALIDATION
****************************************/
function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
   alert("Invalid E-mail ID")
   return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   alert("Invalid E-mail ID")
   return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    alert("Invalid E-mail ID")
    return false
}

 if (str.indexOf(at,(lat+1))!=-1){
    alert("Invalid E-mail ID")
    return false
 }

 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    alert("Invalid E-mail ID")
    return false
 }

 if (str.indexOf(dot,(lat+2))==-1){
    alert("Invalid E-mail ID")
    return false
 }

 if (str.indexOf(" ")!=-1){
    alert("Invalid E-mail ID")
    return false
 }

	 return true					
} 


