///////////////////////////////////////////////////////////////////////
//Sample script for pulling selection criteria from marketing web site
//which is passed to the IRM and put in an iframe
///////////////////////////////////////////////////////////////////////

function IFrame(parentElement)
{
   // Create the iframe which will be returned
   var iframe = document.createElement("iframe");
 
   // If no parent element is specified then use body as the parent element
   if(parentElement == null)
      parentElement = document.body;
 
   // This is necessary in order to initialize the document inside the iframe
   parentElement.appendChild(iframe);
 
   // Initiate the iframe's document to null
   iframe.doc = null;
 
   // Depending on browser platform get the iframe's document, this is only
   // available if the iframe has already been appended to an element which
   // has been added to the document
   if(iframe.contentDocument)
      // Firefox, Opera
      iframe.doc = iframe.contentDocument;
   else if(iframe.contentWindow)
      // Internet Explorer
      iframe.doc = iframe.contentWindow.document;
   else if(iframe.document)
      // Others?
      iframe.doc = iframe.document;
 
   // If we did not succeed in finding the document then throw an exception
   if(iframe.doc == null)
      throw "Document not found, append the parent element to the DOM before creating the IFrame";
 
   // Create the script inside the iframe's document which will call the
   iframe.doc.open();
   iframe.doc.close();
 
   // Return the iframe, now with an extra property iframe.doc containing the
   // iframe's document
   return iframe;
}

function createIframe (iframeName, width, height) {
var iframe;
if (document.createElement && (iframe =
document.createElement('iframe'))) {
iframe.name = iframe.id = iframeName;
iframe.width = width;
iframe.height = height;
iframe.src = 'about:blank';
document.body.appendChild(iframe);
}
return iframe;
}
// construct the IRM url including all search criteria and dates
// source the irm iframe with this url
function showAlert()
{
	alert("kjkjkjk");
	var frmObj=document.getElementById('form1');
}

function init(){
		document.getElementById('loading').style.display='none';
		}
   
	//var frameUrl = "http://irm.resortdata.com/irmnet/res/resmain.aspx?resort=96&";
	var frameUrl ="http://irm.resortdata.com/irmnet/res/resmain.aspx?";
	var getUrl = window.location.href;
	var position = getUrl.indexOf("?");
	position = position + 1;
	var getString = getUrl.slice(position);
	var getItem = getString.split("&");
	var i = 0;
	var frameUrlPath = frameUrl;
	while(i < getItem.length){
		if (frameUrlPath != frameUrl){
			frameUrlPath = frameUrlPath + "&";
			}
		frameUrlPath = frameUrlPath + getItem[i];
		i++;
		}

function callIRM()
{
   var frmObj=document.getElementById('form1')
   
   var url = "http://irm.resortdata.com/irmnet/res/resmain.aspx?";
   var foundOne = false;
      
   //Add guest requets if selected
   //These must correspond to GuestRequest1-9 values
   var location = frmObj.ddLocation.value;
   if (location != "*")
   {
      url = url + "Request2=" + location;
      foundOne = true;
   }
   var bedrooms = frmObj.ddBedrooms.value;
   if (bedrooms != "*") 
   {
       if (foundOne) url = url + "&";
       url = url + "Request1=" + bedrooms;
       foundOne = true;
   }
   var pricerange = frmObj.ddPrice.value;
   if (pricerange != "*")
   {
      if (foundOne) url = url + "&";
      url = url + "Request3=" + pricerange;
      foundOne = true;
   }
   var smoking = frmObj.ddSmoking.value;
   if (smoking != "*")
   {
      if (foundOne) url = url + "&";
      url = url + "Request4=" + smoking;
      foundOne = true;
   }
  
   //Add arrival and departure dates if selected 
   var arrival = frmObj.wdcArrival_input.value;
   var departure = frmObj.wdcDeparture_input.value;
   if (arrival != "") 
   {
      //verify that arrival choice is not in the past
      if (new Date(arrival) < now)
      {
         alert("Arrival date must be later than today.\nPlease change your calendar dates and try again.");
         return false;
      }
      
      //for demo convert year to 1998
      var arrDate = arrival.split("/");
	  arrival = arrDate[0]+"/"+arrDate[1]+"/1998";

      if (departure != "") 
      {
         //for demo convert year to 1998 (or 1999 if applicable)
         var depDate = departure.split("/");
         if (arrDate[0] == "12" && depDate[0] != "12")
         { departure =  depDate[0]+"/"+depDate[1]+"/1999"; }
         else
         { departure =  depDate[0]+"/"+depDate[1]+"/1998"; }
            
      
         //verify departure > arrival
         var nights = calcNights(arrival, departure);
         if (nights <= 0 )
         {
            alert("Departure date must be later than arrival date.\nPlease change your calendar dates and try again.")
            return false;
         }
         //additional checking
        // if (nights > 10)   // for example enforce a maximum stay
       //  {
       //     alert("Online Reservations cannot be made for more than a 10 night stay.\nPlease change your calendar dates, or call us about your extended stay.\nThank You");
      //      return false;
      //   }
      }
      
      if (foundOne) url = url + "&";
      url = url + "Arrival=" + arrival;
      foundOne = true;      
      if (departure != "") 
      { url = url + "&Departure=" + departure; }
      else   // use default number of nights
      { url = url + "&Nights=" + numberNights; }
      
      //Add people selections
      url = url + "&People1=" + frmObj.ddAdults.value;
      var children = frmObj.ddChildren.value;
      if (children != "0") url = url + "&People2=" + children;
   }
   
   //Nothing was selected, get all rooms
   if (!foundOne) url = url + "AllRooms=true";
   //alert(url);   
   
   // put IRM.net in the iframe
   //var irmFrame = document.getElementById('irmFrame');
  // alert(irmFrame);
   // put IRM.net in the iframe
  // var data = document.getElementById('showData');
  // data.innerHTML = url;
   //parent.irmFrame.location.href = url;
  // frmObj.action=url;
  // frmObj.submit();
   ////////////////code for dynamic iframe ////////////////////
  // var canvas = document.getElementById("canvas");
  // var iframe = new IFrame(canvas);
  //  var div = iframe.doc.createElement("div");
	//div.style.width = "700px"; 
	//div.style.height = "650px";
	//div.style.border = "solid 1px #000000";
	//div.innerHTML = "Hello IFrame!";
//	iframe.doc.body.appendChild(div);
//	iframe.doc.height=window.frames["iframe"].document.body.scrollHeight;
//	iframe.src=url;
///////////////////////////////////////
var canvas = document.getElementById("canvas");
//var iframe = new IFrame(canvas);
// var iframe = createIframe ('iframe0', 300, 300);
// iframe.src=url;
// document.getElementById("formdiv").style.visibility = "hidden";
 canvas.innerHTML='<iframe src='+url+' width="775" height="1000" marginwidth="0" marginheight="0" frameborder="no" scrolling="yes" style="background-color:#F9EBC7;" allowtransparency="true"></iframe>';


////////////////////////////////////////
   // OR open a new window for the IRM  
   //window.open(url,'_new','height=900, width=850, scrollbars=yes, resizable=yes');
   
}  //callIRM

// caclulate number of nights from selected arrival and departure dates
function calcNights(arrival, departure)
{   
    var arrDate = arrival.split("/");
    var depDate = departure.split("/");
   	var newArrDate = new Date(arrDate[0]+"/"+arrDate[1]+"/"+arrDate[2]);
	var newDepDate = new Date(depDate[0]+"/"+depDate[1]+"/"+depDate[2]);
	var numNights = (Math.round((newDepDate-newArrDate)/86400000));
	return numNights;
}		

/////////////////User defined function for redirection////////////////////
function callUserIRM()
{
   var frmObj=document.getElementById('form1')
   
  // var url = "http://www.signitysolutions.com/demo/jart/index.php?option=com_content&view=article&id=47&";
//  var url = "http://www.iegdesign.com/tuscana.net/searchresult?";
  var url = "http://www.tuscana.net/searchresult?";
   var foundOne = false;
      
   //Add guest requets if selected
   //These must correspond to GuestRequest1-9 values
 /*  var location = frmObj.ddLocation.value;
   if (location != "*")
   {
      url = url + "Request2=" + location;
      foundOne = true;
   }
   var bedrooms = frmObj.ddBedrooms.value;
   if (bedrooms != "*") 
   {
       if (foundOne) url = url + "&";
       url = url + "Request1=" + bedrooms;
       foundOne = true;
   }
   var pricerange = frmObj.ddPrice.value;
   if (pricerange != "*")
   {
      if (foundOne) url = url + "&";
      url = url + "Request3=" + pricerange;
      foundOne = true;
   }
   var smoking = frmObj.ddSmoking.value;
   if (smoking != "*")
   {
      if (foundOne) url = url + "&";
      url = url + "Request4=" + smoking;
      foundOne = true;
   }
  */
   //Add arrival and departure dates if selected 
   var arrival = frmObj.wdcArrival_input.value;
   var departure = frmObj.wdcDeparture_input.value;
   if (arrival != "") 
   {
      //verify that arrival choice is not in the past
      if (new Date(arrival) < now)
      {
         alert("Arrival date must be later than today.\nPlease change your calendar dates and try again.");
         return false;
      }
      
      //for demo convert year to 1998
      var arrDate = arrival.split("/");
	 // arrival = arrDate[0]+"/"+arrDate[1]+"/1998";

      if (departure != "") 
      {
         //for demo convert year to 1998 (or 1999 if applicable)
         var depDate = departure.split("/");
         if (arrDate[0] == "12" && depDate[0] != "12")
         { 
         //departure =  depDate[0]+"/"+depDate[1]+"/1999"; 
         }
         else
         { 
         //departure =  depDate[0]+"/"+depDate[1]+"/1998"; 
         }
            
      
         //verify departure > arrival
         var nights = calcNights(arrival, departure);
         if (nights <= 0 )
         {
            alert("Departure date must be later than arrival date.\nPlease change your calendar dates and try again.")
            return false;
         }
         //additional checking
//         if (nights > 10)   // for example enforce a maximum stay
//         {
//            alert("Online Reservations cannot be made for more than a 10 night stay.\nPlease change your calendar dates, or call us about your extended stay.\nThank You");
 //           return false;
  //       }
      }
      
      if (foundOne) url = url + "&";
      url = url + "Arrival=" + arrival;
      //alert(arrival);
      foundOne = true;      
      if (departure != "") 
      { url = url + "&Departure=" + departure; }
      else   // use default number of nights
      { url = url + "&Nights=" + numberNights; }
      
      //Add people selections
      url = url + "&People1=" + frmObj.ddAdults.value;
      var children = frmObj.ddChildren.value;
      if (children != "0") url = url + "&People2=" + children;
   }
   
   //Nothing was selected, get all rooms
   if (!foundOne) url = url + "AllRooms=true";
   //alert(url);   
   
    
   window.location.href = url;
  
}

function init(){
		document.getElementById('loading').style.display='none';
		}
 function sendIRM()
 {
	//var frameUrl = "http://irm.resortdata.com/irmnet/res/resmain.aspx?";
	var frameUrl ="http://res.tuscana.net/IRMNet/Res/ResMain.aspx?";
	
	var getUrl = window.location.href;
	var position = getUrl.indexOf("?");
	position = position + 1;
	var getString = getUrl.slice(position);
	var getItem = getString.split("&");
	var i = 0;
	var frameUrlPath = frameUrl;
	while(i < getItem.length){
	if (frameUrlPath != frameUrl){
		frameUrlPath = frameUrlPath + "&";
		}
	frameUrlPath = frameUrlPath + getItem[i];
	i++;
	}
	//alert(frameUrlPath);
	var canvas = document.getElementById("canvas");
	 canvas.innerHTML='<iframe src='+frameUrlPath+' width="850" height="1100" marginwidth="0" marginheight="0" frameborder="no" scrolling="no" style="background-color:#F9EBC7;" allowtransparency="true" class="wrapper"></iframe>';
}
//////////////////////////////////////////
// Supporting date variables and functions
//////////////////////////////////////////
// Get today's current date. 
var now = new Date();

// Array list of days.
//var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');

// Calculate four digit year.
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
	}
	

//Get actual values for date and month from DOM, For security purposes of demo year is not set to current//
var nowdate = 01;   //now.getDate();
var nowmonth = 2;  //now.getMonth(); // 2 = March see months array above//
var nowyear = 1998;

//Default Days ahead of todays date for arrival and departure set to current dates//
//var arr = 2;
//var dep = 4;

//For use with demo -will set the default departure date to x number of days ahead of arrival - remove or comment out for current date functionality;
var arr = 0;
var dep = 2;



//For use with current dates this function Adds 2 days for default Arrival Date & 4 days for default Departure Date.//
//For Demo use this function just returns proper format for default arrival and departure dates//
function checkDate (m, d, y, field){
	//var maxDays = DaysInMonth(m, y);
	var newDay = d + field;
	var newMonth = months[m];
	var newYear = fourdigits(y);
	//if ((newDay) > maxDays){
//		newDay = newDay - maxDays;
//		var nextMonth = m + 1;
//		if (nextMonth > 11){
//			nextMonth = 0;
//			newYear++;
//			}
//		newMonth = months[nextMonth];
//		}		
//	newDay = ((newDay<10) ? "0" : "")+ newDay;
	var fieldDate = newMonth + "/" +
					newDay + "/" +
					newYear;
	return fieldDate;
	}

// default arrival and departure dates	
var arrivalDate = checkDate(nowmonth, nowdate, nowyear, arr);
var departureDate = checkDate(nowmonth, nowdate, nowyear, dep);
//sets default number of nights
var numberNights = calcNights(arrivalDate, departureDate); 



