// JavaScript Document
// ############################################################
// Created By Vinod Jawanjalkar
// Date 23-8-2010
// ###########################################################
//	**************   FOLLOWING ARE THE GLOBAL VARIABLES AND ALL ARE IMPORTANT , SO PLEASE DONT DELTE ******************************/
	var resultResponse  = "";
	var waitingImage   = "<img src='images/waitimage.gif' id='waitImage' class='ajaxLoading'>"; 
	var globalParam = "";
//	***************************  BLOCK END OF GLOBLA VARIABLES ************************************************/

function wait(){
	$("body").append(waitingImage);
}
function removeWait(){
	$('#waitImage').fadeOut(1200, function() {
		$('#waitImage').remove();
	});
}

var calendarFunction = "";

function ajaxA(requestUrl, urlData, method,callingFunction,waitImage){

	if(waitImage == "Y"){
		$("body").append(waitingImage);
	}
	$.ajax({
		//THIS IS THE PHP FILE THAT PROCESS THE DATA
		url: requestUrl,	
		
		// THIS IS METHOD OF SENT VARIABLES TO PHP FILE, DEFAULT IS GET
		type: method,
		
		// PARAMETERS SENT WITH URL
		data: urlData,		
		
		// DO NOT CACHE THE PAGE
		cache: false,
		
		// IF THE PROCESS COMPLETED AND SUCCESS
		success: function(responseHtml){
			resultResponse = responseHtml;
			eval(callingFunction);
			if(waitImage == "Y"){
				$('#waitImage').fadeOut(1200, function() {
					$('#waitImage').remove();
				});
			}
		} // END OF success 
	}); // END OF FUNCTION $.ajax
}// END OF FUNCTION ajaxRequestAsyncrs


// THIS IS AJAX ASYCHRONOUS FUNCTION, FUNCTION WILL SET THE SERVER CONNECTION FOR COMMUNICATION USING JQUERY AND RETURN THE RESULT
// PARAMETERS 
// 1) requestUrl      - THIS IS USED TO SENT THE REQUEST TO GIVEN PHP FILE
// 2) urlData         - WANT TO PASS THE VARIABLES FOR PROCESS
// 3) method          - VAR USE FOR FORM METHOD LIKE GET OR POST
// 4) callingFunction - AFTER SUCCESS OF PROCESS THIS FUNCTION WILL CALL TO SET THE RESULT
function ajaxS(requestUrl, urlData, method){
	$.ajax({
		//THIS WILL SENT THE ASYCHRONOUS REQUEST TO PROCESS THE DATA
		async: false,
		
		//THIS IS THE PHP FILE THAT PROCESS THE DATA
		url: requestUrl,	
		
		// THIS IS METHOD OF SENT VARIABLES TO PHP FILE, DEFAULT IS GET
		type: method,
		
		// PARAMETERS SENT WITH URL
		data: urlData,		
		
		// DO NOT CACHE THE PAGE
		cache: false,
		
		// IF THE PROCESS COMPLETED AND SUCCESS
		success: function(responseHtml){	
			resultResponse = responseHtml;
		} // END OF success 
	}); // END OF FUNCTION $.ajax
}// END OF FUNCTION ajaxRequestSynchronous




// 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..
	}
}
// END OF FUNCTION onKeyPressed



function OpenFileInFrame(url,iFrameId,hideDivId){
	var frameObj = document.getElementById(iFrameId);
	document.getElementById(hideDivId).style.display = 'none';
	frameObj.src = url;
	frameObj.scrolling = 'auto';
	frameObj.style.display = '';
	if(document.getElementById('giFrameTable')){
		document.getElementById('giFrameTable').style.display = '';
	}
	var subH = 15;
	//Main Frame Height. Reducing 10 to avoid multiple scrollbars.
	if (document.getElementById('subheadingSpan')){
		subH = parseInt(subH)+parseInt($("#subheadingSpan").height());
	}
	var mfh	= window.parent.document.getElementById('contentFrame').clientHeight - subH;
	document.getElementById(iFrameId).style.height = mfh+'px';
}


function closeFileInFrame(iFrameId,hideDivId){
	var frameObj = document.getElementById(iFrameId);
	frameObj.src = "";
	$("#"+hideDivId).show();
	$("#"+iFrameId).hide();
}

var DATESEPARATOR	= "-";
var DATEFORMAT 		= 'dd-mm-yyyy';
var splitArr     = "";
var txtBoxId     = "";
$(document).ready(function(){ 
  
   $("img").each(function(){
		if($(this).attr("src") == "images/jscalendar.gif"){
			calendarFunction = "" + $(this).attr("onclick") + "";
			calendarFunction = calendarFunction.split('.');
			for(i=0;i<calendarFunction.length;i++){
				if(i==2){
					splitArr   = calendarFunction[i].split(',');
					txtBoxId   = splitArr[0];
					dateFormat = splitArr[1];
					$("#"+txtBoxId).unbind();
					$("#"+txtBoxId).bind('change',function(){
						if(calenderValidator($(this).val(), DATESEPARATOR, DATEFORMAT, "")==false){
							$(this).val('');
							$(this).focus();
						}
					});
				}
			}
		}
	});
});

// Following Function Validates The Entered Date
/*
CREATED BY
	Vishal Chikhalikar
PARAMETERS:
	1. inputDate 		= Date to be validate
	2. dateSeperator 	= '-' or '/'  e.g. 12-12-2010 or 12/12/2010
	3. dateFormat		= e.g. dd-mm-yyyy, mm-dd-yyyy, yyyy-dd-mm  ... etc.
	4. FuturePastValidation = P - Past Date, F- Future Date, A- All Date [All type includes current date]
	5. DATESEPARATOR & DATEFORMAT  both are constants defined in config.js
*/

function calenderValidator(inputDate, dateSeparator, dateFormat, FuturePast){
	if(!inputDate){
		return true;	
	}
	// SET the default value
	var separator 	= (dateSeparator=="") ? DATESEPARATOR : dateSeparator;
	var dateFormat 	= (dateFormat=="") ? DATEFORMAT : dateFormat;
	var FuturePastValidation = (FuturePast=="") ? 'A' : FuturePast;

	
	// SET the default value complete
	var dateArr = inputDate.split(separator);
	// Checking Valid date Separator
	if(dateArr.length!=3){
		alert('Invalid Date: Please use ('+dateFormat+') format');
		return false;
	}
	
	// Checking Valid Date Format
	var dateFormatArr = dateFormat.split(separator);

	for(var i=0; i < dateFormatArr.length; i++){
		if(dateFormatArr[i] == "dd" || dateFormatArr[i] == "DD" || dateFormatArr[i] == 'dd' || dateFormatArr[i] == 'DD'){
			var day = dateArr[i];
		}
		if(dateFormatArr[i] == "mm" || dateFormatArr[i] == "MM" || dateFormatArr[i] == 'mm' || dateFormatArr[i] == 'MM'){
			var month = dateArr[i];
		}
		if(dateFormatArr[i] =="yyyy"|| dateFormatArr[i] == "YYYY" || dateFormatArr[i] =='yyyy'|| dateFormatArr[i] == 'YYYY'){
			var year = dateArr[i];
		}
	}
	
	// Checking Digit of Day Month and year
	if(day < 1 || day > 31){
		alert('Invalid Date:  Date should between 01 - 31');
		return false;
	}
	if(month < 1 || month > 12){
		alert('Invalid Date:  Month should between 1 - 12');
		return false;
	}
	
	if(day.length != 2 || month.length != 2){
		alert('Invalid Date: Date and Month should be 2 digit value');
		return false;
	}
	
	if(year.length != 4 || year.length != 4){
		alert('Invalid Date: Year should be 4 digit value');
		return false;
	}
	
	var today 		= new Date();
	var passingDate = new Date(year, month-1, day);

	var ntoday		 = Date.parse(today);
	var npassingDate = Date.parse(passingDate);
	
	// Checking Future and past validation
	if(FuturePastValidation == 'P' || FuturePastValidation == "P"){
		if(npassingDate > ntoday){
			alert("Invalid Date: Date should be less than or equal to today!");
			return false;
		}
	}
	
	if(FuturePastValidation == 'F' || FuturePastValidation == "F"){
		if( ntoday > npassingDate ){
			alert("Invalid Date: Date should be greater than or equal to today!");
			return false;
		}
	}
	
	return true;
}

function getSelectedStudent(radioGroupName)
{
	var selStudId	= 0;
	var rdObjArr	= document.getElementsByName(radioGroupName+'[]');
	for(i=0;i<rdObjArr.length;i++){
		if(rdObjArr[i].checked == true){
			selStudId	= rdObjArr[i].value;
			break;
		}
	}
	return selStudId;
}

function unCheckAllChechBox(val,id){
	if(val != ""){
		$("input[name='"+id+"']:eq(0)").attr("checked",false);
	}
	else{
		$("input[name='"+id+"']:gt(0)").each(function(){
			$(this).attr("checked",false);
		})
	}
}
function getAlertBox(message){
	$("#divAlert").remove();
	var str = "<div id='divAlert' style='display:none' title='Sahajeevan Alert Message'></div>";
	$('body').append(str);
	$("#divAlert").html(message);
	$("#divAlert").dialog({
			bgiframe: true,
			autoOpen : false,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					$("#divAlert").html("");
					$("#divAlert").remove();
				}
			}
	});
	$('#divAlert').dialog('open');
}

function getMyProfile(profileId){
	if(profileId != undefined){
		var prfId = profileId;	
	}
	wait();
	$.get('modules/previewProfile/profile.php?profileId='+prfId,function(data){
		$('#resultDiv').html(data);
		removeWait();
	})	
}
function execFunction(){
	if(globalFunction != ""){
		eval(globalFunction+"('"+globalParam+"')");
	}
}
function getState(selfId,toId,css,stateName,cityTrue,cityName,cityDiv,validationTrue){
	if($("#"+selfId).val() != ""){
		var countryId = $("#"+selfId).val();
		$.get('include/common/commonAS.php?action=getState&countryId='+countryId+"&css="+css+"&stateName="+stateName+"&cityName="+cityName+"&cityTrue="+cityTrue+"&validationTrue="+validationTrue+"&cityDiv="+cityDiv,function(data){
			$("#"+toId).html(data);
		});
	}
	else{
		$("#"+stateName+" option:gt(0)").remove();
	}
}

function getCity(selfId,toId,css,cityName,validationTrue){
	if($("#"+selfId).val() != ""){
		var stateId = $("#"+selfId).val();
		$.get('include/common/commonAS.php?action=getCity&stateId='+stateId+"&css="+css+"&cityName="+cityName+"&validationTrue="+validationTrue,function(data){
			$("#"+toId).html(data);
		});
	}
	else{
		$("#"+cityName+" option:gt(0)").remove();
	}
}
