var imageHost = "http://images.studentuniverse.com";

	loggedIn = false;
	hasResults = false;
	
function getCurrentDateRef() 
{
	return new Date();
}

var months = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");

var daysFull = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var days = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

function formatTime(dateObj) {
	timeString = days[dateObj.getDay()] + " " + formatTimeOfDay(dateObj);
	return timeString;
}

function formatFullDate(dateObj) {
	year =  (dateObj.getYear() % 1900) + 1900; // IE returns full year, FF # years after 1900
	timeString = daysFull[dateObj.getDay()] + ", " + months[dateObj.getMonth()] + " " + dateObj.getDate() + " " +  year;
	return timeString;
}

function getEasternTimeOffset(timeString) {
	var easternDate = new Date("Wed Jul 25 13:49:42 EDT 2007");
	var localDate = new Date("Wed Jul 25 13:49:42 2007");
	var offset = easternDate.getTime() - localDate.getTime();
	return offset;
}

function formatTimeOfDay(origDateObj) 
{
	var dateObj = new Date(origDateObj.getTime());
	
	dateObj.setTime( dateObj.getTime()  );
	
	ampm = "am";
	hours = dateObj.getHours();
	
	if ( hours > 12 ) {
		hours -= 12;
		ampm = "pm";
	}
	else if ( hours == 12 ) ampm = "pm";
	
	if ( hours == 0 ) hours = 12;
	
	minutes = dateObj.getMinutes();
	if ( minutes < 10 ) {
		minutes = "0" + minutes;
	}
	timeString = hours + ":" + minutes + ampm;
	return timeString;
}

function roundToNearestMinutes(date,minuteInterval,roundUp) {
	numBlocks = date.getMinutes() / minuteInterval;
	if ( roundUp ) {
		numBlocks = Math.ceil(numBlocks);
	}
	else {
		numBlocks = Math.floor(numBlocks);
	}
	date.setMinutes( numBlocks * minuteInterval );
	return date;
}

function resolveScheme(url) {
	// abort if this isn't a regular url ("javascript:" or something)
	if ( url.indexOf("http") != 0 ) return url; 
	// abort if this url isn't on the current domain (external link)
	else if ( url.indexOf(insecureHost) != 0 && url.indexOf(secureHost) != 0 ) return url;
	var uri=getURI(url);
	return fullPathToMe(uri);
}

function get(elementID) {
	return document.getElementById(elementID);
}

function createElement(type,id,className,parentObj) {
	var element=get(id);
	if ( !element ) {
		if ( !parentObj ) parentObj=document.body;
		var element=document.createElement(type);
		element.id=id;
		document.body.appendChild(element);
	}
	element.className=className;
	return element;
}

function getTopPos(div) {
	pos=div.offsetTop; //init
	while ( div.offsetParent ) {
		div=div.offsetParent;
		pos += div.offsetTop;
	}
	return pos;
}

function getBottomPos(div) {
	return div.offsetHeight+getTopPos(div);
}

function getLeftPos(div) {
	pos=div.offsetLeft; //init
	while ( div.offsetParent ) {
		div=div.offsetParent;
		pos += div.offsetLeft;
	}
	return pos;
}

function getRightPos(div) {
	return div.offsetWidth+getLeftPos(div);
}



jQuery(document).ready(function()
{

	var myDate = new Date();
	var myTorrow = new Date(); 
	myTorrow.setDate(myTorrow.getDate()+1); 

	reWriteDayOfWeek();

	jQuery("#checkInDate").blur(function(){
		
		
		var ii = jQuery("#checkInDate").val();
		var arrii = ii.split("/");
		var checkInDateInput;
		
		if (arrii!=undefined && arrii.length == 3)
		{
			//date ok
			if(arrii[0].length < 2)
			{  
				arrii[0] = "0"+arrii[0];
			}
			if(arrii[1].length < 2)
			{
				arrii[1] = "0"+arrii[1];
			}
			if(arrii[2].length < 4)
			{
				arrii[2] = "20"+arrii[2];
			}
			
			checkInDateInput = new Date();
			checkInDateInput.setFullYear(arrii[2],arrii[0]-1,arrii[1]);
		}
		else
		{
			//date error
			checkInDateInput = new Date();
		}
		
	
		var today = new Date();

        if(jQuery("#checkInDate").val()=="" || (checkDate(jQuery("#checkInDate").val())==false))
		{
			var myDate = new Date();
			jQuery("#checkInDate").val((myDate.getMonth()+1)+"/"+myDate.getDate()+"/"+myDate.getFullYear().toString().substring(2));
		}
		else if(today > checkInDateInput)
		{
			var ss = jQuery("#checkInDate").val();
			var arrss = ss.split("/");
			jQuery("#checkInDate").val(arrss[0]+"/"+arrss[1]+"/"+(parseInt(arrss[2])+1,10).toString());
			jQuery("#checkOutDate").val(arrss[0]+"/"+arrss[1]+"/"+(parseInt(arrss[2])+1,10).toString());
		}

        reWriteDayOfWeek();
	})
	
	jQuery("#checkOutDate").blur(function()
	{
		var oo = jQuery("#checkOutDate").val();
		var arroo = oo.split("/");
		var checkOutDateInput;
		
		if (arroo!=undefined && arroo.length == 3)
		{
			if(arroo[0].length < 2)
			{  
				arroo[0] = "0"+arroo[0];
			}
			if(arroo[1].length < 2)
			{
				arroo[1] = "0"+arroo[1];
			}
			if(arroo[2].length < 4)
			{
				arroo[2] = "20"+arroo[2];
			}
			
			checkOutDateInput = new Date();
			checkOutDateInput.setFullYear(arroo[2],arroo[0]-1,arroo[1]);
		}
		else
		{
			//error
			alert('The check-out date is invalid.');
			checkOutDateInput = new Date();
		}

		

		var today = new Date();
		
		if(jQuery("#checkOutDate").val()=="" || (checkDate(jQuery("#checkOutDate").val())==false))
		{
			var myTorrow = new Date(); 
			myTorrow.setDate(myTorrow.getDate()+1); 
			jQuery("#checkOutDate").val((myTorrow.getMonth()+1)+"/"+myTorrow.getDate()+"/"+myTorrow.getFullYear().toString().substring(2));  
			
			
	    }else if(today > checkOutDateInput)
		{
			var ss = jQuery("#checkOutDate").val();
			var arrss = ss.split("/");
			jQuery("#checkOutDate").val(arrss[0]+"/"+arrss[1]+"/"+(parseInt(arrss[2])+1,10).toString());
			
		}
	    
        reWriteDayOfWeek();
	})

	if(jQuery.browser.msie){
		if(jQuery.browser.version > 7){
           
            jQuery("#checkInCalendarLink").css("left","170px");
            jQuery("#checkInCalendarLink").css("margin-top","3px");

            
            jQuery("#checkOutCalendarLink").css("left","170px");
            jQuery("#checkOutCalendarLink").css("margin-top","3px");
            //jQuery("#checkOutDate").css("margin-left","193px");
		 }else{

			jQuery("#checkInCalendarLink").css("left","172px");
			jQuery("#checkInCalendarLink").css("margin-top","5px");

			
			jQuery("#checkOutCalendarLink").css("left","172px");
			jQuery("#checkOutCalendarLink").css("margin-top","5px");
			//jQuery("#checkOutDate").css("margin-left","193px");
		 }

		jQuery("#checkIn-calendarIcon").bind("click",calendarClick);
		jQuery("#checkOut-calendarIcon").bind("click",calendarClick);
    }

	if(jQuery.browser.mozilla){

		jQuery("#checkIn-calendarIcon").bind("click",calendarClick);
		jQuery("#checkOut-calendarIcon").bind("click",calendarClick);

    }

    if(jQuery.browser.safari){

		jQuery("#checkIn-calendarIcon").bind("click",calendarClick);
		jQuery("#checkOut-calendarIcon").bind("click",calendarClick);
        
        var bVersion = jQuery.browser.version;
        var temp = bVersion.split(".");
        var sVersion = temp[0];
        if(sVersion < 528){
        	jQuery("#checkInCalendarLink").css("left","122px");
        	jQuery("#checkInCalendarLink").css("margin-top","4px");
        	
        	jQuery("#checkOutCalendarLink").css("left","433px");
        	jQuery("#checkOutCalendarLink").css("margin-top","4px");
        }
    }
    
});

function calendarClick(){
	jQuery("#calendarDiv").bind("click",reWriteDayOfWeek);
}

function reWriteDayOfWeek(){//yyyy-m-d

	var myDate = new Date();
	var myTorrow = new Date();
	myTorrow.setDate(myTorrow.getDate()+1);

    var validatCheckInFormat = checkDate(jQuery("#checkInDate").val());
    var validatCheckOutFormat = checkDate(jQuery("#checkOutDate").val());
    
    if(!(validatCheckInFormat && validatCheckOutFormat)){
        if(!validatCheckInFormat){
        	jQuery("#checkInDate").val((myDate.getMonth()+1)+"/"+myDate.getDate()+"/"+myDate.getFullYear().toString().substring(2));
            }
        if(!validatCheckOutFormat){
        	jQuery("#checkOutDate").val((myTorrow.getMonth()+1)+"/"+myTorrow.getDate()+"/"+myTorrow.getFullYear().toString().substring(2));
            }
        
        }else{
        	var oo = jQuery("#checkOutDate").val();
        	
        	var arroo = oo.split("/");
        	if(arroo[0].length < 2){  
        		arroo[0] = "0"+arroo[0];
        		}
        	if(arroo[1].length < 2){
        		arroo[1] = "0"+arroo[1];
        		}
        	if(arroo[2].length < 4){
        		arroo[2] = "20"+arroo[2];
        		}
        	var asEndDate = arroo[2]+"/"+arroo[0]+"/"+arroo[1];

        	var ii = jQuery("#checkInDate").val();
            var arrii = ii.split("/");
        	if(arrii[0].length < 2){
        		arrii[0] = "0"+arrii[0];
        		}
        	if(arrii[1].length < 2){
        		arrii[1] = "0"+arrii[1];
        		}
        	if(arrii[2].length < 4){
        		arrii[2] = "20"+arrii[2];
        		}
            var asStartDate = arrii[2]+"/"+arrii[0]+"/"+arrii[1];  

            var startday = new Date(Date.parse(asStartDate)); 
            var endday = new Date(Date.parse(asEndDate));
            var today = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

            jQuery("#checkInDesc").html(today[startday.getDay()]);
            jQuery("#checkOutDesc").html(today[endday.getDay()]);
            }

}

function validationCheckBothDate(){
	var oo = jQuery("#checkOutDate").val();
	
	var arroo = oo.split("/");
	if(arroo[0].length < 2){  
		arroo[0] = "0"+arroo[0];
		}
	if(arroo[1].length < 2){
		arroo[1] = "0"+arroo[1];
		}
	if(arroo[2].length < 4){
		arroo[2] = "20"+arroo[2];
		}
	var asEndDate = arroo[2]+"-"+arroo[0]+"-"+arroo[1];

	var ii = jQuery("#checkInDate").val();
    var arrii = ii.split("/");
	if(arrii[0].length < 2){
		arrii[0] = "0"+arrii[0];
		}
	if(arrii[1].length < 2){
		arrii[1] = "0"+arrii[1];
		}
	if(arrii[2].length < 4){
		arrii[2] = "20"+arrii[2];
		}
    var asStartDate = arrii[2]+"-"+arrii[0]+"-"+arrii[1];    
	
	 var miStart = Date.parse(asStartDate.replace(/\-/g, '/'));
	 var miEnd   = Date.parse(asEndDate.replace(/\-/g, '/'));

      if((miEnd-miStart)<=0){
    	  return false;
         }else{
            return true;
             }
	
}

function   DateAdd(strInterval,   NumDay,   dtDate)   {  
	  var   dtTmp   =   new   Date(dtDate);  
	  if   (isNaN(dtTmp))   dtTmp   =   new   Date();  
	  switch   (strInterval)   {  
	  case   "s":return   new   Date(Date.parse(dtTmp)   +   (1000   *   NumDay));  
	  case   "n":return   new   Date(Date.parse(dtTmp)   +   (60000   *   NumDay));  
	  case   "h":return   new   Date(Date.parse(dtTmp)   +   (3600000   *   NumDay));  
	  case   "d":return   new   Date(Date.parse(dtTmp)   +   (86400000   *   NumDay));  
	  case   "w":return   new   Date(Date.parse(dtTmp)   +   ((86400000   *   7)   *   NumDay));  
	  case   "m":return   new   Date(dtTmp.getFullYear(),   (dtTmp.getMonth())   +   NumDay,   dtTmp.getDate(),   dtTmp.getHours(),   dtTmp.getMinutes(),   dtTmp.getSeconds());  
	  case   "y":return   new   Date((dtTmp.getFullYear()   +   NumDay),   dtTmp.getMonth(),   dtTmp.getDate(),   dtTmp.getHours(),   dtTmp.getMinutes(),   dtTmp.getSeconds());  
	  }  
} 

function checkIfNeedUpdateOutDate()
{
	var oo = jQuery("#checkInDate").val();
	
	var arroo = oo.split("/");
	var dftTemp;
	
	if (arroo!=undefined && arroo.length == 3)
	{
		//date is ok
		if(arroo[0].length < 2)
		{  
			arroo[0] = "0"+arroo[0];
		}
		if(arroo[1].length < 2)
		{
			arroo[1] = "0"+arroo[1];
		}
		if(arroo[2].length < 4)
		{
			arroo[2] = "20"+arroo[2];
		}

		dftTemp = DateAdd("d",1,new Date(arroo[2],arroo[0],arroo[1])).format("mm/dd/yy");

		var dftTemp = new Date();
		dftTemp.setFullYear(arroo[2],arroo[0]-1,arroo[1]);
		
		dftInTemp = DateAdd("d",1,dftTemp).format("mm/dd/yy");
	
		if(validationCheckBothDate())
		{
		
		}
		else
		{
			jQuery("#checkOutDate").val(dftInTemp).css("color","#000000");	
		}
	}
	else
	{
		alert('The check-in date is invalid.');
		dftTemp = new Date();
		dftInTemp = DateAdd("d",1,dftTemp).format("mm/dd/yy");
		jQuery("#checkOutDate").val(dftInTemp).css("color","#000000");
	}
	
	
}

function Date_Compare(){

	var oo = jQuery("#checkOutDate").val();
	
	var arroo = oo.split("/");
	if(arroo[0].length < 2){  
		arroo[0] = "0"+arroo[0];
		}
	if(arroo[1].length < 2){
		arroo[1] = "0"+arroo[1];
		}
	if(arroo[2].length < 4){
		arroo[2] = "20"+arroo[2];
		}
	var asEndDate = arroo[2]+"-"+arroo[0]+"-"+arroo[1];

	var ii = jQuery("#checkInDate").val();
    var arrii = ii.split("/");
	if(arrii[0].length < 2){
		arrii[0] = "0"+arrii[0];
		}
	if(arrii[1].length < 2){
		arrii[1] = "0"+arrii[1];
		}
	if(arrii[2].length < 4){
		arrii[2] = "20"+arrii[2];
		}
    var asStartDate = arrii[2]+"-"+arrii[0]+"-"+arrii[1];    
	
	 var miStart = Date.parse(asStartDate.replace(/\-/g, '/'));
	 var miEnd   = Date.parse(asEndDate.replace(/\-/g, '/'));

     return (miEnd-miStart)/(1000*24*3600);
	}


function checkDate(date) {
	
	date = date.replace(/-/g,"/");
	var validFormat = /(^\d{1,2}\/\d{1,2}\/\d\d$)/;
	var validFormatFullYear = /(^\d{1,2}\/\d{1,2}\/\d\d\d\d$)/;
	var validDateStatus = validFormat.test(date) || validFormatFullYear.test(date);
	if ( !validDateStatus || validatedDate(date)==null) {
		return false;
	}else{
		return true;
	}
}




function validationForDate(){
	var oo = jQuery("#checkOutDate").val();
	
	var arroo = oo.split("/");
	if(arroo[0].length < 2){  
		arroo[0] = "0"+arroo[0];
		}
	if(arroo[1].length < 2){
		arroo[1] = "0"+arroo[1];
		}
	if(arroo[2].length < 4){
		arroo[2] = "20"+arroo[2];
		}
	var asEndDate = arroo[2]+"-"+arroo[0]+"-"+arroo[1];

	var ii = jQuery("#checkInDate").val();
    var arrii = ii.split("/");
	if(arrii[0].length < 2){
		arrii[0] = "0"+arrii[0];
		}
	if(arrii[1].length < 2){
		arrii[1] = "0"+arrii[1];
		}
	if(arrii[2].length < 4){
		arrii[2] = "20"+arrii[2];
		}
    var asStartDate = arrii[2]+"-"+arrii[0]+"-"+arrii[1];    
	
	 var miStart = Date.parse(asStartDate.replace(/\-/g, '/'));
	 var miEnd   = Date.parse(asEndDate.replace(/\-/g, '/'));

      if((miEnd-miStart)<=0){
    	  alert("Check-Out date must be later than Check-In date");
    	  jQuery("#checkOutDate").css("color","#CC0000");
    	  jQuery("#checkOutDate").css("font-weight","bold");
    	  return false;
         }else{
            return true;
             }
	
}



function prepareSubmit()
{
	var validatCheckInFormat = checkDate(jQuery("#checkInDate").val());
    var validatCheckOutFormat = checkDate(jQuery("#checkOutDate").val());
	
	jQuery("#checkInDate").css("color","#000000");
	jQuery("#checkInDate").css("font-weight","normal");
	jQuery("#checkOutDate").css("color","#000000");
	jQuery("#checkOutDate").css("font-weight","normal"); 

    if(validatCheckInFormat!=true)
	{
        alert("Invalid Check-In date");
		jQuery("#checkInDate").css("color","#CC0000");
		jQuery("#checkInDate").css("font-weight","bold");

		var today = new Date();

        if(jQuery("#checkInDate").val()=="" || (checkDate(jQuery("#checkInDate").val())==false))
		{
			var myDate = new Date();
			jQuery("#checkInDate").val((myDate.getMonth()+1)+"/"+myDate.getDate()+"/"+myDate.getFullYear().toString().substring(2));
		}
		else if(today > checkInDateInput)
		{
			var ss = jQuery("#checkInDate").val();
			var arrss = ss.split("/");
			jQuery("#checkInDate").val(arrss[0]+"/"+arrss[1]+"/"+(parseInt(arrss[2])+1,10).toString());
			jQuery("#checkOutDate").val(arrss[0]+"/"+arrss[1]+"/"+(parseInt(arrss[2])+1,10).toString());
		}

        reWriteDayOfWeek();	  
	  
    }

    if(validatCheckOutFormat!=true)
	{
        alert("Invalid Check-Out date");
		jQuery("#checkOutDate").css("color","#CC0000");
		jQuery("#checkOutDate").css("font-weight","bold");   
		
		if(jQuery("#checkOutDate").val()=="" || (checkDate(jQuery("#checkOutDate").val())==false))
		{
			var myTorrow = new Date(); 
			myTorrow.setDate(myTorrow.getDate()+1); 
			jQuery("#checkOutDate").val((myTorrow.getMonth()+1)+"/"+myTorrow.getDate()+"/"+myTorrow.getFullYear().toString().substring(2));  
	    }
		else if(today > checkOutDateInput)
		{
			var ss = jQuery("#checkOutDate").val();
			var arrss = ss.split("/");
			jQuery("#checkOutDate").val(arrss[0]+"/"+arrss[1]+"/"+(parseInt(arrss[2])+1,10).toString());
		}
	    
        reWriteDayOfWeek();
    }

    var validationDateResult = false;
    if(validatCheckInFormat && validatCheckOutFormat){
    	validationDateResult = validationForDate();
        }

    var validationLocationResult = true;
   // validationLocationResult = validationForLocation();

    if(validationLocationResult&&validationDateResult&&validatCheckInFormat&&validatCheckOutFormat)
	{
		return true;
	
    	/*var days = Date_Compare();

        var checkInDate = jQuery("#checkInDate").val();
        var arrii = checkInDate.split("/");
    	if(arrii[0].length < 2){
    		arrii[0] = "0"+arrii[0];
    		}
    	if(arrii[1].length < 2){
    		arrii[1] = "0"+arrii[1];
    		}
    	if(arrii[2].length < 4){
    		arrii[2] = "20"+arrii[2];
    		}
        var asStartDate = arrii[2]+arrii[0]+arrii[1];  

           if(jQuery("#hostelLocationGeonamesId").val().length>0){

                //save to cookie
 				var cookieval = '?cid=' + 2
 								+ '&start=' + jQuery("#checkInDate").val()
 								+ '&end=' + jQuery("#checkOutDate").val()
 								+ '&location=' + jQuery("#hotelsLcation").val()
 								+ '&hostelLocationGeonamesId=' + jQuery("#hostelLocationGeonamesId").val();
 							
 				var wid = 1217;
  				
 				setCookie('su_dd_widget_' + wid, cookieval);
                 
 				location.href="http://booking.studentuniverse.com/?geonameId="+jQuery("#hostelLocationGeonamesId").val()+"&wid=1217&aid=9787&d="+asStartDate+"&n="+days+"&cid=2";

                }else{

     				var cookieval = '?cid=' + 2
					+ '&start=' + jQuery("#checkInDate").val()
					+ '&end=' + jQuery("#checkOutDate").val()
					+ '&location=' + ""
					+ '&hostelLocationGeonamesId=' + "";
				
	                var wid = 1217;
	
	                setCookie('su_dd_widget_' + wid, cookieval);
                
            	    jQuery("#locationText").html(jQuery("#hotelsLcation").val());

                    if(jQuery.trim(jQuery("#hotelsLcation").val()).length<3){
                        alert("Please enter a valid Location");
                        }else{
                            jQuery("#locationNotMatch").show();
                            jQuery("#hotelErrMsgClk").css("cursor","pointer");
                        }
                }*/
    }	
	else
	{
		return false;
	}
}