$(document).ready(function(){

        $('#order').validate();

       $(".required").each(function(){
          $('#' + this.id).rules("add", {
           required: true,
           messages: {
               required: "Obligatoriskt fält"
           }
        });
       });


       $(".email").each(function(){
        $('#' + this.id).rules("add",{
         email:true,
         messages:{
            email:"Ogiltig e-postadress"
         }
       });
       });

       
       $("#toConfirm").click(function(){
         if($('#order').valid()){
         
            $('#frmToConfirm').val('submit');
            $("#order").submit();		
          }
       });


	$("#accordion").accordion({ header: "h3" });

	if($.cookies.get("shop_vat")=="incl"){
		setVat('incl');
	} else {
		if($.cookies.get("shop_vat")=="excl"){
			setVat('excl');
		} else {
			//Do modal
			$("#divChoseVat").dialog({
				resizable: false,
				height:140,
				modal: true,
				buttons: {
					'Exkl. moms': function() {
						$(this).dialog('close');
						setVat('excl');
					},
					'Inkl. moms': function() {
						$(this).dialog('close');
						setVat('incl');
					}
				}
			});
		}
	}
});

function swapPlan(prEx, prIn){
  if((prEx==-1)&&(prIn==-1)){
    $('#divPriceTagAbb').fadeOut('fast',function(){$('#divPriceTag').fadeIn('fast');});
  } else {
    $('#divPriceTag').fadeOut('fast',function(){
      $('#divPriceTagAbb').fadeOut('fast', function(){
        $('#divPriceTagAbb span.priceIncl span').html(prIn);
        $('#divPriceTagAbb span.priceExcl span').html(prEx);
        $('#divPriceTagAbb').fadeIn('fast');
      });
    });
  }
}

function setVat(val){
  $.get("/customerspecific/svenskatele/ajax.asp", {'val':val, 'action':'setvat', 'rnd':Math.random()}, function(response){
    if(response=='ok incl'){
      $('body').removeClass('priceVAT');
      $('body').addClass('priceVATInclusive');
      $.cookies.set("shop_vat", "incl", {expiresAt:new Date(2100,1,1)});
    }
    else{
      $('body').addClass('priceVAT');
      $('body').removeClass('priceVATInclusive');
      $.cookies.set("shop_vat", "excl", {expiresAt:new Date(2100,1,1)});
    }

  });
}

function setNumber(val){
  if(val){
    $("#dt_strOrgOrPersNr").rules("remove","required");
    $("#dt_strOrgOrPersNr").attr("disabled","disabled");
    $("#dt_strOrgOrPersNr").css({'background-color': '#ddd'});
    $("#dt_strOrgOrPersNr").val();
  }
  else{
      $("#dt_strOrgOrPersNr").rules("add",{
         required:true,
         messages:{
            required: "Skriv in ditt person/organisationsnummer"
         }
      });
      $("#dt_strOrgOrPersNr").removeAttr("disabled");
      $("#dt_strOrgOrPersNr").css({'background-color':'#fff'});
  }
}


function setMobile(val, id){
   if(val){
     $("#" + id + "_mobile").rules("remove","required");
     $("#" + id + "_mobile").attr("disabled","disabled");
     $("#" + id + "_mobile").css({'background-color': '#ddd'});
     $("#" + id + "_mobile").val('');
   }
   else{
      $("#" + id + "_mobile").rules("add", {
         required: true,
         messages:{
            required: "Skriv in det nummer du vill portera(flytta)"
         }
      });
     $("#" + id + "_mobile").removeAttr("disabled");
     $("#" + id + "_mobile").css({'background-color': '#fff'});
   }
}

function getAbonnemang(val){
  var rnd =  (Math.random())
  if(val)
    $('#divAbonnemang').load('/customerspecific/svenskatele/ajax.asp?action=abb&rnd=' + rnd + '&val=' + val);
}


function search(){
    val = $('#searchText').val();
      if(val.length > 2){
		$.ajax({
			type:	"get",
			url:	'/customerspecific/svenskatele/ajax.asp',
			data:	({'action':'search', 'val': val, 'rnd':Math.random()}),
			success:function(response){
                               $('#loader').css({'display':'none'});
			       $('#divSearch').html(response);
                               $('#divSearch').css({'display':'block'});
			},
			beforeSend:function(){
                                   $('#loader').css({'display':'block'});
                                   $('#divSearch').css({'display':'none'});
			},
			error:function(){
				alert('Error');
			}
		});
      } else {
         $('#divSearch').fadeOut('fast');
      }
if(val.length ==0)
  $('#divSearch').fadeOut('fast');
  $('#divSearch').html('');
}

function search2(e){
  val = $('#txtProductSearch').val();
  if(val.length>2){
    if(val=='Sök produkt') val = '*';
    smart = $('#cbTouchscreen').attr('checked');
    gps= $('#cbGPS').attr('checked');
    wifi= $('#cbWifi').attr('checked');
    speaker= $('#cbSpeaker').attr('checked');
    bluetooth= $('#cbBluetooth').attr('checked');
    if ((smart)||(gps)||(wifi)||(speaker)||(bluetooth)) {somethingchecked = true} else {somethingchecked = false}
    if((!somethingchecked)&&(val=='*')){
        alert('Du måste markera någon egenskap om du inte anger sökord.');
    } else {
	$.ajax({
		type:	"get",
		url:	'/customerspecific/svenskatele/ajax2.asp',
		data:	({'action':'search', 'val': val, 'smart': smart,'gps': gps,'wifi': wifi,'speaker': speaker, 'bluetooth': bluetooth,'rnd':Math.random()}),
		success:function(response){
                       $('#loader').css({'display':'none'});
		       $('#divSearch').html(response);
                       $('#divSearch').css({'display':'block'});
		},
		beforeSend:function(){
                        $('#loader').css({'display':'block'});
                        $('#divSearch').css({'display':'none'});
		},
		error:function(){
			alert('Error');
		}
	});
     }
  } else {
    if(val.length<3) alert('Du måste ange minst tre tecken för att söka\neller lämna tomt för att söka på allt.');
    $('#divSearch').fadeOut('fast');
    $('#divSearch').html('');
  }
}
