﻿//The validate.js file validates the input fields

function ValidateInfo() {

	returnval = "T";
	var uemail = document.CourseInfo.cUserName.value;
	
	if (Trim(document.CourseInfo.cInsName.value)=="") {
		alert("Please fill in the Institute's Name field!");
		document.CourseInfo.cInsName.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.website.value)=="") {
		alert("Please fill in the Institute Website!");
		document.CourseInfo.website.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cName.value)=="") {
		alert("Please fill in the Course Name field!");
		document.CourseInfo.cName.focus();
		returnval = "F";
	} else if (document.CourseInfo.category.value=="") {
		alert("Please select the Category field!");
		document.CourseInfo.category.focus();
		returnval = "F";
	} else if (document.CourseInfo.coursetype.value=="") {
		alert("Please select the Type of Course !");
		document.CourseInfo.coursetype.focus();
		returnval = "F";
	} else if ((document.CourseInfo.coursetype.value=="Degree") && (document.CourseInfo.coursedegree.value=="")) {
		alert("Please select the Degree Level !");
		document.CourseInfo.coursedegree.focus();
		returnval = "F";
	} else if (document.CourseInfo.co.value=="") {
		alert("Please select the Country field!");
		document.CourseInfo.co.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cCity.value)=="") {
		alert("Please fill in the City field!");
		document.CourseInfo.cCity.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cRegistration.value)=="") {
		alert("Please fill in the Entry Requirements field!");
		document.CourseInfo.cRegistration.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.coursemode.value)=="") {
		alert("Please fill in the Course Study Mode field!");
		document.CourseInfo.coursemode.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cDuration.value)=="") {
		alert("Please fill in the Course Duration field! \n(Only Digits 0-9)");
		document.CourseInfo.cDuration.focus();
		returnval = "F";
	} else if (digits_only(document.CourseInfo.cDuration.value) == false) {
		alert("Only Digits [0-9] should be filled in Course Duration field!\n blank space not allowed");
		document.CourseInfo.cDuration.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.duration_type.value)=="") {
		alert("Please select the Course Duration \n( days  or  months  )");
		document.CourseInfo.duration_type.focus();
		returnval = "F";
	} else if ((document.CourseInfo.cDetails.value)=="") {
		alert("Please fill in the Course Details field!");
		document.CourseInfo.cDetails.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cFee.value)=="") {
		alert("Please fill in the Course Fee field! \nOnly digits [0-9]\n blank space not allowed");
		document.CourseInfo.cFee.focus();
		returnval = "F";
	} else if (digits_only(document.CourseInfo.cFee.value) == false) {
		alert("Only Digits [0-9] should be filled in Course Fee field!");
		document.CourseInfo.cFee.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cCurrency.value)=="") {
		alert("Please Select the Currency For Course Fee");
		document.CourseInfo.cCurrency.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.startDate.value)=="") {
		alert("Please fill in the Course Start Date !\n\nby clicking on the Calander Icon");
		document.CourseInfo.startDate.focus();
		returnval = "F";
	} else if (new Date(document.getElementById("startDate").value) < new Date()) {
		alert("Course Start Date should be greated than Current Date");
		document.CourseInfo.startDate.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cContact.value)=="") {
		alert("Please fill in the Contact Person field!");
		document.CourseInfo.cContact.focus();
		returnval = "F";
	} else if ((Trim(uemail) == "") || echeck(Trim(uemail)) === false) {
		alert("Please fill in the valid contact email");
		document.CourseInfo.cUserName.focus();
		returnval = "F";
	} else if (Trim(document.CourseInfo.cPassword.value)=="") {
		alert("Please fill in the Password");
		document.CourseInfo.cPassword.focus();
		returnval = "F";	
	} else if (document.CourseInfo.agree.checked== false) {
		alert("Please confirm that you have read Terms of Use and Privacy Policy");
		document.CourseInfo.agree.focus();
		returnval = "F";	
	}	
	
	if (returnval == "F"){
		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 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;
	}
}

