function SetExternalHref(){
  jQuery(function($){
	$("a").each(function(){
		if( this.hostname > '' && 'http:' == this.protocol && ( this.pathname.toLowerCase().indexOf('media(')>-1 || this.pathname.toLowerCase().indexOf('.pdf')>-1 || ( this.hostname.toLowerCase().indexOf(document.location.hostname)==-1 ) ) )
			$(this).attr("target", "_blank");
	});
  });
}

function emailCheck(s, m) {
  if (s.match(new RegExp("^[-._a-zA-Z0-9]+@([-_a-zA-Z0-9]+\\.)+[a-zA-Z][a-zA-Z0-9]+$"))) {
    return true;
  } else {
    alert(m);
    return false;
  }
}

/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
/*                        */
/* Favorite                    */
/*                        */
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
var AddToFavorite;
var RemoveFromFavorite;

function GetFavoriteLength(){
  var FavoriteCookie = GetCookie('Favorite');
  if(FavoriteCookie){
    var CurrentList = FavoriteCookie.split(';');
    return CurrentList.length;
  } else {
    return 0;
  }
}

function SetFavoriteLink(type){
  link = document.getElementById('favorite');
  if(type == 'ADD'){
    link.innerHTML = AddToFavorite;
  } else {
    link.innerHTML = RemoveFromFavorite;
  }
}

function SetStartFavoriteLink(houseId){
  link = document.getElementById('favorite');
  if(!VerifyFavoriteItem(houseId)){
    link.innerHTML = AddToFavorite;
  } else {
    link.innerHTML = RemoveFromFavorite;
  }
}


function AddFavoriteItem(houseId){
  if(!VerifyFavoriteItem(houseId)){
    var FavoriteCookie = GetCookie('Favorite');
    if (!FavoriteCookie){
      SetCookie('Favorite', houseId, SetDate(), false);
    } else {
      var CurrentPath = GetCookie('Favorite');
      SetCookie('Favorite', CurrentPath + ';' + houseId, SetDate(), false)
    }
    SetFavoriteLink('DELETE');
    // FavouritesDocId defined in template
    if(typeof(FavouritesDocId) == 'undefined')
      FavouritesDocId = 58;
    document.location ="/composite-" + FavouritesDocId + ".htm";
  } else {
    DeleteFavoriteItem(houseId);
  }
}

function DeleteFavoriteItem(houseId){
  var FavoriteCookie = GetCookie('Favorite');
  var NewList = '';
  if(FavoriteCookie){
    var CurrentList = FavoriteCookie.split(';');
    for(i=0;i<CurrentList.length;i++){
      if(CurrentList[i] != houseId){
        (NewList!='') ? NewList += ';' : NewList += '';
        NewList += CurrentList[i];
      }
    }
  }
  SetCookie('Favorite', NewList, SetDate(), false);
  SetFavoriteLink('ADD');
}
  
function VerifyFavoriteItem(houseId){
  var FavoriteCookie = GetCookie('Favorite');
  if(FavoriteCookie){
    var CurrentList = FavoriteCookie.split(';');
    for(i=0;i<CurrentList.length;i++){
      if(CurrentList[i] == houseId){
        return true;
      }
    }
  }
  return false;
}


/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
/*                        */
/* Cookie                    */
/*                        */
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
function SetCookie(name, value, expires, isSecure) {
  var CurrentCookie = name + '=' + escape(value) + '; path=/' +
  ((expires) ? '; expires=' + expires.toGMTString() : '') +
  ((isSecure) ? '; secure' : '');
  document.cookie = CurrentCookie;
}

function GetCookie(name) {
  var Cookie = document.cookie;
  var Prefix = name + '=';
  var Begin = Cookie.indexOf('; ' + Prefix);
  if (Begin == -1) {
    Begin = Cookie.indexOf(Prefix);
    if (Begin != 0) return null;
  } else {
    Begin += 2;
  }
  var End = document.cookie.indexOf(';', Begin);
  if (End == -1)
    End = Cookie.length;
  return unescape(Cookie.substring(Begin + Prefix.length, End));
}

function DeleteCookie(name) {
  if (GetCookie(name)) {
    document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT';
  }
}
  
function SetDate(){
  var CurrentDate = new Date();
  RepairDate(CurrentDate);
  return new Date((CurrentDate.getTime() + 365*1000*60*60*24));
}

function RepairDate(date) {
  var Base = new Date(0);
  var Skew = Base.getTime();
  if (Skew > 0)
    date.setTime(date.getTime() - Skew);
}

function GetTomorrow(){
  CurrentDate = new Date()
  if(10 > CurrentDate.getDate()){ Day = '0' + CurrentDate.getDay() } else { Day = CurrentDate.getDate() }
  if(10 > Math.round((CurrentDate.getMonth()+1))){ Month = '0' + CurrentDate.getMonth() } else { Month = Math.round(CurrentDate.getMonth()+1) }
  Year = CurrentDate.getYear();
  return AddDays(FormatDate(Year + '-' + Month + '-' + Day), 1)
}

var NJ = 0, 
    OJ = 1,
    VJ = 2,
    SJ = 3,
    F  = 4,
    SN = 5,
    SS = 6,
    B  = 7;
    
var Regions = ["NJ", "OJ", "VJ", "SJ", "F", "SN", "SS", "B"];

var Hostels = [[],[],[],[],[],[],[],[]];
Hostels[NJ][5] = 15;
Hostels[NJ][8] = 16;
Hostels[NJ][4] = 18;
Hostels[NJ][15] = 19;
Hostels[NJ][2] = 20;
Hostels[NJ][3] = 21;
Hostels[NJ][14] = 22;
Hostels[NJ][7] = 23;
Hostels[NJ][12] = 24;
Hostels[NJ][13] = 29;
Hostels[NJ][11] = 30;
Hostels[NJ][1] = 31;
Hostels[NJ][6] = 33;
Hostels[NJ][10] = 34;
Hostels[NJ][9] = 35;
Hostels[NJ][0] = 58;
Hostels[NJ][0] = 140;
Hostels[SJ][12] = 79;
Hostels[F][9] = 90;
Hostels[OJ][7] = 59;
Hostels[F][5] = 80;
Hostels[B][4] = 122;
Hostels[VJ][6] = 37;
Hostels[VJ][5] = 135;
Hostels[SN][9] = 92;
Hostels[SN][8] = 93;
Hostels[SN][7] = 144;
Hostels[OJ][8] = 39;
Hostels[SJ][6] = 60;
Hostels[SJ][1] = 61;
Hostels[F][7] = 81;
Hostels[SS][8] = 94;
Hostels[VJ][1] = 139;
Hostels[NJ][8] = 16;
Hostels[SN][4] = 95;
Hostels[SJ][5] = 62;
Hostels[SN][3] = 96;
Hostels[OJ][10] = 40;
Hostels[OJ][2] = 41;
Hostels[OJ][5] = 42;
Hostels[B][2] = 123;
Hostels[SJ][7] = 63;
Hostels[SN][2] = 97;
Hostels[VJ][8] = 64;
Hostels[VJ][3] = 44;
Hostels[SN][5] = 146;
Hostels[SN][14] = 0;
Hostels[SN][12] = 99;
Hostels[OJ][11] = 45;
Hostels[VJ][7] = 46;
Hostels[SN][10] = 136;
Hostels[OJ][12] = 47;
Hostels[SN][15] = 101;
Hostels[OJ][6] = 48;
Hostels[F][1] = 82;
Hostels[SS][5] = 103;
Hostels[SJ][4] = 66;
Hostels[SS][1] = 142;
Hostels[SJ][11] = 67;
Hostels[SN][6] = 104;
Hostels[SS][12] = 105;
Hostels[F][10] = 83;
Hostels[SS][15] = 106;
Hostels[SS][7] = 110;
Hostels[SS][11] = 107;
Hostels[SS][14] = 108;
Hostels[SN][13] = 109;
Hostels[F][4] = 86;
Hostels[F][3] = 85;
Hostels[VJ][9] = 68;
Hostels[SJ][13] = 143;
Hostels[OJ][1] = 49;
Hostels[SJ][2] = 69;
Hostels[F][6] = 87;
Hostels[VJ][4] = 50;
Hostels[SS][4] = 112;
Hostels[SJ][8] = 71;
Hostels[OJ][4] = 52;
Hostels[B][5] = 125;
Hostels[SN][11] = 128;
Hostels[SJ][9] = 70;
Hostels[F][11] = 88;
Hostels[SS][13] = 113;
Hostels[OJ][13] = 148;
Hostels[B][1] = 126;
Hostels[OJ][3] = 54;
Hostels[SS][6] = 115;
Hostels[OJ][9] = 55;
Hostels[SS][2] = 116;
Hostels[SJ][15] = 75;
Hostels[SJ][14] = 72;
Hostels[SS][3] = 117;
Hostels[SS][9] = 118;
Hostels[B][3] = 127;
Hostels[F][8] = 89;
Hostels[SN][1] = 120;
Hostels[SJ][10] = 76;
Hostels[VJ][10] = 2070;
Hostels[SJ][3] = 147;
Hostels[OJ][14] = 56;
Hostels[SS][10] = 121;

function selectHostel( R, H ) {
  if( typeof(HostelsDocId) == 'undefined' )
    HostelsDocId = 185;
  if( typeof(Hostels[R][H]) != 'undefined' && Hostels[R][H] > 0 )
    document.location.href = "/composite-" + HostelsDocId + ".htm?HostelId=" + Hostels[R][H] + "&RegionAbb=" + Regions[R];
}

function postCatalogOrderForm(form){
	// send post to external page.
	var url = 'http://danhostel.dk';//'http://danhostel-crm.interlogic.com.ua/CompositeRequests/CatalogRequest.aspx';
	var data = new Hash();
	var strCatalog = '';
	var formElements = form.getElements();
	//debugger;
	for(var i in formElements)
	{
		if (formElements[i].name == '')
			continue;
		if (formElements[i].name == 'catalog')
		{
			if (formElements[i].checked)
			{
				strCatalog += formElements[i].value + ',';
			}
		}
		else
		{
			eval('data.' + formElements[i].name + '=\'' + formElements[i].value + '\';');
		}
	}
	
	data.catalog = strCatalog.substring(0, strCatalog.length-1);

	new Ajax.Request(url ,{
					method: 'get',
					crossSite: true,
					parameters: data.toJSON(),
					onSuccess: function(response){
						var d = document.getElementById("debugInfo");
						if(d)
							d.innerHTML = response.responseText;
					}
				  });
}