﻿//The validate.js file validates the input fields

function ValidateInfo(){

if (document.userInfo.newsletterYN.checked == false)
{
	document.userInfo.newsletter.value="N";
}
if (document.userInfo.newsletterYN.checked == true)
{
	document.userInfo.newsletter.value="Y";
}

if (Trim(document.userInfo.salutation.value)=="")
{
alert("Please select the salutation field!");
document.userInfo.salutation.value="";
document.userInfo.salutation.focus();
return false;
}
else if (Trim(document.userInfo.fname.value)=="")
{
alert("Please fill in the First Name field!");
document.userInfo.fname.value="";
document.userInfo.fname.focus();
return false;
}
else if (Trim(document.userInfo.lname.value)=="")
{
alert("Please fill in the Last Name field!");
document.userInfo.lname.focus();
document.userInfo.lname.value="";
return false;
}
/* else if (Trim(document.userInfo.address.value)=="")
{
alert("Please fill in the address field!");
document.userInfo.address.value="";
document.userInfo.address.focus();
return false;
} */
else if (Trim(document.userInfo.cityname.value)=="")
{
alert("Please fill in the City field!");
document.userInfo.cityname.value="";
document.userInfo.cityname.focus();
return false;
}
else if (Trim(document.userInfo.co.value)=="")
{
alert("Please select the Country field!");
document.userInfo.co.value="";
document.userInfo.co.focus();
return false;
}
else if (Trim(document.userInfo.pin.value)=="")
{
alert("Please fill the pin field!");
document.userInfo.pin.value="";
document.userInfo.pin.focus();
return false;
}
else if (document.userInfo.area_of_int.value=="")
{
alert("Please select in the area of interest field!");
document.userInfo.area_of_int.focus();
return false;
}
else if (Trim(document.userInfo.lookingfor.value)=="")
{
alert("Please select the Qualification Type!");
document.userInfo.lookingfor.value="";
document.userInfo.lookingfor.focus();
return false;
}
else if (Trim(document.userInfo.coursetype.value)=="")
{
alert("Please select the course type field!");
document.userInfo.coursetype.value="";
document.userInfo.coursetype.focus();
return false;
}
else if (Trim(document.userInfo.co_pref.value)=="")
{
alert("Please select the prefered location field!");
document.userInfo.co_pref.value="";
document.userInfo.co_pref.focus();
return false;
}
else if (Trim(document.userInfo.finance.value)=="")
{
alert("Please select the finance field!");
document.userInfo.finance.value="";
document.userInfo.finance.focus();
return false;
}
else if (Trim(document.userInfo.username.value)=="")
{
alert("Please fill in the User Name field!");
document.userInfo.username.value="";
document.userInfo.username.focus();
return false;
}
else if (echeck(document.userInfo.username.value)==false)
{
document.userInfo.username.value="";
document.userInfo.username.focus();
return false;
}
else if (Trim(document.userInfo.password.value)=="")
{
alert("Please fill in the Password field!");
document.userInfo.password.value="";
document.userInfo.password.focus();
return false;
}
else if (Trim(document.userInfo.password.value).length<6)
{
alert("Password must be between 6-12 character long!");
document.userInfo.password.value="";
document.userInfo.cpassword.value="";
document.userInfo.password.focus();
return false;
}
else if (Trim(document.userInfo.cpassword.value)=="")
{
alert("Please fill in the Confirm Password field!");
document.userInfo.cpassword.value="";
document.userInfo.cpassword.focus();
return false;
}
else if (Trim(document.userInfo.password.value)!=Trim(document.userInfo.cpassword.value))
{
alert("Passwords don't match!");
document.userInfo.password.value="";
document.userInfo.cpassword.value="";
document.userInfo.password.focus();
return false;
}
	else if (document.userInfo.terms.checked == false)
	{
		alert("Terms and Conditions should be accepted!");
		document.userInfo.terms.value="";
		document.userInfo.terms.focus();
		return false;
	}
else
{
return true;
}
}

var keyProtected = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890_','Alpha-numeric input only.');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxyz. ','Charater input only.');
var keybNumeric = new keybEdit('0123456789 -.+','Numeric input only.');
var keyMail = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890_.@-','E-mail input only.');

function keybEdit(strValid, strMsg) {
	//	Variables
	var reWork = new RegExp('[a-z]','gi');		//	Regular expression\

	//	Properties
	if(reWork.test(strValid))
		this.valid = strValid.toLowerCase() + strValid.toUpperCase();
	else
		this.valid = strValid;

	if((strMsg == null) || (typeof(strMsg) == 'undefined'))
		this.message = '';
	else
		this.message = strMsg;

	//	Methods
	this.getValid = keybEditGetValid;
	this.getMessage = keybEditGetMessage;
	
	function keybEditGetValid() {

		return this.valid.toString();
	}
	
	function keybEditGetMessage() {
		
		return this.message;
	}
}

void function editKeyBoard(objForm, objKeyb) {
	strWork = objKeyb.getValid();
	strMsg = '';							// Error message
	blnValidChar = false;					// Valid character flag

	// Part 1: Validate input
	if(!blnValidChar)
		for(i=0;i < strWork.length;i++)
			if(window.event.keyCode == strWork.charCodeAt(i)) {
				blnValidChar = true;

				break;
			}

	// Part 2: Build error message
	if(!blnValidChar) {
		if(objKeyb.getMessage().toString().length != 0)
			//alert('Error: ' + objKeyb.getMessage());

		window.event.returnValue = false;		// Clear invalid character
		objForm.focus();						// Set focus
	}
}


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

