// JavaScript Document


//	**************   Global variables are very important so dont delete any one.  ******************************/
	var results       = "";  // This used in Ajax request functions.
	var inlineIdsArr  = "";  // Array Variable of inline elements.
	var noneIdsArr    = "";  // Array Variable of hide elements.
	var url           = "";   // Create the url of the ajax request file.
	var urlString     = "";  // Variable For Url String.
	var objectIdsArr  = ""; //  Array used for collect Document objects.
	var waitingImage  = "<img src='images/waitimage.gif'>"; // will display the Please wait Image.
	var oldAction     = 0;	
	
//	***************************   Block End of Global variables ************************************************/

var hostUrl = "";



// Function set the ajax Asyncronouse request and give the output in results variable.
// url is the calling file with complete path.
// calling function is the var want to called after readyState == 4 return from ajax function. eg. setRequestResult()
function ajaxAsyncronouseRequest(url,callingFunction){
	var http = zXmlHttp.createRequest();
	http.open("GET", url, true);
	http.onreadystatechange = function(){
		if(http.readyState == 4){
			results = http.responseText;
			if (http.status != 200){
				if(http.status == 12031){
					alert("Problem of Internet");
				}else{
					alert("Problem of Internet : " + http.statusText);
				}
			}else{
				if(callingFunction!=""){
					//alert(results);
					
					var calledFunction = callingFunction;
					//alert(calledFunction);
					eval(calledFunction);	// calling functioin here.
				}
			}
		}
	}
	http.send(null);
}
// EOF ajaxAsyncronouseRequest



// Function used For Hide(none) and Show(inline/block) Form Elements.
// inlineIdsArr is a collection of Object Id's Array for inline(block) their display properties.
// noneIdsArr is a collection of Object Id's Array for none their display properties.
function inlineNoneObjects(inlineIdsArr,noneIdsArr){
	if(noneIdsArr.length != null){
		for(j=0;j<noneIdsArr.length;j++){
			document.getElementById(noneIdsArr[j]).style.display = "none";
		}
	}
	if(inlineIdsArr.length != null){
		for(i=0;i<inlineIdsArr.length;i++){
			document.getElementById(inlineIdsArr[i]).style.display = "";
		}
	}
}
// EOF inlineNoneObjects

// Function set the ajax Syncronouse request and give the output in results variable.
// url is the specified path of the file.
// divId is the var to show the generated result.
function ajaxSyncronouseRequest(url,divId){
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;
	if(divId !=""){
		document.getElementById(divId).style.display = "inline";
		document.getElementById(divId).innerHTML = results;
	}
}
// EOF ajaxSyncronouseRequest


// FUCTNION WILL SHOW AND HIDE THE SPECIFIC DOCUMENT ELEMENT.
// PARAMETERS -> 1) objectId - ELEMENT ID WANT TO SHOW OR HIDE.
// 				 2) action   - IF YOU PASS 'Show' THEN ELEMENTS DISPLAY PROPERTY WILL INLINE OR BLOCK
// IF YOU ARE NOT PASS action VALUE THEN FUNCTION WORK LIKE FIREST ELEMENT SHOW AND SECOND TIME HIDE
function showHideObject(objectId,action){
	if(action!=""){
		if(action == "Show"){
			document.getElementById(objectId).style.display = "";
		}else{
			document.getElementById(objectId).style.display = "none";
		}
	}else{
		if(oldAction%2 == 0){
			document.getElementById(objectId).style.display = "";
		}else{
			document.getElementById(objectId).style.display = "none";
		}
		oldAction++;
	}
}
// EOF showHideObject

// FUNCTION WILL DISPLAY THE WAITING IMAGE IN CENTER OF OUR BROWSER.
// IT CREATE ONE DIV ELEMENT AND APPLY ONE CSS 'waitImage' SO APPEAR THIS DIV AT CENTER
function displayWaitImageInCenter(){ 
  	var myPopupDiv = document.createElement('DIV');
	myPopupDiv.setAttribute("id","popupDiv");
	myPopupDiv.className = "waitImage";
	myPopupDiv.innerHTML = waitingImage;
	this.document.body.appendChild(myPopupDiv);
}
// EOF displayWaitImageInCenter

// FUNCTION WILL DELETE THE CREATED DIV FORM THE FUCNTION displayWaitImageInCenter 
// THIS FUNCTION WILL CALL AFTER THE displayWaitImageInCenter FUNCTION.
function hideWaitImageInCenter(){
	  var popUp   = document.body;
	  popupDetele = document.getElementById("popupDiv");
	  popUp.removeChild(popupDetele);
}
// EOF hideWaitImageInCenter




function setFocusToField(fieldId){
	document.getElementById(fieldId).focus();
}


function backToHomePage(){
	window.open(window.parent.location, "_parent");	
}


function createCountryCombo(inDivId,fo,cmbName,onchangeFunction)
{	
	action	= "getCountryRec";
	url		= hostUrl+"include/php/ajax/studentAS.php?action="+action+"&cmbName="+cmbName+"&firstOption="+fo+"&onchangeFunction="+onchangeFunction;	
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;	
	document.getElementById(inDivId).innerHTML = results;
}

function createNationalityCombo(inDivId,fo,cmbName,onchangeFunction)
{	
	action	= "getNationalityRec";
	url		= hostUrl+"include/php/ajax/studentAS.php?action="+action+"&cmbName="+cmbName+"&firstOption="+fo+"&onchangeFunction="+onchangeFunction;	
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;	
	document.getElementById(inDivId).innerHTML = results;
}



function updateStateCombo(Obj,inDivId,fo,cmbName,selValue)
{	
	var countryId	= Obj.value;
	var cmbObj	= document.getElementById(cmbName);
	var i = 0;	
	if(countryId == 'other')
	{		
		if(cmbName == "home_state")
		{				
			document.getElementById('homecountrydiv').style.display = "";
		}
		else
		{
			document.getElementById('corcountrydiv').style.display = "";			
		}	
	}
	else
	{
		if(cmbName == "home_state")
			document.getElementById('homecountrydiv').style.display = "none";
		else
			document.getElementById('corcountrydiv').style.display = "none";
	}

	cmbObj.options.length	= 0;
	cmbObj.options[0]		= new Option("Please Wait...","");
	
	action	= "getStateRec";
	url		= hostUrl+"include/php/ajax/studentAS.php?action="+action+"&countryId="+countryId;
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;	
	cmbObj.options.length	= 0;
	if (fo=='S'){
		cmbObj.options[0]	= new Option("--Select--","");
	}
	else{
		cmbObj.options[0]	= new Option("---All---","");
	}	
	if(results != ""){		
		resultArr	= results.split('`');
		for(i=1;i<=resultArr.length;i++){
			optArr	= resultArr[i-1].split('~');
			if (optArr[0] == selValue){
				cmbObj.options[i]	= new Option(optArr[1],optArr[0],true);
			}
			else{
				cmbObj.options[i]	= new Option(optArr[1],optArr[0]);
			}
		}
		cmbObj.options[i]	= new Option("other","other");
	}
	else
	{
		cmbObj.options[i+1]	= new Option("other","other");
	}
	
}

function updateCityCombo(Obj,inDivId,fo,cmbName,selValue)
{	
	
	var stateId	= Obj.value;
	var cmbObj	= document.getElementById(cmbName);
	var i = 0;	
	if(stateId == 'other')
	{			
		if(cmbName == "home_city")
		{
			document.getElementById("homestatediv").style.display = "";			
		}
		else
		{			
			document.getElementById("corstatediv").style.display = "";						
		}		
	}
	else
	{
		if(cmbName == "home_city")
			document.getElementById('homestatediv').style.display = "none";
		else
			document.getElementById('corstatediv').style.display = "none";
		
	}
	
	cmbObj.onchange = function(){ changeCityCombo(this,cmbName); };	

	cmbObj.options.length	= 0;
	cmbObj.options[0]		= new Option("Please Wait...","");
	
	action	= "getCityRec";
	url		= hostUrl+"include/php/ajax/studentAS.php?action="+action+"&stateId="+stateId;
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;
	cmbObj.options.length	= 0;
	if (fo=='S'){
		cmbObj.options[0]	= new Option("--Select--","");
	}
	else{
		cmbObj.options[0]	= new Option("---All---","");
	}	
	if(results != ""){			
		resultArr	= results.split('`');
		for(i=1;i<=resultArr.length;i++){
			optArr	= resultArr[i-1].split('~');
			if (optArr[0] == selValue){
				cmbObj.options[i]	= new Option(optArr[1],optArr[0],true);
			}
			else{
				cmbObj.options[i]	= new Option(optArr[1],optArr[0]);
			}
		}
		cmbObj.options[i]	= new Option("other","other");
	}
	else
	{
		cmbObj.options[i+1]	= new Option("other","other");
	}
	
}

function changeCityCombo(obj,cmbName)
{	
	val = obj.value;
	if(val == "other")
	{
		if(cmbName == 'home_city')
		{
			document.getElementById("homecitydiv").style.display = "";		
		}
		else
		{
			document.getElementById("corcitydiv").style.display = "";	
		}
	}
	else
	{
		if(cmbName == 'home_city')
		{
			document.getElementById("homecitydiv").style.display = "none";		
		}
		else
		{
			document.getElementById("corcitydiv").style.display = "none";	
		}
	}
}
function showHideNationality(obj)
{
	cmbValue = obj.value;
	if(cmbValue == 'other')
		document.getElementById("nationalitydiv").style.display = "";
	else
		document.getElementById("nationalitydiv").style.display = "none";
}


function getCourseCode(obj)
{
	var courseID = obj.value;
	if (courseID != ""){
		url = "../include/php/ajax/get_course_details.php?act=getCourseCode&courseID="+courseID;
		var http = zXmlHttp.createRequest();
		http.open("GET", url, false);
		http.send(null);
		results = http.responseText;
		resultArr	= results.split('~');
		document.getElementById('course_code').value = resultArr[0];
		document.getElementById('duration').value    = resultArr[1];
	}
}

function getSessionDetails(obj)
{
	if (document.getElementById('stud_course_type').value == ""){
		alert("Select course.");
		document.getElementById('stud_course_type').focus();
		obj.value	= "";
		return false;
	}
	var courseID  = document.getElementById('stud_course_type').value;
	var sessionId = obj.value;
	
	if (sessionId == ""){
		document.getElementById('course_start_date').value 		= "";
		document.getElementById('course_end_date').value 		= "";
		obj.focus();
		return false;
	}
	if (document.getElementById('fee_status').value == 'O'){
		var feesfor	=  'foroverseas';
	}
	else{
		var feesfor	= 'forlocal';
	}	
	
	url = "../include/php/ajax/get_course_details.php?act=fetchSessionDetails&courseID="+courseID+"&sessionId="+sessionId+"&feesfor="+feesfor;
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;
	resultsa = results.split("~");
	document.getElementById('course_start_date').value 		= resultsa[0];
	document.getElementById('course_end_date').value 		= resultsa[1];
}

// FUNCTION CREATED BY SUNIL ON MARCH 30, 2010
// WILL CHANGE THE DURATION AND LEVEL COMBO BOX ACCORDING TO THAT
// PARAM - 1) shortCourseCombo  - SHORT COURSE COMBO BOX NAME
//         2) durationCombo     - DURATION COMBO BOX NAME
//         3) levelCombo        - LEVEL COMBO BOX NAME
function shortCourseComboChangeEvent(shortCourseCombo,durationCombo,levelCombo){
	var levelComboTR 	 = levelCombo+"TR";
	var shortCourseCombo = document.getElementById(shortCourseCombo);
	if(durationCombo != ""){
		document.getElementById(durationCombo).options.length = 0;
		document.getElementById(durationCombo).disabled = false;
		document.getElementById(durationCombo).options[0] = new Option("- - - Select - - -","");
	}
	if(levelCombo != ""){
		//Following line is commented for creating Term Level Checkbox on 08-06-2010 By Abhilash
		//document.getElementById(levelCombo).options.length    = 0;
		//document.getElementById(levelCombo).options[0]    = new Option("- - - Select - - -","");
		//Following line is added for creating Term Level Checkbox on 08-06-2010 By Abhilash
		document.getElementById(levelComboTR).style.display 	= "none";
		document.getElementById(levelCombo).innerHTML 			= "";
	}
	var levelDataArr = "";
	var levels       = "";
	shortCourseData.sort();
	if(shortCourseCombo.value != ""){
		for(var c=0; c<shortCourseData.length; c++){
			if(shortCourseCombo.value == shortCourseData[c][0]){
				if(durationCombo !=""){
					if(shortCourseData[c][2] == "F"){
						var fixedWeekArr = shortCourseData[c][3].split(',');
						for(var d=0; d<fixedWeekArr.length; d++){
							document.getElementById(durationCombo).options[document.getElementById(durationCombo).options.length] = new Option(fixedWeekArr[d],fixedWeekArr[d]);
						}
					}else{
						for(var d=1; d<=shortCourseData[c][1]; d++){
							document.getElementById(durationCombo).options[document.getElementById(durationCombo).options.length] = new Option(d,d);
						}
					}
				}
				if(levelCombo !=""){
					if(shortCourseData[c][4]){
						levelDataArr = shortCourseData[c][4].split('@');
						var levelCheckbox = "";
						for(l=0;l<levelDataArr.length;l++){
							levels = levelDataArr[l].split('~');
							//Following line is commented for creating Term Level Checkbox on 08-06-2010 By Abhilash
							//document.getElementById(levelCombo).options[document.getElementById(levelCombo).options.length] = new Option(levels[1],levels[0]);
							//Following line is added for creating Term Level Checkbox on 08-06-2010 By Abhilash
							//levelCheckbox += "<input name='scTermSel[]' id='scTermSel_"+levels[0]+"' type='checkbox' value='"+levels[0]+"' onclick='checkSCApprovedTermSelection(this);' />&nbsp;"+levels[1]+"&nbsp;&nbsp;";
							levelCheckbox += "<input name='"+levelCombo+"[]' id='"+levelCombo+"_"+levels[0]+"' type='checkbox' value='"+levels[0]+"' />&nbsp;"+levels[1]+"&nbsp;&nbsp;";
						}
						//Following line is added for creating Term Level Checkbox on 08-06-2010 By Abhilash
						document.getElementById(levelComboTR).style.display = "";
						document.getElementById(levelCombo).innerHTML 		= levelCheckbox ;
					}
				}  // if(levelCombo 
			} // if(shortCourseCombo.value == shortCourseData[c][0]){
    	} // end of for loop
	}
}
// ----- EOF shortCourseComboChangeEvent ----------


function checkSelectedShortCourse(scId){
	if(scId == "shortCourseCombo1"){
		if(document.getElementById(scId).value != "" || document.getElementById(scId).value != 0){
			if($("#"+scId).val() == $("#shortCourseCombo2").val() || $("#"+scId).val() == $("#shortCourseCombo3").val()){
				alert("Please Select Another Short Course");
				document.getElementById(scId).focus();
				document.getElementById(scId).options[0].selected = true;
				document.getElementById('levelCheckBox1').innerHTML = "";
				return false;
			}
		}else{
			alert("BLANK");
			document.getElementById("scstartDateTxt").value = "";	
		}
	}
	if(scId == "shortCourseCombo2"){
		if(document.getElementById(scId).value != "" || document.getElementById(scId).value != 0){
			if($("#"+scId).val() == $("#shortCourseCombo1").val() || $("#"+scId).val() == $("#shortCourseCombo3").val()){
				alert("Please Select Another Short Course");
				document.getElementById(scId).focus();
				document.getElementById(scId).options[0].selected = true;
				document.getElementById('levelCheckBox2').innerHTML = "";
				return false;
			}
		}
	}
	if(scId == "shortCourseCombo3"){
		if(document.getElementById(scId).value != "" || document.getElementById(scId).value != 0){
			if($("#"+scId).val() == $("#shortCourseCombo1").val() || $("#"+scId).val() == $("#shortCourseCombo2").val()){
				alert("Please Select Another Short Course");
				document.getElementById(scId).focus();
				document.getElementById(scId).options[0].selected = true;
				document.getElementById('levelCheckBox3').innerHTML = "";
				return false;
			}
		}
	}
}
function appSCTermLevelCBox(cbName){
//Following Part is added for checking the Term Level if Applied Course Level have the Term Level selection = Yes. on 05-06-2010 By Abhilash
	var CBoxName = cbName+"[]";
	var appSCTermSelCB = document.getElementsByName(CBoxName);
	var appSCTermSelChecked = 0;
	if(appSCTermSelCB.length > 0){
		for(i=0; i<appSCTermSelCB.length;i++){
			if(appSCTermSelCB[i].checked == true){
				appSCTermSelChecked = 1;	
			}	
		}
		if(appSCTermSelChecked == 1){
			return true;
		}else{
			alert("Select Atleast one Term Level");
			return false;
		}
	}	
}

//This function is used for checking the Short Cousre Start Date Must no conflict with the other courses while adding the New Short Course On 21-06-2010 By Abhilash
function checkStartDateAndTime(scNo){
	//var scDate1 	= convertDateFormat(document.getElementById('scstartDateTxt1').value,"YYYY-mm-dd");
	var scDays1		= document.getElementById('durationCombo1').value;
	var scTime1		= document.getElementById('timeCombo1').value;
	var scEndDate1 	= AddDaysToDate("scstartDateTxt1", scDays1);
	var scEndDate1 	= convertDateFormat(scEndDate1,"YYYY-mm-dd");
	if(scNo == 2){
		var scDate2 	= convertDateFormat(document.getElementById('scstartDateTxt2').value,"YYYY-mm-dd");
		var scDays2		= document.getElementById('durationCombo2').value;
		var scTime2		= document.getElementById('timeCombo2').value;
		var scEndDate2 	= AddDaysToDate("scstartDateTxt2", scDays2);
		//var scEndDate2 	= convertDateFormat(scEndDate2,"YYYY-mm-dd");
		if(document.getElementById('timeCombo2').value != " "){	
			var fDate2   = new Date(scEndDate1);
			var sDate2   = new Date(scDate2);
			if((scDate2 <= scEndDate1)&&(scTime2 == scTime1)){
				alert("Select Second Short Course Date Greater Than First Short Course Date");
				document.getElementById('scstartDateTxt2').value = "";
				document.getElementById('scendDateTxt2').value = "";
				document.getElementById('timeCombo2').options[0].selected = true;
				document.getElementById('scstartDateTxt2').focus();
				return false;
			}
		}
	}
	if(scNo == 3){
		var scDate2 	= convertDateFormat(document.getElementById('scstartDateTxt2').value,"YYYY-mm-dd");
		var scDays2		= document.getElementById('durationCombo2').value;
		var scTime2		= document.getElementById('timeCombo2').value;
		var scEndDate2 	= AddDaysToDate("scstartDateTxt2", scDays2);
		var scEndDate2 	= convertDateFormat(scEndDate2,"YYYY-mm-dd");
		
		var scDate3 	= convertDateFormat(document.getElementById('scstartDateTxt3').value,"YYYY-mm-dd");
		var scDays3		= document.getElementById('durationCombo3').value;
		var scTime3		= document.getElementById('timeCombo3').value;
		//var scEndDate3 	= AddDaysToDate(scDate3, scDays3);
		var scEndDate3 	= AddDaysToDate("scstartDateTxt3", scDays3);
		
		if(document.getElementById('timeCombo3').value != " "){
			var fDate3   = new Date(scEndDate2);
			var sDate3   = new Date(scDate3);
			if((scDate3 <= scEndDate1)&&(scTime1 == scTime3)){
				alert("SHORT COURSE DATE 3 MUST BE GREATER THAN SHORT COURSE 1");
				document.getElementById('scstartDateTxt3').value = "";
				document.getElementById('scendDateTxt3').value = "";
				document.getElementById('timeCombo3').options[0].selected = true;
				document.getElementById('scstartDateTxt3').focus();
				return false;
			}
			if((scDate3 <= scEndDate2)&&(scTime2 == scTime3)){
				alert("SHORT COURSE DATE 3 MUST BE GREATER THAN SHORT COURSE 2");
				document.getElementById('scstartDateTxt3').value = "";
				document.getElementById('scendDateTxt3').value = "";
				document.getElementById('timeCombo3').options[0].selected = true;
				document.getElementById('scstartDateTxt3').focus();
				return false;
			}
		}		
	}
}
//Following function is used to Calculate The Short Course End Date From the Start Date and Duartion in Weeks On 21-06-2010 By Abhilash
//function getEndDateFromStartDate(scNo,obj){
function getEndDateFromStartDate(scNo){
	var scDurationName = "durationCombo"+scNo;
	var scStartDateName 	= "scstartDateTxt"+scNo;
	if(document.getElementById(scDurationName).value == ""){
		alert("Please Select Duration.");
		document.getElementById(scDurationName).focus();
		return false;
	}else{
		if(document.getElementById(scStartDateName).value != ""){
			var days 		= document.getElementById(scDurationName).value;
			var scEndValue	= AddDaysToDate(scStartDateName, days);
			document.getElementById("scendDateTxt"+scNo).value = scEndValue;
			return true;
		}else{
			alert("Please Enter Start Date.");
			document.getElementById(scStartDateName).focus();
			return false;
		}
	}
}

//This function is used for adding the suppled days to the Start Date and will return the End Date on 22-06-2010 By Abhilash 
function AddDaysToDate(scStartDate, days){
	var conDate 	= convertDateFormat(document.getElementById(scStartDate).value,"YYYY-mm-dd");
	var myDate 		= new Date(conDate);
	var in_a_week   = myDate.setDate(myDate.getDate() + ((7 * days)-3));
	
	var myDateDate 	= (myDate.getDate()<=9) ? "0"+myDate.getDate() : myDate.getDate();
	var myDateMonth	= ((myDate.getMonth()+1)<=9) ? "0"+(myDate.getMonth()+1) : (myDate.getMonth()+1);
	var myDateYear	= myDate.getFullYear();
	
	var finalMyDate	= myDateYear+"-"+myDateMonth+"-"+myDateDate;
	//finalMyDate.getDay()
	var endDate		= convertDateFormat(finalMyDate,"dd-mm-YYYY");
	return endDate;
}

/*--------------------------Added on 12-07-2010 By Abhilash
							This function namely findShortCourseEndDate() is called from onchange event of Short Course Start Date for fetching the Short Course End Date which must be a Previous Friday of the generated Short Course End Date. In this function the table which is used in query for just genertaing the end date.
							PARAMETRS USED HERE ARE:
							scNo => this is the No for the scstartDateTxt or scendDateTxt which is used in application as well in office_use.php page.
														-----------------------------------------------------------------------------------------------------------*/
function findShortCourseEndDate(scNo){
	var scDurationName 	= "durationCombo"+scNo;
	var scStartDateName = "scstartDateTxt"+scNo;
	var scEndDateName	= "scendDateTxt"+scNo;
	var startDate 		= document.getElementById(scStartDateName).value;
	var duration 		= document.getElementById(scDurationName).value;
	
	url = "../include/php/ajax/get_course_details.php?act=getSCEndDate&startDate="+startDate+"&duration="+duration; 
	var http = zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	results = http.responseText;
	document.getElementById(scEndDateName).value = results;
}