﻿//The validate.js file validates the input fields

function ValidateInfo(){

var catx=""; levels="";
var radioAll = document.getElementsByTagName("input");
var totradio = radioAll.length;
for(var i=0; i < totradio; i++){
	obj = radioAll.item(i);
    if(obj.name == "levelx" && obj.checked == true){
        levels = obj.value;
    }
    if(obj.name == "catx" && obj.checked == true){
        catx = obj.value;
    }    
}
        
if (Trim(document.userInfo.salutation.value)=="") {	
	alert("Please select the salutation field!");
	document.userInfo.salutation.focus();
	return false;
} else if (Trim(document.userInfo.fname.value)=="") {
	alert("Please fill in the First Name field!");
	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();
	return false;
} else if (catx == ""){
	alert("Please tell us what best describes you?");
	//document.userInfo.catx.focus();
	return false;
} else if (levels == ""){
	alert("Please fill in the Sub-Catagory for " + catx);
	//document.userInfo.levelx.focus();
	return false;
} else if ((catx == "Educational Institution") && (Trim(document.userInfo.orgins.value)=="")){
	alert("Please fill in the Organization/Institution Name field!");
	document.userInfo.orgins.focus();
	return false;
} else if ((catx == "Educational Institution") && (Trim(document.userInfo.compurl.value)=="")){
	alert("Please fill in the Official URL field!");
	document.userInfo.compurl.focus();
	return false;
} else if ((catx == "Educational Institution") && (chk_url((Trim(document.userInfo.compurl.value)))==false)){
	alert("Website Address seems invalid, please check");
	document.userInfo.compurl.focus();
	return false;
} else if (Trim(document.userInfo.username.value) == ""){    
    if(catx == "Student" || catx == "Tutor"){
	    alert("Please fill in the E-mail field!\nIt should be your e-mail id.");
	} else {
	    alert("Please fill in the Email field!\nIt should be your official e-mail id.");
	}
	document.userInfo.username.value = "";
	document.userInfo.password.value = "";
	document.userInfo.cpassword.value = "";
	document.userInfo.username.focus();
	return false;
} else if (echeck(document.userInfo.username.value) == false) {
	document.userInfo.username.focus();
	return false;
} else if ((catx != "Student" && catx != "Tutor") && (!offcialmailchk())) {
	alert("Your E-mail needs to be your official E-mail. Example: john@yourorganization.com");
	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 {
	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


// 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 offcialmailchk
function offcialmailchk(){
	var	usern = document.userInfo.username.value;
	var	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;
	}
}

