﻿//The validate.js file validates the input fields

function ValidateInfo(){

	if (Trim(document.jobpost.exactdesignation.value)=="")
	{
		alert("Please fill in the Exact Designation ");
		document.jobpost.exactdesignation.focus();
		return false;
	}else if (Trim(document.jobpost.organization.value)=="")
	{
		alert("Please fill in the Organization Name ");
		document.jobpost.organization.focus();
		return false;
	}else if (Trim(document.jobpost.joblocation.value)=="")
	{
		alert("Please fill in the Job Location ");
		document.jobpost.joblocation.focus();
		return false;
	}else if (Trim(document.jobpost.jobdesc.value)=="")
	{
		alert("Please fill in the Job Description ");
		document.jobpost.jobdesc.focus();
		return false;
	}else if (Trim(document.jobpost.emailx.value)=="")
	{
		alert("Please fill in the Contact Email");
		document.jobpost.emailx.focus();
		return false;
	}else if ((Trim(document.jobpost.emailx.value) != "") && (echeck(Trim(document.jobpost.emailx.value))==false))
	{
		document.jobpost.emailx.focus();
		return false;
	}else if (Trim(document.jobpost.telepx.value)=="")
	{
		alert("Please fill in the Contact Telephone ");
		document.jobpost.telepx.focus();
		return false;
	}else if (document.jobpost.username)			// as when loggedin this Form-Field is not created
	{
		if (Trim(document.jobpost.username.value)==""){
			alert("Please fill in the Username ");
			document.jobpost.username.focus();
			return false;
		}
	}else if (document.jobpost.password)		// as when loggedin this Form-Field is not created
	{
		if (Trim(document.jobpost.password.value)=="")
		{
			alert("Please fill in the Password");
			document.jobpost.password.focus();
			return false;
		}
	}
	else
	{
		return true;
	}
}


function Trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

// function for validating email address

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("You have entered an invalid email ID!")
			   
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("You have entered an invalid email ID!")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("You have entered an invalid email ID!")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("You have entered an invalid email ID!")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("You have entered an invalid email ID!")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("You have entered an invalid email ID!")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("You have entered an invalid email ID!")
		    return false
		 }

 		 return true					
	}
// end of the function


// check valid website address
function chk_url(curl){

	curl = curl.toUpperCase();
	
//	http_x = insturl.search("HTTP://");
	www_x = curl.search("WWW");
//	if (http_x >= 0 || www_x >= 0){    // any one is ok  http://amc.com  or  www.amc.com    or http://www.amc.com
	if (www_x >= 0){
		return true;
	} else {
		return false;
	}
}
// check Official email
function offcialmailchk(){
	usern = document.userInfo.username.value;
	compurl = document.userInfo.compurl.value;
	
	usern = usern.toUpperCase();
	compurl = compurl.toUpperCase();
	
	var srch_eml = new String(usern);
	emlpos = srch_eml.search("@");
	emlpos++;
	eml_url = usern.slice(emlpos);
	
	eml_pos = compurl.search(eml_url);
	
	if(eml_pos < 0){
		return false;
	} else {
		return true;
	}

}
//check tele/fax for only digits
function digits_only(digx){
	if (digx == ''){
		 return true;
	 }
	var pattern_d = /^\d{1,}$/;
	if (pattern_d.test(digx)){
		return true;
	}else{
		return false;
	}
}

