/* Function to validate jobs*/
function validateJobs()
{
	trimFields(obj);
	var isChecked = false;
	for(i = 0; i < obj.elements.length; i++)
	{
		if(obj.elements[i].type == "checkbox" && obj.elements[i].checked == true)
		{
			isChecked = true;
			break;
		}
	}
	if(obj.first_name.value == '')
	{
		alert(ALERT_ENTER_FIRST_NAME);
		obj.first_name.focus();
		return;
	}
	if(obj.last_name.value == '')
	{
		alert(ALERT_ENTER_LAST_NAME);
		obj.last_name.focus();
		return;
	}
	if(obj.address.value == '')
	{
		alert(ALERT_ENTER_ADDRESS);
		obj.address.focus();
		return;
	}
	if(obj.email_address.value == '')
	{
		alert(ALERT_ENTER_EMAIL);
		obj.email_address.focus();
		return;
	}
	if(!chkEmail(obj.email_address.value))
	{
		alert(ALERT_INVALID_EMAIL);
		obj.email_address.focus();
		obj.email_address.select();
		return;
	}
	if(obj.phone.value == '')
	{
		alert(ALERT_ENTER_PHONE);
		obj.phone.focus();
		return;
	}
	if(obj.reg_username.value == '')
	{
		alert(ALERT_USER_NAME);
		obj.reg_username.focus();
		return;
	}
	if(obj.reg_password.value == '')
	{
		alert(ALERT_PASSWORD);
		obj.reg_password.focus();
		return;
	}
	if(obj.reg_confirm_password.value == '')
	{
		alert(ALERT_CONFIRM_PASSWORD);
		obj.reg_confirm_password.focus();
		return;
	}
	if(obj.reg_password.value !=  obj.reg_confirm_password.value)
	{
		alert(ALERT_PASSWORD_MISMATCH);
		obj.reg_confirm_password.focus();
		obj.reg_confirm_password.select();
		return;
	}
	if(obj.company_name.value == '')
	{
		alert(ALERT_COMPANY_NAME);
		obj.company_name.focus();
		return;
	}
	if(obj.website.value != '' && !chkURL(obj.website.value))
	{
		alert(ALERT_ENTER_WEBSITE);
		obj.website.focus();
		return;
	}
	if(obj.contact_name.value == '')
	{
		alert(ALERT_CONTACT_NAME);
		obj.contact_name.focus();
		return;
	}
	if(obj.contact_phone.value == '')
	{
		alert(ALERT_CONTACT_PHONE);
		obj.contact_phone.focus();
		return;
	}
	if(obj.contact_email.value != '' && !chkEmail(obj.contact_email.value))
	{
		alert(ALERT_CONTACT_EMAIL);
		obj.contact_email.focus();
		return;
	}
	if(!isChecked)
	{
		alert(ALERT_CHECK_CATEGORY);
		document.getElementById('lnk_chk1').focus();
		return;
	}
	if(obj.job_title.value == '')
	{
		alert(ALERT_JOB_TITLE);
		obj.job_title.focus();
		return;
	}
	if(obj.company_city.value == '')
	{
		alert(ALERT_ENTER_CITY);
		obj.company_city.focus();
		return;
	}
	if(obj.company_state.value == '')
	{
		alert(ALERT_ENTER_STATE);
		obj.company_state.focus();
		return;
	}
	if(obj.country_name.selectedIndex == 0)
	{
		alert(ALERT_SELECT_COUNTRY);
		obj.country_name.focus();
		return;
	}
	if(tinyMCE.getContent('job_desc') == "")
	{
		alert(ALERT_ENTER_DESCRIPTION);
		obj.job_desc.focus();
		return;
	}
	if(obj.captcha.value == "")
	{
		alert(ALERT_SECURITY_CODE);
		obj.captcha.focus();
		return;
	}
	// If everything works fine
	obj.action = 'post_a_job.php';
	obj.submit();
}

// Function to cancel feedback
function cancelJob()
{
	var confMsg = 'Are you sure you want to Cancel?';
	if(confirm(confMsg))
	{
		self.location = 'index.php';
	}
}

/* Function to toggle checkbox of category*/
function toggleCategoryCheck(ele, chkName)
{
	if(document.getElementById(chkName).checked == false)
	{
		document.getElementById(chkName).checked = true;
		ele.className = 'chk_cat_sel';
	}
	else
	{
		document.getElementById(chkName).checked = false;
		ele.className = 'chk_cat';
	}
}

//=========================================================
//============ Check the username availability ============
//=========================================================
function checkUserName(ele, type)
{
	temp = type;
	if(obj.reg_username.value != "")
	{
		//Submit the Link to add
		var ajax = new AJAX();
		var arrParam = new Array();
		arrParam['user_name'] = obj.reg_username.value;
		ajax.getRequest("check_username.php", arrParam, showResult);
	}
}

//==================================================================
//============ Response handler for http request object ============
//==================================================================
function showResult(retVal)
{
	if(retVal == 'FAILURE')
	{
		obj.reg_username.style.backgroundColor = '#FC524D';
		document.getElementById("chk_user").innerHTML = 'User Name Already Exist!';
		obj.reg_username.focus();
		obj.reg_username.select();
	}
	else
	{
		obj.reg_username.style.backgroundColor = '#FFFFFF';
		document.getElementById("chk_user").innerHTML = '<span class="grn1">User Name is Available</span>';
	}
}

function validateLogin()
{
	trimFields(obj);
	if(obj.user_name.value == '')
	{
		alert(ALERT_USER_NAME);
		obj.user_name.focus();
		return false;
	}
	if(obj.password.value == '')
	{
		alert(ALERT_PASSWORD);
		obj.password.focus();
		return false;
	}
	 $.ajax({
	   type: "POST",
	   data: "user_name="+obj.user_name.value+'&password='+obj.password.value,
	   url: "process_user.php",
	   success: function(retVal){
			switch(retVal)
			{
				case "SUCCESS":
					self.location = 'my_jobs.php';
					break;
				case "FAILURE":
					alert(ALERT_LOGIN_FAILURE);
					obj.user_name.value = '';
					obj.password.value = '';
					obj.user_name.focus();
					break;
			}
	   }
	});
}
