//CHANGE REF # 71; GR 
//1. Added selected board to the parameter list of of the js function 'initClassCombo(selectedboard, selectedclass)'


function hideErrorPanel() 
{
	document.getElementById("errorPanel").style.display = "none";
	document.forms[1].password.value="";
	document.forms[1].confirm_password.value="";
	
}

function validate()
{	
	

	var errorMsgs = new Array();
	var errorFlag = true;
	var errorIndex = 0;
	
	if (isEmpty((document.getElementById("first_name").value)))
	{
		errorMsgs[errorIndex] = emptyFirstName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	
	if (!isValidCNames(document.getElementById("first_name").value) && !isEmpty(document.getElementById("first_name").value))
	{
		errorMsgs[errorIndex] = invalidFirstName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	/*if (valid(document.getElementById("first_name").value) && !isEmpty(document.getElementById("first_name").value))
	{
		errorMsgs[errorIndex] = invalidFirstName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}*/
	if ((!isEmpty(document.getElementById("mid_name").value) && !isValidCNames(document.getElementById("mid_name").value)))
	{
		errorMsgs[errorIndex] = invalidMiddleName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	if ((!isEmpty(document.getElementById("mid_name").value) && valid(document.getElementById("mid_name").value)))
	{
		errorMsgs[errorIndex] = invalidMiddleName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	
	if (isEmpty(document.getElementById("last_name").value))
	{
		errorMsgs[errorIndex] = emptyLastName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	
	if (!isValidCNames(document.getElementById("last_name").value) && !isEmpty(document.getElementById("last_name").value))
	{
		errorMsgs[errorIndex] = invalidLastName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	/*if ( valid(document.getElementById("last_name").value) && !isEmpty(document.getElementById("last_name").value))
	{
		errorMsgs[errorIndex] = invalidLastName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}*/
	var nickname = document.getElementById("nick_name").value;
	if (isEmpty(nickname))
	{
		errorMsgs[errorIndex] = emptyNickName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	
	if (!isEmpty(nickname) && !isValidNickName(nickname))
	{
		errorMsgs[errorIndex] = invalidNickName;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	var day = document.getElementById("DOBday").value;
	
	if (day == "null") {
		errorMsgs[errorIndex] = emptyBirthDay;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	var month = document.getElementById("DOBmn").value;
	
	if (month == "null") 
	{
		errorMsgs[errorIndex] = emptyBirthMonth;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	var year = document.getElementById("DOByr").value;
	
	if (year == "null")
	{
		errorMsgs[errorIndex] = emptyBirthYear;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	if ("null" != day && "null" != month && "null" != year)
	{
		if (!isValidDate(day, month, year))
		{
			errorMsgs[errorIndex] = invalidDOB;
			errorIndex = errorIndex + 1;
			errorFlag = false;
		}
	}
	var emailId = document.getElementById("email_address").value;
	if (isEmpty(emailId))
	{	
	
		
		errorMsgs[errorIndex] = emptyEmailAddress;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	if (!isEmpty(emailId) && -1 != isValidEmail(emailId.toLowerCase()))
	{	
		var ind=isValidEmail(emailId.toLowerCase());
		errorMsgs[errorIndex] = "Email Address : "+emailError[ind];//"Invalid Email Address.  Format is john@yahoo.com.";
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	var confirmEmailId = document.getElementById("confirm_email").value;
	
	if (isEmpty(confirmEmailId))
	{		
		errorMsgs[errorIndex] = emptyConfirmEmail;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	/*
	if (!isEmpty(confirmEmailId) && -1 != isValidEmail(confirmEmailId.toLowerCase()))
	{	
		var ind = isValidEmail(confirmEmailId.toLowerCase());
		errorMsgs[errorIndex] = "Confirm Email Address : " + emailError[ind];//"Invalid Confirm Email Address.  Format is john@yahoo.com.";
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	
	*/
	if ((!isEmpty(confirmEmailId) ) && (!isEmpty(emailId) ))
	{		
		if (emailId.toLowerCase() != confirmEmailId.toLowerCase())
		{	
			errorMsgs[errorIndex] = confirmEmailMistmatch;
			errorIndex = errorIndex + 1;
			errorFlag = false;
		}
	}
	var pwd = document.forms[1].password.value;
	
	if (isEmpty(pwd))
	{
		errorMsgs[errorIndex] = emptyPwd;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	
	if (!isEmpty(pwd) && !isValidPwd(pwd))
	{
		errorMsgs[errorIndex] = invalidPwd;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	var cnfrmPwd = document.forms[1].confirm_password.value;
	
	if (isEmpty(cnfrmPwd)) 
	{
		errorMsgs[errorIndex] = emptyConfirmPwd;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	/*
	if (!isEmpty(cnfrmPwd) && !isValidPwd(cnfrmPwd))
	{
		errorMsgs[errorIndex] = invalidConfirmPwd;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	*/
	if ((!isEmpty(pwd) ) && (!isEmpty(cnfrmPwd)))
	{
		if (pwd != cnfrmPwd)
		{
			pwd = "";
			cnfrmPwd = "";
			errorMsgs[errorIndex] = confirmPwdMistmatch;
			errorIndex = errorIndex + 1;
			errorFlag = false;
		}
	}
	if (document.getElementById("studclass").value == "null")
	{
		errorMsgs[errorIndex] = emptyStuudentClass;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	if (document.getElementById("board").value == "null")
	{
		errorMsgs[errorIndex] = emptyBoardOfEducation;
		errorIndex = errorIndex + 1;
		errorFlag = false;
	}
	if (errorFlag) {
		//alert("hi0")		
		checkIfEmailExisting(document.StudentRegistrationForm.email_address.value);
	}else{
	document.getElementById("errorPanel").style.display='block';
		
		document.getElementById("errorPanel").focus();
		var errorMsg = "";
		for (var i = 0; i < errorIndex; i++) {
			errorMsg = errorMsg +  "\t" + errorMsgs[i] + "<br>";
			}
			document.getElementById("errorPanel").innerHTML = errorMsg;
			document.getElementById("errorPanel").setAttribute('class', 'error');
			return
	
}
}


/*
Following script kept on hold for implementing disable copy paste feature
*/
/*
var omitformtags = ["input"];
omitformtags = omitformtags.join("|");
function disableselect(e) {
	if (omitformtags.indexOf(e.target.tagName.toLowerCase()) == -1) {
		return false;
	}
}
function reEnable() {
	return true;
}
if (typeof document.onselectstart != "undefined") {
	document.onselectstart = new Function("return false");
} else {
	document.onmouseover = enableselect;
	document.onmousedown = disableselect;
	document.onmouseup = reEnable;
}
var omitformtags = ["input"];
omitformtags = omitformtags.join("|");
function disableselect(e) {
	if (omitformtags.indexOf(e.target.tagName.toLowerCase()) == -1) {
		return false;
	}
}
function reEnable() {
	return true;
}
if (typeof document.onselectstart != "undefined") {
	document.onselectstart = new Function("return false");
} else {
	document.onmouseover = enableselect;
	document.onmousedown = disableselect;
	document.onmouseup = reEnable;
}*/
function initClassCombo(selectedBoard,selectedClass) {
	//alert(selectedBoard)
	document.getElementById("first_name").focus();
	if("null"==selectedBoard || ""==selectedBoard) 
		{
			document.getElementById("studclass").options.length = 1;
		
			return;
		}
	xmlHttp = getXMLHttpRequest();
	var queryString = "op=/getSchools.do&boardid="+encodeURI(selectedBoard);
	//alert("hi1")
	xmlHttp.open("POST", url, false);
	/*xmlHttp.onreadystatechange = function () {
	//alert("hi")
		handleStateChangeForClass(selectedClass);
	};*/
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(queryString);
//}
//function handleStateChangeForClass(selectedClass) {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			parseResultsForClass(selectedClass);
		}
	}
}
function parseResultsForClass(selectedClass) {
//alert("hi2"+selectedClass)
	var selectedFlag = false;
	if (selectedClass != undefined) {
		selectedFlag = true;
	}
	var size = xmlHttp.responseXML.getElementsByTagName("Size")[0].firstChild.data;
	document.getElementById("studclass").options.length = 1;
	if ("0" != size) {
		for (var index = 0; index < size; index++) {
			var cnId = xmlHttp.responseXML.getElementsByTagName("Id")[index].firstChild.data;
			var cnName = xmlHttp.responseXML.getElementsByTagName("Name")[index].firstChild.data;
			document.getElementById("studclass").options[index + 1] = new Option(cnName, cnId);
			if (selectedFlag) {
				if (selectedClass == cnId) {
					document.getElementById("studclass").options[index + 1].selected = true;
				}
			}
		}
	}
	//document.getElementById("studclass").options[index + 1] = new Option("Other", null);
}
//////////////////
function initBoardCombo(selectedBoard) {
	xmlHttp = getXMLHttpRequest();
	var queryString = "op=/getBoards.do";
	window.status = "please wait while the page is loading...";
	xmlHttp.open("POST", url, false);
	/*	xmlHttp.onreadystatechange = function () {
		handleStateForBoard(selectedBoard);
	};*/
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(queryString);
//}
//function handleStateForBoard(selectedBoard) {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			parseResultsForBoard(selectedBoard);
		}
	}
}
function parseResultsForBoard(selectedBoard) {
	var selectedFlag = false;
	if (selectedBoard != undefined) {
		selectedFlag = true;
	}
	var size = xmlHttp.responseXML.getElementsByTagName("Size")[0].firstChild.data;
	if ("0" != size) {
		for (var index = 0; index < size; index++) {
			var cnId = xmlHttp.responseXML.getElementsByTagName("Id")[index].firstChild.data;
			var cnName = xmlHttp.responseXML.getElementsByTagName("Name")[index].firstChild.data;
			document.getElementById("board").options[index + 1] = new Option(cnName, cnId);
			if (selectedFlag) {
				if (selectedBoard == cnId) {
					document.getElementById("board").options[index + 1].selected = true;
				}
			}
		}
	}
	window.status = "";
}


function checkIfEmailExisting(email) {
	xmlHttp = getXMLHttpRequest();
	var queryString = "op=chkForDupEmail&emailId=" + encodeURI(email);
	xmlHttp.open("POST", url, false);
	/*xmlHttp.onreadystatechange = function () {
		handleStateForCountry(selectedCountry, selectedState, selectedCity, selectedSchool);
	};*/
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(queryString);
//}
//function handleStateForCountry(selectedCountry, selectedState, selectedCity, selectedSchool) {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			parseResultsForisExistingEmail();
		}
	}
}

function parseResultsForisExistingEmail() {
	
	var isExisting = xmlHttp.responseXML.getElementsByTagName("isExisting")[0].firstChild.data;
	if ("true" != isExisting) {
		
		var op = "registerStudentEmail";
		document.StudentRegistrationForm.action = "StudentRegistrationTwo.do";
		document.StudentRegistrationForm.submit();
	} else {
	//alert("email is existing");
		document.getElementById("errorPanel").style.display = "block";
		document.getElementById("errorPanel").focus();
		document.getElementById("errorPanel").innerHTML = isExistingEmail;
		document.getElementById("errorPanel").setAttribute("class", "error");
		return;
	}
}

function openwindow(url)
{
window.open(url,"MyWindow","menubar=0,resizable=1,toolbar=0,status=0,scrollbars=yes")
}


function listClassesByBoard(){
	var selectedBoard = document.getElementById("board").value;
	var selectedClass = document.getElementById("studclass").value;
	initClassCombo(selectedBoard,'')
}