// JavaScript Document

 function trim(s){
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function fnError(frm, ctrl, msg, val){
	if(trim(document[eval("frm")][eval("ctrl")].value)==val){
		alert(msg);
		document[eval("frm")][eval("ctrl")].focus();
		return false;
	}
	else return true;
}
function Check_DOB(chkdate, reqd){ //alert(yr + " " + mnth + " " + day);
	//---check the format of date i.e mm-dd-yyyy or mm-dd-yyyy
	if(trim(chkdate) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate) == "" && reqd == false) return false; //---if date is compulsory---
	regexp = /^([0-9]{2,2}[\-|\/]){2,2}[0-9]{4,4}$/;
	if(trim(chkdate) != "" && !regexp.test(chkdate)) return false;
	else{ //---IF SELECTED DATE IS GREATER THAN OR EQUAL TO TODAY'S DATE---	
		mnth = Number(chkdate.substr(0, 2));
		day = Number(chkdate.substr(3, 2));
		yr = Number(chkdate.substr(6, 4));
		//--CHECK IF ENTERED DATE IS VALID----
		if((mnth == 2) && (yr % 4 == 0) && (day > 29)) return false; //---if leap year and feb day > 29---
		if((mnth == 2) && (yr % 4 > 0) && (day > 28)) return false; //---if not leap year and feb day > 28---
		//---if max days are 31---
		if(((mnth == 1) || (mnth == 3) || (mnth == 5) || (mnth == 7) || (mnth == 8) || (mnth == 10) || (mnth == 12)) && day > 31)
			return false;
		//---if max days are 30---
		if(((mnth == 4) || (mnth == 6) || (mnth == 9) || (mnth == 11)) && day > 30) return false;
		//---
		d=new Date();
		cur_yr=d.getFullYear();       //getting current year  
		cur_mnth=d.getMonth()+1;  //getting current year
		cur_day=Number(d.getDate());   //getting current year 
		if (yr > cur_yr) return false;       //comparing year
		else if (yr==cur_yr && mnth > cur_mnth) return false;  //comparing month
		else if (yr==cur_yr && mnth==cur_mnth && day > cur_day) return false;  //comparing day
	}
	return true;
}

function Check_Date(chkdate, reqd){ //alert(yr + " " + mnth + " " + day);
	//---check the format of date i.e mm-dd-yyyy or mm-dd-yyyy
	if(trim(chkdate) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate) == "" && reqd == true) return false; //---if date is compulsory---
	regexp = /^([0-9]{2,2}[\-|\/]){2,2}[0-9]{4,4}$/;
	if(trim(chkdate) != "" && !regexp.test(chkdate)) return false;
	else{ //---IF SELECTED DATE IS LESS THAN TODAY'S DATE---	
		mnth = Number(chkdate.substr(0, 2));
		day = Number(chkdate.substr(3, 2));
		yr = Number(chkdate.substr(6, 4));
		//--CHECK IF ENTERED DATE IS VALID----
		if((mnth == 2) && (yr % 4 == 0) && (day > 29)) return false; //---if leap year and feb day > 29---
		if((mnth == 2) && (yr % 4 > 0) && (day > 28)) return false; //---if not leap year and feb day > 28---
		//---if max days are 31---
		if(((mnth == 1) || (mnth == 3) || (mnth == 5) || (mnth == 7) || (mnth == 8) || (mnth == 10) || (mnth == 12)) && day > 31)
			return false;
		//---if max days are 30---
		if(((mnth == 4) || (mnth == 6) || (mnth == 9) || (mnth == 11)) && day > 30) return false;
		//---
		d=new Date();
		cur_yr=d.getFullYear();       //getting current year  
		cur_mnth=d.getMonth()+1;  //getting current year
		cur_day=Number(d.getDate());      //getting current year 
		if (yr < cur_yr) return false;        //comparing year
		else if (yr==cur_yr && mnth < cur_mnth)  return false; //comparing month
		else if (yr==cur_yr && mnth==cur_mnth && day < cur_day)  return false; //comparing day
	}
	return true;
}

function Compare_Dates(chkdate1, chkdate2, reqd){ //alert(yr + " " + mnth + " " + day);
	//---check the format of date i.e mm-dd-yyyy or mm-dd-yyyy
	if(trim(chkdate1) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate1) == "" && reqd == true) return false; //---if date is compulsory---
	if(trim(chkdate2) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate2) == "" && reqd == true) return false; //---if date is compulsory---
	regexp = /^([0-9]{2,2}[\-|\/]){2,2}[0-9]{4,4}$/;
	if(trim(chkdate1) != "" && !regexp.test(chkdate1)) return false;
	if(trim(chkdate2) != "" && !regexp.test(chkdate2)) return false;
	else{ //---IF SELECTED DATE IS LESS THAN TODAY'S DATE---	
		mnth1 = Number(chkdate1.substr(0, 2));
		day1 = Number(chkdate1.substr(3, 2));
		yr1 = Number(chkdate1.substr(6, 4));
		mnth2 = Number(chkdate2.substr(0, 2));
		day2 = Number(chkdate2.substr(3, 2));
		yr2 = Number(chkdate2.substr(6, 4)); 
		//--CHECK IF ENTERED DATE IS VALID----
		if((mnth1 == 2) && (yr1 % 4 == 0) && (day1 > 29)) return false; //---if leap year and feb day > 29---
		if((mnth1 == 2) && (yr1 % 4 > 0) && (day1 > 28)) return false; //---if not leap year and feb day > 28---
		//---if max days are 31---
		if(((mnth1 == 1) || (mnth1 == 3) || (mnth1 == 5) || (mnth1 == 7) || (mnth1 == 8) || (mnth1 == 10) || (mnth1 == 12)) && day1 > 31)
			return false;
		//---if max days are 30---
		if(((mnth1 == 4) || (mnth1 == 6) || (mnth1 == 9) || (mnth1 == 11)) && day1 > 30) return false;
		//---
		//--CHECK IF ENTERED DATE IS VALID----
		if((mnth2 == 2) && (yr2 % 4 == 0) && (day2 > 29)) return false; //---if leap year and feb day > 29---
		if((mnth2 == 2) && (yr2 % 4 > 0) && (day2 > 28)) return false; //---if not leap year and feb day > 28---
		//---if max days are 31---
		if(((mnth2 == 1) || (mnth2 == 3) || (mnth2 == 5) || (mnth2 == 7) || (mnth2 == 8) || (mnth2 == 10) || (mnth2 == 12)) && day2 > 31)
			return false;
		//---if max days are 30---
		if(((mnth2 == 4) || (mnth2 == 6) || (mnth2 == 9) || (mnth2 == 11)) && day2 > 30) return false;
		//---
		if (yr2 < yr1) return false;        //comparing year
		else if (yr2 == yr1 && mnth2 < mnth1)  return false; //comparing month
		else if (yr2 == yr1 && mnth2 == mnth1 && day2 < day1)  return false; //comparing day
	}
	return true;
}

function Greater_Date(chkdate1, chkdate2, reqd){ //alert(yr + " " + mnth + " " + day);
	//---check the format of date i.e mm-dd-yyyy or mm-dd-yyyy
	if(trim(chkdate1) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate1) == "" && reqd == true) return false; //---if date is compulsory---
	if(trim(chkdate2) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate2) == "" && reqd == true) return false; //---if date is compulsory---
	regexp = /^([0-9]{2,2}[\-|\/]){2,2}[0-9]{4,4}$/;
	if(trim(chkdate1) != "" && !regexp.test(chkdate1)) return false;
	if(trim(chkdate2) != "" && !regexp.test(chkdate2)) return false;
	else{ //---IF SELECTED DATE IS LESS THAN TODAY'S DATE---	
		mnth1 = Number(chkdate1.substr(0, 2));
		day1 = Number(chkdate1.substr(3, 2));
		yr1 = Number(chkdate1.substr(6, 4));
		mnth2 = Number(chkdate2.substr(0, 2));
		day2 = Number(chkdate2.substr(3, 2));
		yr2 = Number(chkdate2.substr(6, 4));
		//--CHECK IF ENTERED DATE IS VALID----
		if((mnth1 == 2) && (yr1 % 4 == 0) && (day1 > 29)) return false; //---if leap year and feb day > 29---
		if((mnth1 == 2) && (yr1 % 4 > 0) && (day1 > 28)) return false; //---if not leap year and feb day > 28---
		//---if max days are 31---
		if(((mnth1 == 1) || (mnth1 == 3) || (mnth1 == 5) || (mnth1 == 7) || (mnth1 == 8) || (mnth1 == 10) || (mnth1 == 12)) && day1 > 31)
			return false;
		//---if max days are 30---
		if(((mnth1 == 4) || (mnth1 == 6) || (mnth1 == 9) || (mnth1 == 11)) && day1 > 30) return false;
		//---
		//--CHECK IF ENTERED DATE IS VALID----
		if((mnth2 == 2) && (yr2 % 4 == 0) && (day2 > 29)) return false; //---if leap year and feb day > 29---
		if((mnth2 == 2) && (yr2 % 4 > 0) && (day2 > 28)) return false; //---if not leap year and feb day > 28---
		//---if max days are 31---
		if(((mnth2 == 1) || (mnth2 == 3) || (mnth2 == 5) || (mnth2 == 7) || (mnth2 == 8) || (mnth2 == 10) || (mnth2 == 12)) && day2 > 31)
			return false;
		//---if max days are 30---
		if(((mnth2 == 4) || (mnth2 == 6) || (mnth2 == 9) || (mnth2 == 11)) && day2 > 30) return false;
		//---
		if (yr2 < yr1) return false;        //comparing year
		else if (yr2 == yr1 && mnth2 < mnth1)  return false; //comparing month
		else if (yr2 == yr1 && mnth2 == mnth1 && day2 <= day1)  return false; //comparing day
	}
	return true;
}

function fnDays(frm, lstyear, lstmonth, lstday)
{

	year=document[eval("frm")][eval("lstyear")].value;
	month=Number(document[eval("frm")][eval("lstmonth")].value);
	
	document[eval("frm")][eval("lstday")].length=0;
	document[eval("frm")][eval("lstday")].options[0]=new Option("DD", "0");
	
	//---INSERT 1 - 31 IN LSTDAYS--------
	if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
	{
		for(i=1; i<=31; i++)
		{
			dd=i;
			if(dd<10)
				dd="0" + dd;
			document[eval("frm")][eval("lstday")].options[i]=new Option(dd, dd);
		}
	}
	//-----------
	//---INSERT 1 - 30 IN LSTDAYS--------
	if(month==4 || month==6 || month==9 || month==11)
	{
		for(i=1; i<=30; i++)
		{
			dd=i;
			if(dd<10)
				dd="0" + dd;
			document[eval("frm")][eval("lstday")].options[i]=new Option(dd, dd);
		}
	}
	//-----------
	//---INSERT 1 - 30 IN LSTDAYS--------
	if(month==2)
	{
		if(year%4==0 || year%400==0)//-------LEAP YEAR-----
		{
			for(i=1; i<=29; i++)
			{
				dd=i;
				if(dd<10)
					dd="0" + dd;
				document[eval("frm")][eval("lstday")].options[i]=new Option(dd, dd);
			}
		}
		else
		{
			for(i=1; i<=28; i++)
			{
				dd=i;
				if(dd<10)
					dd="0" + dd;
				document[eval("frm")][eval("lstday")].options[i]=new Option(dd, dd);
			}
		}
	}
	//-----------
	
}

function fnYear(frm, lstyear, lstmonth, lstday)
{
	mnth=document[eval("frm")][eval("lstmonth")].value;
	day=document[eval("frm")][eval("lstday")].value;
	yr=document[eval("frm")][eval("lstyear")].value;
	
		if(yr=="-1")
		{
			alert("Please select the Year first.");
			document[eval("frm")][eval("lstyear")].focus();
			return false;
		}	
	
}
function fnInitialize(frm, lstyear, lstmonth, lstday)
{
	
	//-------INSERT VALUES IN LISTS-----
	dat = new Date();
	year=dat.getFullYear();
	var i;
	//-----INSERT VALUES OF YEAR IN LSTYEAR----
	j=1;
	//alert(year);
	for(i=year; i >= 1900; i--){
		document[eval("frm")][eval("lstyear")].options[j]=new Option(i, i);
		j++;
	}
	//---------------
	
	//------INSERT VALUES OF MONTH IN LSTMONTH------
	
	document[eval("frm")][eval("lstmonth")].options[1]=new Option("Jan", "01");
	document[eval("frm")][eval("lstmonth")].options[2]=new Option("Feb", "02");
	document[eval("frm")][eval("lstmonth")].options[3]=new Option("Mar", "03");
	document[eval("frm")][eval("lstmonth")].options[4]=new Option("Apr", "04");
	document[eval("frm")][eval("lstmonth")].options[5]=new Option("May", "05");
	document[eval("frm")][eval("lstmonth")].options[6]=new Option("Jun", "06");
	document[eval("frm")][eval("lstmonth")].options[7]=new Option("Jul", "07");
	document[eval("frm")][eval("lstmonth")].options[8]=new Option("Aug", "08");
	document[eval("frm")][eval("lstmonth")].options[9]=new Option("Sep", "09");
	document[eval("frm")][eval("lstmonth")].options[10]=new Option("Oct", "10");
	document[eval("frm")][eval("lstmonth")].options[11]=new Option("Nov", "11");
	document[eval("frm")][eval("lstmonth")].options[12]=new Option("Dec", "12");
	//------------
}

function fnImage(frm, Img)
{
   if(trim(document[eval("frm")][eval("Img")].value)!="")
	{
	  x = document[eval("frm")][eval("Img")].value.toLowerCase();
	  if(x.indexOf(".jpg")==-1 && x.indexOf(".gif")==-1 && x.indexOf(".png")==-1 && x.indexOf(".swf")==-1)
	  {
	    alert("Please select valid Image.");
		document[eval("frm")][eval("Img")].focus();
		return false;
	  } 
	}
	
	return true;
}

function fnTime(obj_hr, obj_min, obj_am, sel_time){
	var i, hr, minute, sel_hr = '', sel_min = '', sel_am = '';
	if(trim(sel_time) != "") { sel_hr = sel_time.split(":")[0]; sel_min = sel_time.split(":")[1]; sel_am = sel_time.split(":")[2]; }
	obj_hr.options[0]=new Option('00', '12');
	for(i=1; i<=12; i++){ if(i<10) hr="0" + i; else hr=i; obj_hr.options[i]=new Option(hr, hr); }
	for(i=1; i<=12; i++){ if(i<10) hr="0" + i; else hr=i; if(hr == sel_hr) obj_hr.options[i].selected=true; }
	//obj_min.options[0]=new Option('mm', '');
	for(i=0; i<=59; i++){ if(i<10) minute = "0" + i; else minute = i; obj_min.options[i]=new Option(minute, minute); }
	for(i=0; i<=59; i++){ if(i<10) minute = "0" + i; else minute = i; if(minute == sel_min) obj_min.options[i].selected=true; }
	obj_am.options[0]=new Option("AM", "AM");
	if(sel_am == "AM") obj_am.options[0].selected=true;
	obj_am.options[1]=new Option("PM", "PM");
	if(sel_am == "PM") obj_am.options[1].selected=true;
}

function Check_Date_Valid(chkdate, reqd){ //alert(yr + " " + mnth + " " + day);
	//---check the format of date i.e mm-dd-yyyy or mm-dd-yyyy
	
	if(trim(chkdate) == "" && reqd == false) return true; //---if date is optional then shud b true else it must b false---
	else if(trim(chkdate) == "" && reqd == true) return false; //---if date is compulsory---
	regexp = /^([0-9]{2,2}[\-|\/]){2,2}[0-9]{4,4}$/;
	if(trim(chkdate) != "" && !regexp.test(chkdate)) return false;
	return true;
}

function Check_Time(obj_hr, obj_min, obj_am, reqd){
	var hr = trim(obj_hr.value); var minute = trim(obj_min.value); var am = trim(obj_am.value);
	if(reqd == false && hr == "" && minute == ""){//--IF TIME IS BLANK AND REQD IS OPTIONAL---
		Notify_Success(obj_hr); Notify_Success(obj_min); Notify_Success(obj_am); return true;
	}
	else if(reqd == true && hr == "" && minute == ""){//--IF TIME IS BLANK AND REQD IS COMPULSORY---
		Notify_Error(obj_hr); Notify_Error(obj_min); Notify_Error(obj_am); return false; 
	}
	var time = hr + ":" + minute + ":" + am;
	regexp = /^[0-1]{1,1}[0-9]{1,1}[\:][0-5]{1,1}[0-9]{1,1}[\:][A-Z]{2,2}$/;
	if(trim(time) != "" && !regexp.test(time)){
		Notify_Error(obj_hr); Notify_Error(obj_min); Notify_Error(obj_am); return false; 
	}
	Notify_Success(obj_hr); Notify_Success(obj_min); Notify_Success(obj_am); return true;
}

function Compare_Times(time1, time2){
	var hr1 = Number(time1.split(":")[0]); var min1 = Number(time1.split(":")[1]); var am1 = time1.split(":")[2];
	var hr2 = Number(time2.split(":")[0]); var min2 = Number(time2.split(":")[1]); var am2 = time2.split(":")[2];
	if(am1 != am2){ //---IF BOTH MERIDIANS ARE NOT SAME----
		if(am1 == "PM" && hr1 < 12) hr1 = hr1 + 12;
		if(am2 == "PM" && hr2 < 12) hr2 = hr2 + 12;
	}
	else if(am1 == am2 && am1 == "PM"){ //---IF BOTH MERIDIANS ARE NOT SAME----
		if(am1 == "PM" && hr1 < 12) hr1 = hr1 + 12;
		if(am2 == "PM" && hr2 < 12) hr2 = hr2 + 12;
	}
	if(hr2 < hr1) return false; //---IF HOUR2 IS LESS THAN HOUR1---
	else if(hr2 == hr1 && min2 <= min1) return false; //---IF MIN2 IS LESS EQUAL TO MIN1---
	return true;
}

function Change_Days(mnth_obj, day_obj){
	day_obj = document.getElementById(day_obj);
	var month = Number(trim(mnth_obj.value));
	day_obj.length=0;
	if(month == "") { day_obj.options[0]=new Option("DD", ""); return true; }
	day_obj.options[0]=new Option("DD", "");
	if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
		for(i=1; i<=31; i++){
			if(i < 10) dd = "0" + i; else dd = i;
			day_obj.options[i]=new Option(dd, dd);
		}
	}
	if(month==4 || month==6 || month==9 || month==11){
		for(i=1; i<=30; i++){
			if(i < 10) dd = "0" + i; else dd = i;
			day_obj.options[i]=new Option(dd, dd);
		}
	}
	if(month==2){
		for(i=1; i<=29; i++){
			if(i < 10) dd = "0" + i; else dd = i;
			day_obj.options[i]=new Option(dd, dd);
		}
	}
}

function timeDifference(laterdate,earlierdate) {
	var date1 = laterdate.split(":"); hr1 = Number(date1[0]); min1 = Number(date1[1]); am1 = date1[2]; if(am1 == "PM") hr1 += 12;
	var date2 = earlierdate.split(":"); hr2 = Number(date2[0]); min2 = Number(date2[1]); am2 = date2[2]; if(am2 == "PM") hr2 += 12;
	d = new Date();
	laterdate = new Date(d.getFullYear(), d.getMonth(), d.getDay(), hr1, min1, 0);
	earlierdate = new Date(d.getFullYear(), d.getMonth(), d.getDay(), hr2, min2, 0);
    var difference = laterdate.getTime() - earlierdate.getTime();
    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
    var secondsDifference = Math.floor(difference/1000);
	return hoursDifference;
}
