// JavaScript Document
/************************************************************************************************************
	@Author - Kamal Dayani (PHP Programmer)(kamaldayani@gmail.com)
	@Date   - 10 Aug, 2009
	@Verson - 1.0

	Before every function, find one comment line will give you information about , the functions.
	Legent -> 	EOF - END OF FUNCTION
	************************************************************************************************************/

//	**************   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 ************************************************/



// Function will compare two dates and return true if first date is greter than second date.
function compareTwoDates(firstDate,SecondDate){ 
	var fDateArr = firstDate.split("-");
	var fDate    = fDateArr[1] + "-" + fDateArr[0] + "-" + fDateArr[2]; 
	var sDateArr = SecondDate.split("-");
	var sDate    = sDateArr[1] + "-" + sDateArr[0] + "-" + sDateArr[2];
	var fDate    = new Date(fDate);
	var sDate    = new Date(sDate);
	newFDate = Date.parse(fDate);
	newSDate = Date.parse(sDate);
	if(newFDate > newSDate){
		return true; 
	} 
}
// EOF compareTwoDates

// Function will find the Enter key or Mouse click event occured and called the given passed function.
// e is the event and callingFunction is the javascript function name eg. validateForm()
function onKeyPressed(e,callingFunction){ 
	var eventOccured = (!e) ? window.event : e;
	if(eventOccured.keyCode == 13 || eventOccured.type == "click"){ //  13 is keycode of Enter Key Pressed 
		eval(callingFunction);    // function wants to execute..
	}
}

// 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 creates UrlString.
// PARAMETER -> objectIdsArr - FIELD ID ARRAY
function urlCreator(objectIdsArr){
	urlString = "";
	for(i=0;i<objectIdsArr.length;i++){
		urlString += objectIdsArr[i]+"="+document.getElementById(objectIdsArr[i]).value+"&";
	}
	return urlString.substr(0,urlString.length-1);
}
// EOF urlCreator

// 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

// Finction Check and Uncheck the numbers of Check Box.
// Required - 1) Check box Id
//            2) Id for change the content of Inner HTML.
function checkUncheckCheckBoxes(checkBoxId,innerHTMLId){ 
	var action = ""; 
	if(document.getElementById(innerHTMLId).innerHTML=="Check All"){ 
		action="Check";
		document.getElementById(innerHTMLId).innerHTML="UnCheck All";
	}else{
		action="UnCheck";
		document.getElementById(innerHTMLId).innerHTML="Check All";
	}
	var checkBoxLen=document.getElementsByName(checkBoxId).length;
	for(var i=0;i < checkBoxLen;i++){
		if (action=="Check"){
			document.getElementsByName(checkBoxId)[i].checked=true;
		}
		else{
			document.getElementsByName(checkBoxId)[i].checked=false;
		}
	}	
}
// EOF checkUncheckCheckBoxes

// 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 WILL CHECK THE INPUT FIELDS ARE NOT BLANK.
// PARAMETERS -> 1) fieldNameIdArr - INPUT FIELDS ID. 
//				 2) alertMsgArr    - WHAT ALERT MSG YOU WANT TO PRINT. 
function validateInputFields(fieldNameIdArr,alertMsgArr){
	var isBlank; 
	var message = "";
	for(k=0;k<fieldNameIdArr.length;k++){
		isBlank = document.getElementById(fieldNameIdArr[k]).value;
		isBlank = isBlank.replace(/^\s+$/,'');
		if(isBlank == null || isBlank == 0){
			message = alertMsgArr[k];
			alert("Please "+ message);
			document.getElementById(fieldNameIdArr[k]).value = "";
			document.getElementById(fieldNameIdArr[k]).focus();
			return false;
		}
	}
	return true;
}
// EOF validateInputFields


//FUNCTION VALIDATES THE EMAIL FIELD 
// PARAMETER -> fieldId - ID OF INPUT FIELD 
function validateEmailField(fieldId){
	var email = document.getElementById(fieldId);
	fieldId   = email.value;
	if((fieldId.indexOf(".") > 2) && (fieldId.indexOf("@") > 0)){}
	else{
		alert("Please Check Email Address.");
		email.focus();
		return false;
	}
	return true;
}
// EOF validateEmailField

function checkBlankSpace(thisObject){
	var isBlank; 
	var message = "";
	isBlank = thisObject.value;
	isBlank = isBlank.replace(/^\s+$/,'');
	if(isBlank == ""){
		thisObject.value = "";
	}
}

function checkNumericValue(fieldId){
	if(isNaN(document.getElementById(fieldId).value)){
		alert("Please Enter Numeric Value");
		document.getElementById(fieldId).value = "";
		document.getElementById(fieldId).focus();
		return false;
	}else{
		return true;
	}
}

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

// 	FUNCTION WILL ALLOW THE FILE TYPE AND SPECIFIED SIZE OF THE IMAGE/ FILE CALLED FROM ONCHANGE EVENT OF FILE BROWSE BUTTON
function getImageSizeValidation(pathField){
	var imglength = 0;
	var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i;
	var path = document.getElementById(pathField).value;
	if(path.search(imgRe) != -1){ 
		var img = new Image();
		img.src = path;
		for(i=0; i < 2000000; i++){
			//
		}
		imglength = img.fileSize;
		if(imglength > 80000){
			alert("Selected Image to large, Please select small Image");
			document.getElementById(pathField).focus();
			return false;
		}
	}else{ 
		alert("JPG, PNG, and GIFs only!");
	} 
}


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


function updateStateCombo(Obj,inDivId,fo,cmbName,selValue)
{	
	var countryId	= Obj.value;
	var cmbObj	= document.getElementById(cmbName);

	cmbObj.options.length	= 0;
	cmbObj.options[0]		= new Option("Please Wait...","");
	
	action	= "getStateRec";
	url		= "include/php/ajax/commonAS.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]);
			}
		}
	}
	
}

function updateCityCombo(Obj,inDivId,fo,cmbName,selValue)
{	
	var stateId	= Obj.value;
	var cmbObj	= document.getElementById(cmbName);

	cmbObj.options.length	= 0;
	cmbObj.options[0]		= new Option("Please Wait...","");
	
	action	= "getCityRec";
	url		= "include/php/ajax/commonAS.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]);
			}
		}
	}
	
}


function getTermCombo(Obj,inDivId,fo,cmbName)
{
	/*if(Obj.name	== 'cmbcourse'){
		var inDivId	= 'divtermCmb';
		var fo		= 'S';
		var cmbName	= "cmbTerm";
	}
	else{
		var inDivId	= 'divtermsCmb';
		var fo		= 'A';
		var cmbName	= "cmbsTerm";
	}*/
	var courseId	= Obj.value;
	document.getElementById(inDivId).innerHTML	= "Wait...";
	action	= "getTerm";
	displayWaitImageInCenter();
	url		= "include/php/ajax/subjectMasterAS.php?action="+action+"&courseId="+courseId+"&fo="+fo+"&cmbName="+cmbName;
	hideWaitImageInCenter();
	ajaxSyncronouseRequest(url,inDivId);
}

// This function will be used to get session and term combo's
function getsessNTerm(obj,subFO,callSubFn,studFO,callStudFn)
{
	
	var courseId	= obj.value;
	displayWaitImageInCenter();
	var url 		= "include/php/ajax/commonAS.php?action=getsessNTerm&courseId="+courseId+"&subfo="+subFO+"&callSubFn="+callSubFn+"&studfo="+studFO+"&callStudFn="+callStudFn;
	var http 		= zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	hideWaitImageInCenter();
	results 	= http.responseText;
	resultsa 	= results.split("~");
	document.getElementById('term').innerHTML			= resultsa[0];
	document.getElementById('spanSession').innerHTML	= resultsa[1];
}

//Obj - is term combox box object. subfo - This parameter is used for subject first option.
function getsubject(obj,subFO)
{
	if (obj.value == ""){
		document.getElementById('ssubjectCmb').options.length	= 1;
	}
	if (document.getElementById('ssessionCmb').value == ""){
		alert("select session first");
		document.getElementById('ssessionCmb').focus();
		obj.value	= "";
		return false;
	}
	
	var term		= obj.value;
	var courseId	= document.getElementById('scourseCmb').value;
	displayWaitImageInCenter();
	var url 		= "include/php/ajax/commonAS.php?action=getsubjectCmb&courseId="+courseId+"&term="+term+"&subfo="+subFO;
	var http 		= zXmlHttp.createRequest();
	http.open("GET", url, false);
	http.send(null);
	hideWaitImageInCenter();
	results 	= http.responseText;
	document.getElementById('spansubject').innerHTML	= results;
}

function getStudentCmb(studFO)
{
	if (document.getElementById('stermCmb').value == ""){
		document.getElementById('sstudentCmb').options.length	= 1;	
	}
	
	if ((document.getElementById('stermCmb').value != "") && (document.getElementById('ssessionCmb').value != "")){
		var courseId	= document.getElementById('scourseCmb').value;
		var sessionId	= document.getElementById('ssessionCmb').value;
		var term		= document.getElementById('stermCmb').value;
		//Branch Id
		if(document.getElementById('cmbsBranch') != null)
	 	var branchId       = document.getElementById('cmbsBranch').value;
		else
	 	var branchId       = "";
		
		displayWaitImageInCenter();
		var url 		= "include/php/ajax/commonAS.php?action=getstudentCmb&courseId="+courseId+"&term="+term+"&sessionId="+sessionId+"&studfo="+studFO+"&branchId="+branchId;
		var http 		= zXmlHttp.createRequest();
		http.open("GET", url, false);
		http.send(null);
		hideWaitImageInCenter();
		results 	= http.responseText;
		document.getElementById('spanstudent').innerHTML = results;
	}

}

// FUNCTION ADDED BY SUNIL DATE OCTOBER 20, 2009
// FUNCTION WILL CALLED FROM COUNTRY COMBO BOX, TO CHANGE THE CONTAIN OF STATE COMBO BOX.
// FOR CREATION OF COUNTRY COMBO BOX USE functions.php FILE, AND CALL THE FUNCTION commonCounrtyCombo.
function changeStateCombo(){
	var countryCombo = document.getElementById("countryComboNameHdn").value;
	var stateCombo   = document.getElementById("stateComboNameHdn").value;
	var cityCombo    = document.getElementById("cityComboNameHdn").value;
	document.getElementById(cityCombo).options.length=0;
	document.getElementById(stateCombo).options.length=0;
	document.getElementById(stateCombo).options[0] = new Option("- - - Select  State - - -","");
	document.getElementById(cityCombo).options[0] = new Option("- - - Select  City - - -","");
	stateData.sort();
	for(var c=0; c<stateData.length; c++){
		if(document.getElementById(countryCombo).value == ""){
			document.getElementById(stateCombo).options[document.getElementById(stateCombo).options.length] = new Option(stateData[c][2], stateData[c][0]);
		}else{
			if(document.getElementById(countryCombo).value == stateData[c][1]){
				document.getElementById(stateCombo).options[document.getElementById(stateCombo).options.length] = new Option(stateData[c][2], stateData[c][0]);
			}
		}
    }
}
// ----- EOF changeStateCombo ----------


// FUNCTION ADDED BY SUNIL DATE OCTOBER 20, 2009
// FUNCTION WILL CALLED FROM STATE COMBO BOX, TO CHANGE THE CONTAIN OF CITY COMBO BOX.
// FOR CREATION OF STATE COMBO BOX USE functions.php FILE, AND CALL THE FUNCTION commonStateCombo.
function changeCityCombo(){
	var stateCombo = document.getElementById("stateComboNameHdn").value;
	var cityCombo  = document.getElementById("cityComboNameHdn").value;
	document.getElementById(cityCombo).options.length=0;
	document.getElementById(cityCombo).options[0] = new Option("- - - Select  City - - -","");
	cityData.sort()
	for(var c=0; c<cityData.length; c++){
		if(document.getElementById(stateCombo).value == ""){
			document.getElementById(cityCombo).options[document.getElementById(cityCombo).options.length] = new Option(cityData[c][2], cityData[c][0]);
		}else{
			if(document.getElementById(stateCombo).value == cityData[c][1]){
				document.getElementById(cityCombo).options[document.getElementById(cityCombo).options.length] = new Option(cityData[c][2], cityData[c][0]);
			}
		}
    }
}
// ----- EOF changeCityCombo ----------

