/* Global Containers: Should be in sync with the database. */
var HOME_PAGE			= 1;
var SPECIAL_OFFER_PAGE	= 2;
var ACTIVE_BEACH_GREECE	= 4;
var ACTIVE_BEACH_TURKEY	= 8;
var ACTIVE_MOUNTAIN 	= 16;

var currentGroupId='';
var currentGroupTitle='';
var currentGroupIntroText='';
var currentGroupOutroText='';
var groupcount=1;
var urlArray=new Array();
var urlArrayIntro=new Array();
var urlArrayOutro=new Array();
var groupNames=new Array();

/*
   Dummy error handler to catch all the error reported by DWR.
*/
DWREngine.setErrorHandler(DWRErrorHandler);
function DWRErrorHandler(v) {
	return false;
}

// Call backend to get special offer groups.
function getSpecialOfferGroups(displayParam){
	var site_id_hid = document.getElementById('SITEID_SO').value;
	
	if(displayParam == HOME_PAGE) {
		specialOfferFacade.getSpecialOfferGroups(site_id_hid, displayParam, populateSpecialOfferGroups);
	} else if(displayParam == SPECIAL_OFFER_PAGE) {
		specialOfferFacade.getSpecialOfferGroups(site_id_hid, displayParam, populateSpecialOfferGroupsOnOffersPage);
	} else if(displayParam == ACTIVE_BEACH_GREECE || displayParam == ACTIVE_BEACH_TURKEY || displayParam == ACTIVE_MOUNTAIN) {
		var callbackProxy = function(dataFromServer) {
			  processSpecialOfferGroups(dataFromServer, displayParam);
		};
		var callMetaData = { callback:callbackProxy };
		specialOfferFacade.getSpecialOfferGroups(site_id_hid, displayParam, callMetaData);
	}
}

// Call backend to get special offers by country
function getSpecialOffersByCountry(groupId, index, displayParam){
	specialOfferFacade.getSpecialOffersByCountry(groupId, displayParam, populateSpecialOffersByCountry);

	currentGroupId=groupId;
	currentGroupTitle=groupNames[index];
	currentGroupIntroText=urlArrayIntro[index];
	currentGroupOutroText=urlArrayOutro[index];
}

// Call backend to get special offers
function getSpecialOffers(tabId,groupId,displayParam,groupno){
	currentGroupId=groupId;
 // Remove style class from all offer tabs
	anchorslist = document.getElementsByTagName('a');
	for(i=0;i<anchorslist.length;i++) {
	 if (anchorslist[i].id.indexOf('offergrouptab') > -1) {
		anchorslist[i].className = '';
	 }
	}
 // add style class to current tabs
	document.getElementById('offergrouptab' + groupId).className = 'cstabactive';	
	specialOfferFacade.getSpecialOffers(groupId, displayParam, populateSpecialOffers);
	displayTeaserForGroup(groupno);
}

// CALLBACK FUNCTIONS BEGIN - PLEASE PUT ALL BACK END INTERACTION FUNCTIONS ABOVE THIS
// Function to populate special offer groups on special offers page
function populateSpecialOfferGroupsOnOffersPage(specialOfferGroups) {
// specify the special offer to show at page load.
// to use append an anchor to the url with the index of the specialOffer eg. crystalski.co.uk/specialoffers/#2
// it uses a single digit only 0 - 9

if ((specialOfferGroups == null ) || (specialOfferGroups == "" )){

              document.getElementById('specialOfferGroupOutro').innerHTML = '<br/>'+"There are no Special offers at the moment..";
		document.getElementById("rsslink").style.display='none';

		return false;
	} 

	if(specialOfferGroups.length > 0)
	{
	var url = window.location.toString();
	var index = 0;
    if(url.indexOf('?') > -1) {
    index = parseInt(url.substring(url.indexOf('?')+1));
    }

    if (isNaN(index)){ index = 0;}
    if(index >= specialOfferGroups.length) {
    index = 0;
    }

	var text = '';
	for(i = 0; i < specialOfferGroups.length; i++) {
		urlArrayIntro[i]= specialOfferGroups[i].introText;
	    urlArrayOutro[i]= specialOfferGroups[i].outroText;
	    groupNames[i]=specialOfferGroups[i].groupName;
	   
	 	text += '<a class="leftNavLink" href="javascript:getSpecialOffersByCountry(';
		text += "'"+specialOfferGroups[i].groupId+"'";
		text += ',';
		text += "'"+i+"'";
		text += ',';
		text += "'"+SPECIAL_OFFER_PAGE+"'";
		text += ')">';
		text += specialOfferGroups[i].groupName;
		text += '</a>';
	}
	text += '';
	document.getElementById('specialoffersnav').innerHTML = text;
	// Fire off special offers fetch. -
	getSpecialOffersByCountry(specialOfferGroups[index].groupId, index, SPECIAL_OFFER_PAGE);
	}
	else
	{
		document.getElementById('specialOfferGroupOutro').innerHTML = '<br/>'+"There are no Special offers at the moment..";
		document.getElementById("rsslink").style.display='none';
	}
}

// This function populates the special offers by country on special offers page.
function populateSpecialOffersByCountry(specialOffersByCountry){
var SITE_ID = document.getElementById('SITEID_SO').value;
document.getElementById('specialOfferGroupTitle').innerHTML	= currentGroupTitle;
document.getElementById('specialOfferGroupIntro').innerHTML	= currentGroupIntroText;
document.getElementById('specialOfferGroupOutro').innerHTML	= currentGroupOutroText;

var text = '';
for (i = 0; i < specialOffersByCountry.length; i++)
{
	urlArray[i]= new Array();
	var specialOfferCountryList = specialOffersByCountry[i];

	
	text += '<div class="bodytext"><br></div>';
	text += '<div class="bodytext"><b>';
	text += specialOfferCountryList[0].country;
	text += '</b></div>';
	text += '<table class="xTableTBL" cellspacing="0">';
	text += '<tbody>';
	text += '<tr style="background: #6D2B8F; color: #fff;" align="left">';
	text += '<td></td>';
	text += '<td style="color:#fff"><b>DepartureDate</b></td>';
	text += '<td style="color:#fff"><b>Nights</b></td>';
	text += '<td style="color:#fff"><b>Resort</b>';
	text += '</td><td style="color:#fff">';
	text += '<b>Accommodation</b></td>';
	text += '<td style="color:#fff"><b>Rating</b></td>';
	text += '<td style="color:#fff"><b>Board</b></td>';
	text += '<td style="color:#fff"><b>Price</b></td>';
	text +='<td></td></tr>';
	for (j = 0; j < specialOfferCountryList.length ; j++ )
		{
		
	  var spurl ='';
	  var k = j+1;
      spurl = '/crystallakes_ieSelectSpecialOffer.do?';
	  spurl += 'siteId=';
	  spurl +=  SITE_ID;
	  spurl += '&packageType=OfferOnly';
	  spurl += '&departureDate='+specialOfferCountryList[j].departureDate;
	  spurl += '&country='+specialOfferCountryList[j].country;
	  spurl += '&countryCode='+specialOfferCountryList[j].countryCode;
	  spurl += '&resortCode='+specialOfferCountryList[j].resortCode;
	  spurl += '&accomCode='+specialOfferCountryList[j].accomCode;
	  spurl += '&nights='+specialOfferCountryList[j].nights;
	  spurl += '&board='+specialOfferCountryList[j].board;
	  spurl += '&groupId='+currentGroupId;	  
	  spurl += '&resort='+encodeURIComponent(specialOfferCountryList[j].resort);
	  spurl += '&accommodation='+encodeURIComponent(specialOfferCountryList[j].accommodation);

      urlArray[i][j+1] = spurl;
      var accommRating = specialOfferCountryList[j].accommodationRating;

		text += '<tr class="xTableTR1">';
		text += '<td class="xTableTD2" align="center"></td>';
		text +='<td class="xTableTD1" align="left" height="20">';
		text += getFormattedDate(specialOfferCountryList[j].departureDate);
		text += '</td>';
		text += '<td class="xTableTD2" align="left">';
		text += specialOfferCountryList[j].nights;
		text += 'nights';
		text += '</td>';
		text += '<td class="xTableTD2" align="left">';
		text += stringInitCaps(specialOfferCountryList[j].resort);
		text += '</td>';
		text += '<td class="xTableTD2" align="left">';
		text += stringInitCaps(specialOfferCountryList[j].accommodation);
		text += '</td>';
		if(accommRating == null || accommRating == '')
		{
			text += '<td class="xTableTD2" align="center">-</td>';
		}
		else
		{
			text += '<td class="xTableTD2" align="right">';
			text += accommRating;
			text += '&nbsp;<img src="/images/crystalRating.gif"/>&nbsp;&nbsp;</td>';
		}
		text += '<td class="xTableTD2" align="left">';
		text += specialOfferCountryList[j].board;
		text += '</td>';
		text += '<td style="color: red;" class="xTableTD2" align="left"> &euro;';
		text += specialOfferCountryList[j].price;
		text += '</td>';
		text += '<td align="center">';
		text += '<a href="javascript:submitForm('+i+','+k+'';
		
		text += ','+'\''+specialOfferCountryList[j].resortCode+'\'';
		text += ','+'\''+specialOfferCountryList[j].accomCode+'\'';
		text += ')")';
		 text += 'class="currencyColor">';
		text += 'Go<img alt="" src="/images/refine-arrow-closed.gif" border="0" height="8" width="8">';
		text += '</a></td>';
		text += '</tr>';
		}
	text += '</tbody>';
	text += '</table>';
}
document.getElementById('specialOffers').innerHTML = text;
}

// Function to populate special offer groups
function populateSpecialOfferGroups(specialOfferGroups) {

	if (specialOfferGroups == null ) {
		return false;
	} 

	if(specialOfferGroups.length > 0)
	{
	var text = '';
	text += '<a id="offergrouptab';
	text += specialOfferGroups[0].groupId;
	text += '"';
	text += ' href="javascript:getSpecialOffers(\'offergrouptab'+specialOfferGroups[0].groupId+'\',';
	text += '\''+specialOfferGroups[0].groupId+'\'';
	text += ',';
	text += '\''+HOME_PAGE+'\'';
	text += ',' + groupcount + ')"';
	text += ' class="cstabactive" >';
	text += specialOfferGroups[0].groupName;
	text += ' </a>';

	for(i = 1; i < specialOfferGroups.length; i++) {
		groupcount++;
		text += '<a';
		text += ' href="javascript:getSpecialOffers(\'offergrouptab'+specialOfferGroups[i].groupId+'\',';
		text += '\''+specialOfferGroups[i].groupId+'\'';
		text += ',';
		text += '\''+ HOME_PAGE +'\'';
		text += ',' + groupcount + ')"';
		text += ' id="offergrouptab';
		text += specialOfferGroups[i].groupId;
		text += '">';
		text += specialOfferGroups[i].groupName;
		text += '</a>';
	}
	document.getElementById('offernav').innerHTML = text;

	// Fire off special offers fetch.
	getSpecialOffers('offergrouptab'+specialOfferGroups[0].groupId, specialOfferGroups[0].groupId, HOME_PAGE,1);
	}
	else
	{
		document.getElementById("csoffers").style.display='none';
	}
}

// Function to populate special offers
function populateSpecialOffers(specialOffers) {
var SITE_ID = document.getElementById('SITEID_SO').value;
var text='<table border="0" cellpadding="3" cellspacing="0">';
text += '<tbody><div class="bodytext"><tr><th>Depart</th><th>Country</th>';
text += '<th>Resort</th><th>Accommodation</th><th>Rating</th>';
text += '<th align="center">Nights</th><th align="center">Prices From</th></tr></div>';

	for(j = 0; j < specialOffers.length; j++) {	

	
	if ((j % 2) == 0)	
	{
	 text += '<tr class="oddrow">';
	}
	else
	{
	 text += '<tr class="evenrow">';
	}

      
      var spurl = '/crystallakes_ieSelectSpecialOffer.do?';
	  spurl += 'siteId=';
	  spurl +=  SITE_ID;
	  spurl += '&packageType=OfferOnly';
	  spurl += '&departureDate='+specialOffers[j].departureDate;
	  spurl += '&country='+specialOffers[j].country;
	  spurl += '&countryCode='+specialOffers[j].countryCode;
	  spurl += '&resortCode='+specialOffers[j].resortCode;
	  spurl += '&accomCode='+specialOffers[j].accomCode;
	  spurl += '&nights='+specialOffers[j].nights;
	  spurl += '&board='+specialOffers[j].board;
	  spurl += '&groupId='+currentGroupId;	  
	  spurl += '&resort='+encodeURIComponent(specialOffers[j].resort);
	  spurl += '&accommodation='+encodeURIComponent(specialOffers[j].accommodation);
	 
	 
      urlArray[j] = spurl;
      var accommRating = specialOffers[j].accommodationRating;
   	 
      text += '<td><p class="offersText">' + getFormattedDate(specialOffers[j].departureDate) + '</td>';
      text += '<td><p class="offersText">' + specialOffers[j].country + '</td>';
      text += '<td><p class="offersText">' + stringInitCaps(specialOffers[j].resort) + '</td>';
      text += '<td><p class="offersText">' + stringInitCaps(specialOffers[j].accommodation) + '</td>';
      if(accommRating == null || accommRating == '')
      {
		text += '<td align="center"><p class="offersText">-</p></td>';      	
      }
      else
      {
      	text += '<td align="right"><p class="offersText">' + accommRating + '&nbsp;<img src="/images/crystalRating.gif"/></p></td>';
      }
      text += '<td align="center"><p class="offersText">' + specialOffers[j].nights + '</td>';
      text += '<td align="center"><p class="offersText">';

	  text += '<b>'
	  text += '<a href="javascript:submitForm(\''+ j+'\',\'\'';
	  text += ','+'\''+specialOffers[j].resortCode+'\'';
	  text += ','+'\''+specialOffers[j].accomCode+'\'';         
	  text += ')" ';
	  text += 'class="currencyColor">';
	  text += '&euro;' + specialOffers[j].price;
	  text += '</a></b>'
	  text += '&nbsp;</p>';
	  text += '</td>';

	  text += '</tr>';
    
    
    
    }
	text += '<tr><td><a href="./rss.shtml"><img src="./images/rss.gif" border="0"></a></td><td colspan="6" align="right"><p class="offersText"><b><a class="spOfferLink" href="./specialoffers/specialoffers.html">Click here for our full range of special offers<img alt="" src="images/refine-arrow-closed.gif" border="0"></a></b></p></td></tr>';

    text += '</tbody></table>';
	
	document.getElementById("specialOffersSection").innerHTML = text;
  }

//Submit the form
function submitForm(urlindex1,urlindex2, counres,accom){

if (urlindex2 == '')

{

	this.document.spoffers.action=urlArray[urlindex1];
} else{
	
	this.document.spoffers.action=urlArray[urlindex1][urlindex2];
}
    	
	this.document.spoffers.method='POST';
	this.document.getElementById("countryList").value = counres ;
	this.document.getElementById("accommodationList").value = accom ;
	this.document.spoffers.submit();
}

// Return the formatted date.
function getFormattedDate(date) {
var firstDash = date.indexOf("-");
var secondDash = date.lastIndexOf("-");
var year = date.substring(0,firstDash);
var month = date.substring(firstDash+1,secondDash);
var day = date.substring(secondDash+1);

var monthName='';

switch(month)
{
case '1':
case '01':
	monthName="Jan"
	break
case '2':
case '02':
	monthName="Feb"
	break
case '3':
case '03':
	monthName="Mar"
	break
case '4':
case '04':
	monthName="Apr"
	break
case '5':
case '05':
	monthName="May"
	break
case '6':
case '06':
	monthName="Jun"
	break
case '7':
case '07':
	monthName="Jul"
	break
case '8':
case '08':
	monthName="Aug"
	break
case '9':
case '09':
	monthName="Sep"
	break
case '10':
	monthName="Oct"
	break
case '11':
	monthName="Nov"
	break
case '12':
	monthName="Dec"
	break

}

return day+' '+monthName+' '+year;
}
function stringInitCaps(sourceString)
{
var str1=sourceString.charAt(0);
var str2=sourceString.substring(1,sourceString.length).toLowerCase();
return(str1.concat(str2))
}
function displayTeaserForGroup(groupno)
{
   for (i=1; i<=10; i++)
   {
      if (i == groupno) {
           document.getElementById("cmscontent").rows[i-1].style.display=""; 
      }else{
           document.getElementById("cmscontent").rows[i-1].style.display="none"; 
      }
   }
}