// JavaScript Document
//====================================================================================================
//	File Name		:	twoweek.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Client side validation in JavaScript.

//
//====================================================================================================

//====================================================================================================
//	Function Name	:	Form_Submit()
//	Purpose			:	This function will executed when user submits a form. It checks validity of
//						every field in the form.
//
//	Parameters		:	frm  - form name
//	Return			:	true or false

//----------------------------------------------------------------------------------------------------

function calendarCallback(date, month, year)
{
	if(month <= 9)
	{ month = '0'+month }
	if(date <= 9)
	{ date = '0'+date }
	//date =  year +'-' + month + '-' + date ;
	//document.frmweek.news_date.value = date;
	//document.frmweek.news_date.focus();
	document.frmweek.yearlist.value=year;
	document.frmweek.monthlist.value=month;
	document.frmweek.daylist.value=date;
}

function Form_Submit(frm)
{
	
	with(frm)
	{	
				
		if(!IsEmpty(fname,F_Name_Empty))
		{
			
			return false;
		}
		if(!IsEmpty(lname,L_Name_Empty))
		{
			
			return false;
		}
				
		if(!IsEmpty(address,Address_Empty))
		{
			
			return false;
		}
		
		if(!IsEmpty(city,City_Empty))
		{
			
			return false;
		}
		if(!IsEmpty(state,State_Empty))
		{
			
			return false;
		}
						
		if(!IsEmpty(country,Country_Empty))
		{
			
			return false;
		}
		
		if(!IsEmpty(email,Email_Empty))
		{
			
			return false;
		}
		if(!IsEmail(email,Valid_Email))
		{
		
			return false;
		}
		if(!IsEmpty(confirm_email,CEmail_Empty))
		{
			
			return false;
		}
		if(!IsEmail(confirm_email,CValid_Email))
		{
			
			return false;
		}
		if(email.value != confirm_email.value)
		{
		  alert(Match_Email);
		  confirm_email.focus();
		  return false;
		}		 	
		if(!IsValidInteger(day_phone,Phone_Empty))
		{
			return false;
		}
		if(!frm.condition.checked)
		{
			alert("You must confirm that you have read and agree to Terms & condition and Privacy Policy");
			return false;
		}
									 
	return true;
	}
}

//====================================================================================================
//	Function Name	:	Add_Click()
//	Purpose			:	This function will executed when user submits a form. It checks validity of 
//						every field in the form.
//----------------------------------------------------------------------------------------------------
function Add_Click()
{
	with(document.frmtwoweek)
	{
		Action.value = "Add";
		submit();
	}
}

function Check_Ava_Display(hotelid2)
{
	
	with(document.frmweek)
	{ 
	
		//alert(hotelid);
		//SubmitCheck.value = "Availability";
		//selected_month.value = monthlist.value;
		popupWindowURL("avaibility.php?Action=Avaibility&hotelid="+hotelid2, 'PageContent', 800, 400, false, false, true);
		//popupWindowURL("avaibility.php?room_id="+Room_Id+"&Action=View_Gallary&total="+Total, 'PageContent', 650, 500, false, false, true);
		//submit();
		
	}
}

function Check_ava_month(day,month,year,day_out,month_out,year_out)
{
	
	with(document.frmweek)
	{ 
	
		SubmitCheck.value = "Availability";
		selected_month.value = monthlist.value;
		//popupWindowURL("avaibility.php?room_id="+Room_Id+"&Action=View_Gallary&total="+Total, 'PageContent', 650, 500, false, false, true);
		submit();
		
	}
}

function Show_Room_Ava(Adate)
{
	Stamp = new Date();
	with(document.frmweek)
	{
		var cdate="",cyear,cmonth="";
		var fulldate="";
		cmonth=Stamp.getMonth()+1;
		if(cmonth<10)
		{
			cmonth="0"+cmonth;
		}
		
		if(cdate<10)
		{
			cdate="0"+cdate;
		}
		fulldate=Stamp.getFullYear()+"-"+cmonth+"-"+cdate;
		
		if(Adate<fulldate)
		{		
			alert(L_Proper_Arrival_Date);
		}
		else
		{
			Action.value="Show_Room";
			submit();
		}
	}
}

function Check_ava(day,month,year,day_out,month_out,year_out,cday11,cmonth11,cyear11,Check_Var,Wrong_Date)
{
	
	with(document.frmweek)
	{
		//alert(base64Encode(L_Proper_Arrival_Date));
		//var str=decode: (L_Proper_Arrival_Date);
		// Self-initialize the global variables
		//initBase64();
		//var encodedString = base64Decode(L_Proper_Arrival_Date);
		//var encodedString=encode_utf8(L_Proper_Arrival_Date);
		//alert(encodedString);
		/*if (encodeURIComponent) {
			string = encodeURIComponent(L_Proper_Arrival_Date);
			alert("if : " + string);
		} else {
			string = escape(L_Proper_Arrival_Date);
			alert("else : " + string);
		}*/
		/*alert(cday11);
		alert(cmonth11);
		alert(cyear11);*/
		if(day<10)
		{
			day="0"+day;
		}
		if(day_out<10)
		{
			day_out="0"+day_out;
		}
		if(cday1<10)
		{
			cday1="0"+cday1;
		}
		checkin_date=day+"-"+month+"-"+year;
		checkout_date=day_out+"-"+month_out+"-"+year_out;
		select_date=cday11+"-"+cmonth11+"-"+cyear11;//cday1+"-"+cmonth1+"-"+cyear1;
		
		dt1=getDateObject(checkin_date,"-");
		dt2=getDateObject(checkout_date,"-");
		dt3=getDateObject(select_date,"-");
		/*alert(checkin_date);
		alert(checkout_date);
		alert(select_date);*/
		//alert(dt1==dt2);
		var one_day=1000*60*60*24;


		var diff=Math.ceil((dt2.getTime()-dt1.getTime())/(one_day));
		//alert(checkin_date);
		//alert(checkout_date);
		//alert('date sel '+select_date);
		//alert("date : " + dt1);
		//alert("date : " + dt2);
		//alert("date : " + dt3);
		if(noperson.value!='')
		{
			if(!allDigits(noperson.value))
				{
					alert(L_no_of_adult_valid.value);
					noperson.focus();
					noperson.select();
					return false;
				}
		}
		
		if(roomno.value!='')
		{
			if(!allDigits(roomno.value))
				{
					alert(L_no_of_room_valid.value);
					roomno.focus();
					roomno.select();
					return false;
				}
		}
		if(dt1>dt2)
		{		
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			//alert("greater");
			return false;
		}
		else if(checkin_date==checkout_date)
		{	
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			//alert("equal");
			return false;
		}
		/*else if(diff > 14)
		{		
			//alert(Wrong_Date_Range);
			alert(Wrong_Date);
			return false;
		}*/
		else if(dt1<dt3)
		{
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			return false;
		}
		else
		{	
			Submit.value = "Check Availability";
			flag=Show_Room(day+'-'+month+"-"+year,day_out+"-"+month_out+"-"+year_out,Check_Var,Wrong_Date);
		}
	}
}

function getDateObject(dateString,dateSeperator)
{
	//This function return a date object after accepting 
	//a date string ans dateseparator as arguments
	var curValue=dateString;
	var sepChar=dateSeperator;
	var curPos=0;
	var cDate,cMonth,cYear;

	//extract day portion
	curPos=dateString.indexOf(sepChar);
	cDate=dateString.substring(0,curPos);
	
	//extract month portion				
	endPos=dateString.indexOf(sepChar,curPos+1);			cMonth=dateString.substring(curPos+1,endPos);

	//extract year portion				
	curPos=endPos;
	endPos=curPos+5;			
	cYear=curValue.substring(curPos+1,endPos);
	
	//Create Date Object
	dtObject=new Date(cYear,cMonth,cDate);	
	return dtObject;
}

function allDigits(str)
{
	
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
	var strLen = str.length;
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}

function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}

// Global lookup arrays for base64 conversions
var enc64List, dec64List;
// Load the lookup arrays once
function initBase64() {
    enc64List = new Array();
    dec64List = new Array();
    var i;
    for (i = 0; i < 26; i++) {
        enc64List[enc64List.length] = String.fromCharCode(65 + i);
    }
    for (i = 0; i < 26; i++) {
        enc64List[enc64List.length] = String.fromCharCode(97 + i);
    }
    for (i = 0; i < 10; i++) {
        enc64List[enc64List.length] = String.fromCharCode(48 + i);
    }
    enc64List[enc64List.length] = "+";
    enc64List[enc64List.length] = "/";
    for (i = 0; i < 128; i++) {
        dec64List[dec64List.length] = -1;
    }
    for (i = 0; i < 64; i++) {
        dec64List[enc64List[i].charCodeAt(0)] = i;
    }
}

function base64Encode(str) {
    var c, d, e, end = 0;
    var u, v, w, x;
    var ptr = -1;
    var input = str.split("");
    var output = "";
    while(end == 0) {
        c = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) : 
            ((end = 1) ? 0 : 0);
        d = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) : 
            ((end += 1) ? 0 : 0);
        e = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) : 
            ((end += 1) ? 0 : 0);
        u = enc64List[c >> 2];
        v = enc64List[(0x00000003 & c) << 4 | d >> 4];
        w = enc64List[(0x0000000F & d) << 2 | e >> 6];
        x = enc64List[e & 0x0000003F];
        
        // handle padding to even out unevenly divisible string lengths
        if (end >= 1) {x = "=";}
        if (end == 2) {w = "=";}
        
        if (end < 3) {output += u + v + w + x;}
    }
    // format for 76-character line lengths per RFC
    var formattedOutput = "";
    var lineLength = 76;
    while (output.length > lineLength) {
      formattedOutput += output.substring(0, lineLength) + "\n";
      output = output.substring(lineLength);
    }
    formattedOutput += output;
    return formattedOutput;
}

function base64Decode(str) {
    var c=0, d=0, e=0, f=0, i=0, n=0;
    var input = str.split("");
    var output = "";
    var ptr = 0;
    do {
        f = input[ptr++].charCodeAt(0);
        i = dec64List[f];
        if ( f >= 0 && f < 128 && i != -1 ) {
            if ( n % 4 == 0 ) {
                c = i << 2;
            } else if ( n % 4 == 1 ) {
                c = c | ( i >> 4 );
                d = ( i & 0x0000000F ) << 4;
            } else if ( n % 4 == 2 ) {
                d = d | ( i >> 2 );
                e = ( i & 0x00000003 ) << 6;
            } else {
                e = e | i;
            }
            n++;
            if ( n % 4 == 0 ) {
                output += String.fromCharCode(c) + 
                          String.fromCharCode(d) + 
                          String.fromCharCode(e);
            }
        }
    }
    while (typeof input[ptr] != "undefined");
    output += (n % 4 == 3) ? String.fromCharCode(c) + String.fromCharCode(d) : 
              ((n % 4 == 2) ? String.fromCharCode(c) : "");
    return output;
}



function adddate(day,month,year,frm,cval2)
{
	with(frm)
	{
	
	day   = parseInt(day) + 1;
	var monthc=month;
	
	if(cval2=='M' || cval2=='Y' || cval2=='D')
		month = monthc-1;
	else	
		month = monthc-1;
		
	newDate = new Date ( year, month, day );
	year  = newDate.getFullYear();
    
	if(cval2=='M' || cval2=='Y' || cval2=='D')
		month = newDate.getMonth()+1;
	else
		month = newDate.getMonth()+1;
	
    month = (month<10)?'0'+month:month;
    day   = newDate.getDate();
    formattedDate = month + '/' + day + '/' + year;
	
	daylist_out.value=day;
	monthlist_out.value=month;
	yearlist_out.value=year;
	
	}
	
}


function Show_Room(Adate,Ddate,Check_Var,Wrong_Date)
{
	Stamp = new Date();
	with(document.frmweek)
	{
		var cdate="",cyear,cmonth="";
		var fulldate="";
		cmonth=Stamp.getMonth()+1;
		cdate=Stamp.getDate();
		if(cmonth<10)
		{
			cmonth="0"+cmonth;
		}
		if(cdate<10)
		{
			cdate="0"+cdate;
		}
		fulldate=cdate+"-"+cmonth+"-"+Stamp.getFullYear();
		dt1=getDateObject(Adate,"-");
		dt2=getDateObject(fulldate,"-");
		dt3=getDateObject(Ddate,"-");
		if(dt1<dt2)
		{		
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			return false;
		}
		else if(dt1>dt3)
		{
			//alert(L_Proper_Arrival_Date);
			alert(Check_Var);
			return false;
		}
		else
		{
			Action.value="Show_Room";
			submit();
		}
	}
	

/*Stamp = new Date();
	with(document.frmweek)
	{
		var cdate="",cyear,cmonth="";
		var fulldate="";
		cmonth=Stamp.getMonth()+1;
		cdate=Stamp.getDate();
		if(cmonth<10)
		{
			cmonth="0"+cmonth;
		}
		
		if(cdate<10)
		{
			cdate="0"+cdate;
		}
		
		//fulldate=Stamp.getFullYear()+"-"+cmonth+"-"+cdate;
		fulldate=cdate+"-"+cmonth+"-"+Stamp.getFullYear();
		alert("adate : "+ Adate);
		alert("fulldate " + fulldate);
		alert("ddate : "+ Ddate);
		
		if(Adate>Ddate)
		{		
			//alert(L_Proper_Arrival_Date);
			alert("greater");
		}
		else if(Adate<fulldate)
		{		
			alert("less");
			//alert(L_Proper_Arrival_Date);
		}
		else
		{
			Action.value="Show_Room";
			submit();
		}
	}*/
}

function Change_Month(Month)
{ 
	 with(document.frmweek)
	 {
		 selected_month.value = Month;
		 submit();
	 } 
}
function Change_Year(Year)
{	
	 with(document.frmweek)
	 {
		  selected_year.value = Year;
		  submit();
	 } 
}
function New_Search()
{
	 with(document.frmweek)
	 {
		Action.value='Reset';
		submit();
	 	
	 }
}

function IsValidInteger(fld,msg)
{
	var regex = /^[0-9]*$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//checking for other charges fields
/*L_Empty_Room,
					L_Valid_Room,
					L_Zero_Room,
					L_Valid_Adult,
					L_Valid_Child,
					L_Valid_Charge*/
function Submit_Form()
{
	with(document.frmothercharge)
	{	
		//alert(L_Empty_Room.value);
		
		var roominventory = parseInt(room_inventory.value);
		var roomrequest = parseInt(no_of_room.value);
		
		if(!IsEmpty(no_of_room,L_Empty_Room.value))
		{
			return false;
		}

		if(!IsValidInteger(no_of_room,L_Valid_Room.value))
		{
			
			return false;
		}

		if(no_of_room.value<=0)
		{
			alert(L_Zero_Room.value);
			no_of_room.focus();
			return false;
		}
		if(roominventory < roomrequest)
		{
			//alert(JM_Max_room+" "+room_inventory.value+" "+JM_Max_room_Avai);
			//"JM_Max_room"	=> "Maximum",
			//"JM_Max_room_Avai"	=>" Room Available."
			alert("Maximum"+" "+room_inventory.value+" "+"Room Available.");
			no_of_room.focus();
			return false;
		}
		if(!IsEmpty(extra_adult,L_Valid_Adult.value))
		{
			return false;
		}
		if(!IsValidInteger(extra_adult,L_Valid_Adult.value))
		{
			return false;
		}
		if(extra_adult.value<=0)
		{
			alert(L_Valid_Adult.value);
			extra_adult.focus();
			return false;
		}
		if(!IsEmpty(extra_child,L_Valid_Child.value))
		{
			return false;
		}
		if(!IsValidInteger(extra_child,L_Valid_Child.value))
		{
			return false;
		}
		var c=0;
		if(ChargeId.value!='')
		{
			var data=ChargeId.value;
			var arrdata=data.split(",");
			var i;
			var regex = /^[0-9]*$/;
			//alert(data);
			for(i=0; i< arrdata.length-1 ;i++)
				{
					var t="c_"+ arrdata[i];
					var a=eval(t + ".value");
					if(a!='')
					{
						if(!regex.test(a))
						{
							alert(L_Valid_Charge.value);
							eval(t+".focus()");
							eval(t+".select()");
							c++;
							return false;
							
						}
					}//if
				}
				if(c!=0)
					return false;
		}
			
	return true;
	}
}

function Privacy_Click(page_Id)
{
	with(document.frmtwoweek)
	{
		var hotel_id1=hotel_id.value;
		popupWindowURL("document.php?page_id="+page_Id+"&hotel_id="+hotel_id1, 'PageContent', 650, 500, false, false, true);
	}
}

function Terms_Click(page_Id)
{
	with(document.frmtwoweek)
	{
		var hotel_id1=hotel_id.value;
		popupWindowURL("document.php?page_id="+page_Id+"&hotel_id="+hotel_id1, 'PageContent', 650, 500, false, false, true);
	}
}

function View_Gallary(Room_Id,Total)
{
	with(document.frmroom)
	{
		popupWindowURL("gallary.php?room_id="+Room_Id+"&Action=View_Gallary&total="+Total, 'PageContent', 650, 500, false, false, true);
	}
}
function Next_Click()
{
		with(document.frmview)
		{
			action.value="Next";
			submit();
		}
}
function Previous_Click()
{
		with(document.frmview)
		{
			action.value="Previous";
			submit();
		}

}

/*
function base64Decode(str) {
    var c=0, d=0, e=0, f=0, i=0, n=0;
    var input = str.split("");
    var output = "";
    var ptr = 0;
    do {
        f = input[ptr++].charCodeAt(0);
        i = dec64List[f];
        if ( f >= 0 && f < 128 && i != -1 ) {
            if ( n % 4 == 0 ) {
                c = i << 2;
            } else if ( n % 4 == 1 ) {
                c = c | ( i >> 4 );
                d = ( i & 0x0000000F ) << 4;
            } else if ( n % 4 == 2 ) {
                d = d | ( i >> 2 );
                e = ( i & 0x00000003 ) << 6;
            } else {
                e = e | i;
            }
            n++;
            if ( n % 4 == 0 ) {
                output += String.fromCharCode(c) + 
                          String.fromCharCode(d) + 
                          String.fromCharCode(e);
            }
        }
    }
    while (typeof input[ptr] != "undefined");
    output += (n % 4 == 3) ? String.fromCharCode(c) + String.fromCharCode(d) : 
              ((n % 4 == 2) ? String.fromCharCode(c) : "");
    return output;
}*/


/*var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}*/