/* to check the users email, alphabets, name, password, remove spaces form the Name through trim() etc..*/
function trim (strVar) { 
	if(strVar.length >0)
	{
		while(strVar.charAt(0)==" ")						//remove left spaces
			strVar=strVar.substring(1,strVar.length);
		while(strVar.charAt(strVar.length-1)==" ")			//remove right spaces
			strVar=strVar.substring(0,strVar.length-1);
	}
	return strVar;
}

function checkEmail(email) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email))
	{
		return (true);
	}
	return false;
}


var pop = '';
function openwin(nm,width,height) {
	var name = nm;
	if (pop && !pop.closed) {
		pop.close();
	}
	pop = eval("window.open('"+name+"','NewWIN','chrome[4],toolbar=no,left=5,top=5,width="+width+",height="+height+",directories=no,menubar=no,SCROLLBARS=yes,left=2,right=2')");
	if (!pop.opener) popUpWin.opener = self;
}

function closewin()
{
	window.close();
}

function textLimitCheck(thisArea, maxLength, msg)
{
	if (thisArea.value.length > maxLength)
	{
		alert(maxLength + ' characters limit.\rExcessive data will be truncated.');
		thisArea.value = thisArea.value.substring(0, maxLength);
		thisArea.focus();
	}
	document.getElementById(msg).innerText = thisArea.value.length;
}

//DATE VALIDATION
function dateValid(dd, mm, yyyy)
{
	if(dd.length==0 || mm.length==0 || yyyy.length==0)
		return false;
	if(isNaN(dd)) 
		return false;
	if(isNaN(mm)) 
		return false;
	if(isNaN(yyyy)) 
		return false;
	if(yyyy.length<4)
	{
		alert("Please Enter Year in four digit!");
		return false;
	}
	if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
	{
		if(dd>31)
			return false;
	}
	else
	{
		if(mm==4 || mm==6 || mm==9 || mm==11)
		{
			if(dd>30)
				return false;
		}
		else
		{
			var f_day=isLeap(yyyy);
			if(dd>f_day) return false;
		}
	}
	return true;
}

//CHECK DAY IN FEBRUARY MONTH
function isLeap(year)
{
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

//CHECK DATE DIFFERENCE BETWEEN TO DATES
function dateDiff(dd1, mm1, yy1, dd2, mm2, yy2)
{
	var date1 = yy1 + mm1 + dd1;
	var date2 = yy2 + mm2 + dd2;
	if(date1>date2) return false;
	return true;
}

function getCurrentDate()
{
	currentTime = new Date();

	dd = currentTime.getDate();
	mm = currentTime.getMonth();
	yy = currentTime.getFullYear();
	mm = mm + 1;
	if(dd<10) dd = "0" + dd;
	if(mm<10) mm = "0" + mm;
	currDate = yy + "-" + mm + "-" + dd;

	return currDate;
}

function checkAlert(theForm)
{
	var name = trim(theForm.name.value);
	var email = trim(theForm.email.value);
	if(name.length==0)
	{
		alert("Please enter your name!");
		theForm.name.focus();
		return false;
	}
	if(email.length==0)
	{
		alert("Please enter your e-mail!");
		theForm.email.focus();
		return false;
	}
	if(checkEmail(email) == false)
	{
		alert("Invalid email! Please re-enter.");
		theForm.email.select();
		return false;
	}
	return true;
}



/* CALENDER JS*/
var HighlightToday  = true;    
var DisablePast    = true;
var MonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

// Global Vars
var now = new Date();
var dest = null;
var pos  = null;
var sDate = null;
var ny = now.getFullYear(); 
var nm = now.getMonth();
var nd = now.getDate();
var sy = 0; 
var sm = 0;
var sd = 0;
var y = now.getFullYear(); 
var m = now.getMonth();
var d = now.getDate();
var l = 0;
var t = 0;
var MonthLengths = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function getXCoord(el) {
	x = 0;
	while(el){
		x += el.offsetLeft;
		el = el.offsetParent;
	}
	return x;
}
function getYCoord(el) {
	y1 = 0;
	while(el){
		y1 += el.offsetTop;
		el = el.offsetParent;
	}
	return y1;
}

function GetDate() {
  EnsureCalendarExists();
  DestroyCalendar();
 
  if(arguments[0] == null || arguments[0] == "") {
       alert("ERROR: Destination control required in funciton call GetDate()");
    return;
  } else {
  
    dest = arguments[0];
	pos  = arguments[1];
	sDate = arguments[2];
  }
  y = now.getFullYear();
  m = now.getMonth();
  d = now.getDate();
  sm = 0;
  sd = 0;
  sy = 0;
  var cdval = dest.value;
 
  if(/\d{1,2}.\d{1,2}.\d{4}/.test(dest.value)) {
    
    var vParts = cdval.split("/"); // assume mm/dd/yyyy
    sd = vParts[0];
	sm = vParts[1] - 1;
    sy = vParts[2];
    m=sm;
    d=sd;
    y=sy;
  }
   if(sDate != null){
	var sdval = sDate.value;
	if(/\d{1,2}.\d{1,2}.\d{4}/.test(sDate.value)) {
    
    var vParts = sdval.split("/"); 
    sd = vParts[0];
	sm = vParts[1] - 1;
    sy = vParts[2];
    m=sm;
    d=sd;
    y=sy;
  }
   }

  l = getXCoord(pos);
  t  = getYCoord(pos);
  
  if(t < 0) t = 0; 
  DrawCalendar();
}

function DestroyCalendar() {
  var cal = document.getElementById("dpCalendar");
  if(cal != null) {
    cal.innerHTML = null;
    cal.style.display = "none";
  }
  return
}

function DrawCalendar() {
  DestroyCalendar();
  cal = document.getElementById("dpCalendar");
  
  cal.style.position   = 'absolute';
  cal.style.left = l-70 + "px";
  cal.style.top = t + "px";
 
  
  var sCal = "<table><tr><td class=\"cellButton\"><a href=\"javascript: PrevMonth();\" title=\"Previous Month\">&laquo;</a></td>"+
    "<td class=\"cellMonth\" width=\"80%\" colspan=\"5\">"+MonthNames[m]+" "+y+"</td>"+
    "<td class=\"cellButton\"><a href=\"javascript: NextMonth();\" title=\"Next Month\">&raquo;</a></td></tr>"+
    "<tr align=center><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td</tr>"+
    "<tr><td height=1 bgcolor=\"#000066\" colspan=\"7\"></td</tr>";
  var wDay = 1;
  var wDate = new Date(y,m,wDay);
  
  if(isLeapYear(wDate)) {
    MonthLengths[1] = 29;
  } else {
    MonthLengths[1] = 28;
  }
  var dayclass = "";
  var isToday = false;
  for(var r=1; r<7; r++) {
    sCal = sCal + "<tr>";
    for(var c=0; c<7; c++) {
      var wDate = new Date(y,m,wDay);
      if(wDate.getDay() == c && wDay<=MonthLengths[m]) {
        if(wDate.getDate()==sd && wDate.getMonth()==sm && wDate.getFullYear()==sy) {
          dayclass = "cellSelected";
          isToday = true;  // only matters if the selected day IS today, otherwise ignored.
        } else if(wDate.getDate()==nd && wDate.getMonth()==nm && wDate.getFullYear()==ny && HighlightToday) {
          dayclass = "cellToday";
          isToday = true;
        } else {
          dayclass = "cellDay";
          isToday = false;
        }

		if(sDate != null && wDay < sd && wDate.getMonth()==sm && wDate.getFullYear()==sy)
		{
			 sCal = sCal + "<td class=\""+dayclass+"\" style=text-decoration:line-through;>"+wDay+"</td>";
		}
       else if(((now > wDate) && !DisablePast) || (now <= wDate) || isToday) { // >
         
          sCal = sCal + "<td class=\""+dayclass+"\"><a href=\"javascript: ReturnDay("+wDay+");\">"+wDay+"</a></td>";
        }  else {
         	sCal = sCal + "<td class=\""+dayclass+"\" style=text-decoration:line-through;>"+wDay+"</td>";
        }
        wDay++;
      } else {
        sCal = sCal + "<td class=\"unused\"></td>";
      }
    }
    sCal = sCal + "</tr>";
  }
  sCal = sCal + "<tr><td colspan=\"5\" class=\"unused\"></td><td colspan=\"3\" class=\"cellCancel\"><a href=\"javascript: DestroyCalendar();\"><b>close</b></a></td></tr></table>"
  cal.innerHTML = sCal; 
  cal.style.display = "inline";
}

function PrevMonth() {
  m--;
  if(m==-1) {
    m = 11;
    y--;
  }
  DrawCalendar();
}

function NextMonth() {
  m++;
  if(m==12) {
    m = 0;
    y++;
  }
  DrawCalendar();
}

function ReturnDay(day) {
  cDest = document.getElementById(dest);
  var a = m+1;
  var b = day;
  if(a<10)	var a ='0'+a;
  if(b<10) var b ='0'+day;
 //dest.value = day+"/"+(m+1)+"/"+y;
 dest.value = b+"/"+a+"/"+y;
 
  DestroyCalendar();
}

function EnsureCalendarExists() {
  if(document.getElementById("dpCalendar") == null) {
    var eCalendar = document.createElement("div");
    eCalendar.setAttribute("id", "dpCalendar");
    document.body.appendChild(eCalendar);
  }
}

function isLeapYear(dTest) {
  var y = dTest.getYear();
  var bReturn = false;
  
  if(y % 4 == 0) {
    if(y % 100 != 0) {
      bReturn = true;
    } else {
      if (y % 400 == 0) {
        bReturn = true;
      }
    }
  }
  
  return bReturn;
}


function checkCommonQueryForm(thisForm)
{
	var guestName	= trim(thisForm.guestName.value);
	var mobile1		= trim(thisForm.mobile1.value);
	var mobile		= trim(thisForm.mobile.value);
	var email		= trim(thisForm.email.value);

	var arrivalDt = trim(thisForm.arrivalDt.value);
	var departDt = trim(thisForm.departDt.value);

	var city		= trim(thisForm.city.value);
	var totalRoom	= trim(thisForm.totalRoom.value);
	var adult		= trim(thisForm.adult.value);
	var child		= trim(thisForm.child.value);

	if(guestName.length==0)
	{
		alert("Please enter Name!!");
		thisForm.guestName.focus();
		return false
	}
	if(guestName.length==0)
	{
		alert("Please enter Name!!");
		thisForm.guestName.focus();
		return false
	}
	if(mobile1.length==0)
	{
		alert("Please enter Mobile Country Code!!");
		thisForm.mobile1.focus();
		return false
	}
	if(mobile.length==0)
	{
		alert("Please enter Mobile # !!");
		thisForm.mobile.focus();
		return false
	}
	if(email.length==0)
	{
		alert("Please enter Email!!");
		thisForm.email.focus();
		return false
	}
	if(checkEmail(email) == false)
	{
		alert("Invalid email! Please re-enter.");
		thisForm.email.select();
		return false;
	}
	if(arrivalDt.length==0)
	{
		alert("Please select Arrival Date!!");
		thisForm.arrivalDt.focus();
		return false
	}
	if(departDt.length==0)
	{
		alert("Please select Departure Date!!");
		thisForm.departDt.focus();
		return false
	}
	if(city.length==0)
	{
		alert("Please enter Traveling From!!");
		thisForm.city.focus();
		return false
	}
	if(totalRoom.length==0)
	{
		alert("Please enter No. of Rooms!!");
		thisForm.totalRoom.focus();
		return false
	}
	if(isNaN(totalRoom))
	{
		alert("Please enter Valid No. of Rooms!!");
		thisForm.totalRoom.select();
		return false	
	}
	if(adult.length==0)
	{
		alert("Please enter Adults!!");
		thisForm.adult.focus();
		return false
	}
	if(isNaN(adult))
	{
		alert("Please enter Valid No. of Adults!!");
		thisForm.adult.select();
		return false;
	}
	if(child.length>0)
	{
		if(isNaN(child))
		{
			alert("Please enter Valid No. of Kids!!");
			thisForm.child.select();
			return false;
		}
	}
	return true;
}

