var vid2price = {};

function changeDestinationSelection(destination) {
  var curOption = document.getElementById( destination );
  curOption.selected = true;
}

function parse_date(ds)
{
    var parts = ds.match(/^(....)-(..)-(..)$/);
    if (!parts) {
	return null;
    }

    var date = new Date();
    date.setFullYear(parts[1], parts[2]-1, parts[3]);
    return date;
}

function price_format(price)
{
    price = ""+price.toFixed(2);
    price = price.replace(/\./, ",");
    price = price.replace("-", "- ");
    price = price.split("").reverse().join("");
    price = price.replace(/(\d\d\d)/,"$1 ");
    price = price.split("").reverse().join("");
    return currency+" "+price;
}

function get_price(vid, qty)
{
    var plist = vid2price[vid];
    var price = plist[1];

    for (var i in plist) {
	if (qty >= parseInt(i)) {
	    price = plist[i];
	}
    }
    
    /* Display price */
    $("#price_"+vid).html(price_format(price));

    return price;
}

function prodsum_recalc()
{
    var total = 0;
    $('#product-form .qty').each(function (idx, e) {
	var match = e.id.match(/.*_([0-9]+)_([0-9]+)/);
	var vid = match[2];
	total += get_price(vid, e.value) * e.value;
    });
    
    if (total > 0) {
	$('.only_if_prods').show();
    } else {
	$('.only_if_prods').hide();
    }

    $('#totals').html(price_format(total));
}

function sim_pkg_change(el)
{
    /* Get variant id */
    var match = el.id.match(/.*_([0-9]+)_([0-9]+)/);
    var pid = match[1];
    var vid = match[2];

    var qty = $(el).val();

    $('#prod-extra'+pid).hide();
    $('#prod-extra'+pid).load("/simpkg-extra.html", 
			      { "product_id" : pid,
				"variant_id" : vid,
				"qty" : qty },
			      function() {
				  $('#prod-extra'+pid).slideDown("slow");
				  prodsum_recalc();
			      });
}


function check_purch_form(form)
{
    var st = true;

    var trip_date = parse_date($(form.trip_date).val());
    if (trip_date == null) {
	alert("Vänligen ange avresedatum som ÅÅÅÅ-MM-DD");
	return false;
    }

    var return_date = parse_date($(form.return_date).val());
    if (return_date == null) {
	alert("Vänligen ange hemkomstdatum som ÅÅÅÅ-MM-DD");
	return false;
    }

    if (!(trip_date < return_date)) {
	alert("Hemkomstdatum måste vara efter avresedatum");
	return false;
    }
    
    var today = new Date();
    if($(form.TYPE).val() == 1) { //SIM
      for(var i = 0; i < 1; i ++) {
        today.setDate(today.getDate()+1);
        if (today.getDay() == 0 || today.getDay() == 6) i--;
      }
      if (trip_date < today) {
        alert("Ring oss på 08-40026160 och kolla om vi hinner leverera extra snabbt. Om vi bekräftat att vi hinner leverera, så sätt bara avresedag en dag extra fram.");
        return false;
      }
      trip_date.setDate(trip_date.getDate()+180);
      if (trip_date < return_date) {
        alert("Tjänsten kan normalt sett beställas i max 180 dagar. Om du behöver längre giltighetstid, vänligen kontakta oss så ordnar vi det.");
        return false;
      }
    } else { //NoSIM
      trip_date.setDate(trip_date.getDate()+90);
      if (trip_date < return_date) {
        alert("Tjänsten kan beställas max i 90 dagar");
        return false;
      }
    }


    $(".phoneno").each(function(i, e) {
	var pno = $(e).val();
	var match = pno.match(/^(\+467|07)/);
	if (st && !match) {
	    alert("Felaktigt telefonnummer. Vänligen ange ditt telefonnummer i internationellt format. T.ex +4670111111. Om du inte vill göra någon vidarekoppling kan du ange +4670 som nummer.");
	    st = false;
	}
    });
    if(st == true) {

      
      /* CLICKINC */
          var AffiliateId = 0;
                var ReferralURL = "-";
                var Promotion = 0;
                var AffiliateReferral = "-";
                var SubCampaign = 0;

                if (GetCookie(varb.asm) !== null) {
        AffiliateId = Sanitize(GetCookie(varb.asm));
      }
      if (GetCookie(varb.asmReferral) !== null) {
        ReferralURL = Sanitize(GetCookie(varb.asmReferral));
      }
      if (GetCookie(varb.prom) !== null) {
        Promotion = Sanitize(GetCookie(varb.prom));
      }
      if (GetCookie(varb.affreferral) !== null) {
        AffiliateReferral = Sanitize(GetCookie(varb.affreferral));
      }
      if (GetCookie(varb.sub) !== null) {
        SubCampaign = Sanitize(GetCookie(varb.sub));
      }
      form.action = "https://se.holidayphone.com/cart.html?affId="+AffiliateId+"&asmReferral="+ReferralURL+"&affreferral="+AffiliateReferral+"&promotion="+Promotion+"&sub="+SubCampaign;
      _gaq.push(['_linkByPost', form]);
    }
    return st;
}
function quickPurchase(form) {
      /* CLICKINC */
          var AffiliateId = 0;
                var ReferralURL = "-";
                var Promotion = 0;
                var AffiliateReferral = "-";
                var SubCampaign = 0;

      if (GetCookie(varb.asm) !== null) {
        AffiliateId = Sanitize(GetCookie(varb.asm));
      }
      if (GetCookie(varb.asmReferral) !== null) {
        ReferralURL = Sanitize(GetCookie(varb.asmReferral));
      }
      if (GetCookie(varb.prom) !== null) {
        Promotion = Sanitize(GetCookie(varb.prom));
      }
      if (GetCookie(varb.affreferral) !== null) {
        AffiliateReferral = Sanitize(GetCookie(varb.affreferral));
      }
      if (GetCookie(varb.sub) !== null) {
        SubCampaign = Sanitize(GetCookie(varb.sub));
      }
      form.action = "https://se.holidayphone.com/cart.html?affId="+AffiliateId+"&asmReferral="+ReferralURL+"&affreferral="+AffiliateReferral+"&promotion="+Promotion+"&sub="+SubCampaign;
      _gaq.push(['_linkByPost', form]);
    
    return true;
}

function show_fr(we)
{
    var cont = $(we).parents(".fragecontainer");
    cont.find(".fraga").each(function (i, e) {
	if ($(e).hasClass('OPEN')) {
	    $(e).slideUp("slow");
	    $(e).removeClass('OPEN');
	} else {
	    $(e).slideDown("slow");
	    $(e).addClass('OPEN');
	}
    });
}

function buyform_submit(form)
{
    if (form.category_id.value == "other") {
	form.action = "/kontantkort-i-utlandet.html";
    } else {
	form.action = "/products.html";
    }

    return true;
}

function buyform_change_country(newLoc) {
  var selectedCountry = newLoc.options[newLoc.selectedIndex].value;
  if (selectedCountry == "dk") window.location="http://www.holidayphone.dk";
  if (selectedCountry == "se") window.location="http://www.holidayphone.se";
  if (selectedCountry == "de") window.location="http://www.holidayphone.de";
  if (selectedCountry == "uk") window.location="http://www.holidayphone.co.uk";
  if (selectedCountry == "fi") window.location="http://www.holidayphone.fi";
  if (selectedCountry == "us") window.location="http://www.holidayphone.com";  
}

