function showMessage(msg, imp, position){
	if(document.getElementById(position)){
//		document.getElementById(position).innerHTML = "<img src='images/msg" + imp + ".gif' /><font class='style14'>&nbsp;"+ msg + "</font>";
		if(imp == 1){
			alert(msg);
		}else if(imp == 4){
//			document.getElementById(position).innerHTML = "<img src='images/msg" + imp + ".gif' /><font class='style14'>&nbsp;"+ msg + "</font>";	
		}else {
			document.getElementById(position).innerHTML = "<img src='images/msg" + imp + ".gif' /><font class='style14'>&nbsp;"+ msg + "</font>";	
		}
	}
	return;
}
function showMessageOnParentsWindow(msg, imp, position){
	if(document.getElementById(position)){
		window.opener.document.getElementById(position).innerHTML = "<img src='images/msg" + imp + ".gif' /><font class='style14'>&nbsp;"+ msg + "</font>";
	}
	return;
}
function clearMessage(position){
	if(position){
		document.getElementById(position).innerHTML = "";
	}
	else {
		var msgs = document.getElementsByName("messageTd");
		for(var i = 0; i < msgs.length; i++){
			msgs[i].innerHTML = "";
		}
	}
	return;
}

function show_hide(id, flag){
	if(!document.getElementById(id))
		return;
	document.getElementById(id).style.display=(flag!='hide')?'inline':'none';
}

function processResponseXML(xmlDom, position){
	showMessage(xmlDom.getElements("message")[0].getText(), xmlDom.getElements("status")[0].getText(), position);
	return;
}

function getComplementOf(str1, str2){
	var ind = str1.indexOf(str2);
	if(ind < 0)
		return str1;
	else if(str1 == str2)
		return "";
	else
		return str1.substring(0, ind) + str1.substring(ind + str2.length, str1.length);
}

function inSameWeek(dateStr1, dateStr2){
	var date2;
	var stat = false;
	if(!dateStr2){
		date2 = new Date();
	}
	else{
		date2 = getDateFromStr(dateStr2);
	}
	
	var datesInWeek = getDatesInWeek(date2).split("|");
	for(var i=0; i<datesInWeek.length; i++) {
		if(dateStr1 == datesInWeek[i]){
			stat = true;
			break;
		}
	}
	return stat;
}

function getDatesInWeek(date){
      date.setDate(date.getDate() - date.getDay());
	  var dateStr = getStrFromDate(date);
      for(i=0; i<6; i++){
        date.setDate(date.getDate() + 1);
        dateStr += "|" + getStrFromDate(date);
      }
      return dateStr;
}

function highLight(frm, id){
	var ele = document.getElementById(id);
	ele.bgColor = frm.albatrossHighLight.value;
}

function removeHighLight(frm, id, prevClr){
	var ele = document.getElementById(id);
	ele.bgColor = prevClr;
}

function loadPage(option){
	top.mainFrame.document.location = option;
}

function remXtraWildChar(str, chr){
	var fromLeft = 0;
	var fromRight = 0;
	while((str.charAt(0) == chr) && str.length > 0){
		str = str.substring(1, str.length);
		fromLeft++;
	}
	while((str.charAt(--str.length) == chr) && str.length > 0){
		str = str.substring(0, str.length - 1);
		fromRight++;
	}
	
	if(fromLeft > 0){
		str = chr + str;
	}
	if(fromRight > 0){
		str = str + chr;
	}
	if(str == chr){
		str = "";
	}
	return str;
}

function validateEmail(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
 		 return true;					
	}

var dtCh= "-";
var minYear=1900;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr, msgLocation, maxYear){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if(!maxYear || maxYear.length == 0){
		var dat = new Date();
		maxYear = dat.getYear();
	}
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		showMessage("The date format should be : mm/dd/yyyy", 1, msgLocation);
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		showMessage("Please enter a valid month", 1, msgLocation);
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		showMessage("Please enter a valid day", 1, msgLocation);
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		showMessage("Please enter a valid 4 digit year between", 1, msgLocation);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		showMessage("Please enter a valid date", 1, msgLocation);
		return false;
	}
return true;
}

function validatePinCode(pin){
	if(pin.length != 6 || !isInteger(pin)){
		return false;
	}
	return true;
}

function getStrFromDate(date){
	return date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getYear()
}

function getDateFromStr(str){
	var datStr = str.split("-");
	var date = new Date(datStr[2], (datStr[1]-1), datStr[0]);
	return date;
}

function getDateFromSlot(slotId){
	var dat = new Date();
	var dateSelect = document.getElementById("dateSelect").value;
	if(!dateSelect || dateSelect == ""){
		dateSelect = getStrFromDate(dat);
	}
	if(!isDate(dateSelect, "timeTableMessage")){
		clearMessage("timeTableMessage");
		showMessage("Invalid date! Use DD-MM-YYYY format", 1, "timeTableMessage");
		return;
	}

	var selDat = getDateFromStr(dateSelect);
	var selDay = selDat.getDay();
	var diff = 0;
	if(1 <= slotId && slotId <= 8){
		diff = selDay - 1;
	}
	else if(9 <= slotId && slotId <= 16){
		diff = selDay - 2;
	}
	else if(17 <= slotId && slotId <= 24){
		diff = selDay - 3;
	}
	else if(25 <= slotId && slotId <= 32){
		diff = selDay - 4;
	}
	else if(33 <= slotId && slotId <= 40){
		diff = selDay - 5;
	}
	else if(41 <= slotId && slotId <= 48){
		diff = selDay - 6;
	}
	selDat.setDate(selDat.getDate()-diff);
	return selDat;
}

function getSlotFromDate(date){
	var day = date.getDay();
	if(day > 0)
		return (day-1)*8+1;
	else{
		return 1;
	}
}

function moveOption(select1, select2, index ){
	if (select1.length > index){
		var option = select1.options[index];
		select1.remove(index);
		
		try {
			select2.add(option, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			select2.add(option); // IE only
		}
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function calendarPopup(id){
	var cal1 = new calendar1(document.getElementById(id));
	cal1.year_scroll = true;
	cal1.time_comp = false;
	cal1.popup();	
}
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

// inserting and deleting table row

var theTable, theTableBody
/*function initTable(tableId) {
    theTable = (document.all) ? document.all.tableId : document.getElementById(tableId);
    theTableBody = theTable.tBodies[0];
}*/
//for appending row at particular postion from last row ,appending at last position : set variable value "positionFromLast = 0"
function appendRow(form,rowData,tableId,positionFromLast,cssClassName) {
	if(theTable==null||theTableBody==null){
		theTable = (document.all) ? document.all.summaryTable : document.getElementById(tableId);
		theTableBody = theTable.tBodies[0];
	}
    insertTableRow(rowData, form, theTableBody.rows.length - positionFromLast,tableId,cssClassName);
}
//for adding row at start
function addRow(form,rowData,tableId,cssClassName) {
	if(theTable==null||theTableBody==null){
		theTable = (document.all) ? document.all.summaryTable : document.getElementById(tableId);
		theTableBody = theTable.tBodies[0];
	}
    insertTableRow(rowData, form, 0,tableId,cssClassName)
}
function insertTableRow(rowData,form, where,tableId,cssClassName) {
//    var nowData = [now.getHours(), now.getMinutes(), now.getSeconds(),now.getMilliseconds()];
	if(theTable==null||theTableBody==null){
		theTable = (document.all) ? document.all.summaryTable : document.getElementById(tableId);
		theTableBody = theTable.tBodies[0];
	}	
    var newCell;
    var newRow = theTableBody.insertRow(where);
    for (var i = 0; i < rowData.length; i++) {
        newCell = newRow.insertCell(i);
        newCell.innerHTML = rowData[i];
		newCell.setAttribute("CLASS",cssClassName[i]);
    }
}
function roundeFloat(val, rPlaces){
	var rValue = parseFloat(val);
	var temp = ("" + rValue).split(".")[0];
	var decimalValue = "";
	if(("" + rValue).split(".").length == 2){
		decimalValue = ("" + rValue).split(".")[1];
		for(i=0;i<(rPlaces - decimalValue.length);i++){
			rValue += "0"
		}
	}else{
		rValue = rValue + ".";
		for(i=0;i<rPlaces;i++){
			rValue += "0"
		}
	}
	return rValue;
}
function closeWindow(){
	window.close();
}

function searchboxcheckonblur(str,id){
	var txt = str;
	var searchtxt=trimAll(document.getElementById(id).value);
	if(searchtxt == "")
	{
	document.getElementById(id).value=txt;
	}
	}
function searchboxcheckonfocus(str,id){
	var txt = str;
	var searchtxt=trimAll(document.getElementById(id).value);
	if(searchtxt==txt)
	{
		document.getElementById(id).value="";
	}
 }
function trimAll(sString){
	if(sString != ""){
		while (sString.substring(0,1) == ' ')
		{
		sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
		sString = sString.substring(0,sString.length-1);
		}
	}
	return sString;
}
function roundOff(val){
	val = parseInt(("" + val).split(".")[0]);
	var d = parseInt(val/5);
	var r = parseInt(val%5);
	if(r>=3){
		return (d+1)*5;
	}else{
		return (d)*5;
	}
}
function compareDates(dateStr1,dateStr2){ //date format : dd-mm-yyyy @return true if dateStr2 is greater than dateStr1
	var dateArrayTemp1 = dateStr1.split("-");
	var date1 = new Date(dateArrayTemp1[2],dateArrayTemp1[1],dateArrayTemp1[0]);
	var dateArrayTemp2 = dateStr2.split("-");
	var date2 = new Date(dateArrayTemp2[2],dateArrayTemp2[1],dateArrayTemp2[0]);
	if(date2 < date1){
		return false;
   	}else{
   		return true;
   	}
}