/// <reference path="jquery-1.3.1-vsdoc.js" />
/// <reference path="swfobject-vsdoc.js" />
/// <reference path="sifr-vsdoc.js" />


$(document).ready(function() {

var str =  "";
var totalWeight = "";
var totalSum = "";
var sumAfterFreight = 0; 
var freight = 0;
var nUnits = 0;

// If correct form is showing
if(document.getElementById('cust_postnr') != null)
{
	str = document.getElementById('cust_postnr').value;

	totalWeight = document.getElementById('totalWeightHidden').value;
	totalSum = document.getElementById('webshopBasketSumHidden').value;
	country = document.getElementById('country').value;
	nUnits = parseInt(str);

	totalSum = totalSum.substring(0, totalSum.length - 3);

	if(totalSum.length > 3)
	{
		if(totalSum.length > 7)
		{
			totalSum = totalSum.substring(0, totalSum.length - 8) + totalSum.substring(totalSum.length - 7, totalSum.length - 4) + totalSum.substring(totalSum.length - 3, totalSum.length);
		}
		else
		{
			totalSum = totalSum.substring(0, totalSum.length - 4) + totalSum.substring(totalSum.length - 3, totalSum.length);
		}
	}

	// norway ny default
	var country = 1;

	// if sweden is selected
	if(document.getElementById('country').value == "2")
	{
		country = document.getElementById('country').value;
	}


	if(country == 2)
	{
		$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);
	}
	else
	{
		if (nUnits <= 9999 && nUnits > 0 && (document.getElementById('cust_postnr').value).length == 4)
		{
			$('#test').load("FetchCustomer.ashx?CustomerID=" + $('#cust_postnr').val() + "&totalWeight=" + totalWeight + "&totalSum=" + totalSum);
		}
		else
		{
			$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum);
		}
	}
}


$('#country').change(function () {

	// norway ny default
	var country = 1;

	// if sweden is selected
	if(document.getElementById('country').value == "2")
	{
		country = document.getElementById('country').value;
	}

	// if swiden is selected
	if(country == 2)
	{
		if (nUnits <= 99999 && nUnits > 0 && (document.getElementById('cust_postnr').value).length == 5)
		{
			$('#test').load("FetchCustomer.ashx?CustomerID=" + nUnits + "&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);

		}
		else
		{
			$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);
			document.getElementById('cust_postnr').value = "";
		}
	}
	else
	{
		if (nUnits <= 9999 && nUnits > 0 && (document.getElementById('cust_postnr').value).length == 4)
		{
			// if swiden is selected
			$('#test').load("FetchCustomer.ashx?CustomerID=" + nUnits + "&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);

		}
		else
		{
			$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum);
			document.getElementById('cust_postnr').value = "";
		}
	}
} );



$('#cust_postnr').bind("blur", function() {

	if(document.getElementById('cust_postnr') != null)
	{
		str =  document.getElementById('cust_postnr').value;
	}
	else
	{
		return;
	}

	// norway ny default
	var country = 0;

	// if swiden is selected
	if(document.getElementById('country').value == "2")
	{
		country = document.getElementById('country').value;
	}

			
	
	totalWeight = document.getElementById('totalWeightHidden').value;

	totalSum = document.getElementById('webshopBasketSumHidden').value;

	sumAfterFreight = 0; 

	freight = 0;

	var uUnitsFloat = parseFloat(str)

	nUnits = parseInt(uUnitsFloat);

	// if swiden is selected
	if(country == 2)
	{
	}
	else
	{
		// if valid post code
		if(nUnits <= 9999 && nUnits > 0 && (document.getElementById('cust_postnr').value).length == 4)
		{}
		else
		{
			str = 0;
		}
	}
		


	if (str != 0)
	{
		// if swiden is selected
		if(country == 2)
		{
			if (nUnits <= 99999 && nUnits > 0 && (document.getElementById('cust_postnr').value).length == 5)
			{
				$('#test').load("FetchCustomer.ashx?CustomerID=" + nUnits + "&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);

			}
			else
			{
				alert('Ugyldig postnummer');
				$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);
				document.getElementById('cust_postnr').value = "";
			}
		}
		else
		{
			if (nUnits <= 9999 && nUnits > 0 && (document.getElementById('cust_postnr').value).length == 4)
			{
				// if swiden is selected
				$('#test').load("FetchCustomer.ashx?CustomerID=" + nUnits + "&totalWeight=" + totalWeight + "&totalSum=" + totalSum + "&country=" + country);

			}
			else
			{
				alert('Ugyldig postnummer');
				$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum);
				document.getElementById('cust_postnr').value = "";
			}
		}
	}
	else if (str == 0)
	{
		alert('Ugyldig postnummer');
		$('#test').load("FetchCustomer.ashx?CustomerID=0&totalWeight=" + totalWeight + "&totalSum=" + totalSum);
		document.getElementById('cust_postnr').value = "";
	}
	else
	{
		alert('Ugyldig postnummer');
	}
});





/*

	var main_height = $("#div_middle_center").height();
	var right_height = $("#right_doc_list").height();

	var prod_height = $("#prod_item").height();

	if(main_height != null && right_height != null && main_height > right_height)
	{
		document.getElementById('right_doc_list').style.height = main_height + 'px'; 
		document.getElementById('div_snarveier').style.height = main_height + 'px';
	}

	else if(prod_height != null && right_height != null && prod_height < right_height)
	{
		document.getElementById('prod_item').style.height = right_height + 'px'; 
		document.getElementById('div_middle_center').style.height = right_height + 'px';
	}


    var thisImg = "";
    var thisImg2 = "";



    $('.btn-slide').bind("click", function() {
        $(".menu_" + this.id).slideToggle(300);

    });


    $('.div_prodspes_top').bind("click", function() {
        $(".div_sendFor").slideToggle(300);

		if (($('.sendForButton').hasClass('sendForButton_on')))
		{
			$('.sendForButton').removeClass('sendForButton_on');
			$('.div_prodspes_top').addClass('div_prodspes_top_on');
		}
		else
		{
			$('.sendForButton').addClass('sendForButton_on');
			$('.div_prodspes_top').removeClass('div_prodspes_top_on');
		}
    });

    myObject = null;



    $('.dropdown-menu', this).hover(
      function() {




          if (($('.down-list', this).hasClass('isdown') || $('.topMenuImage', this).hasClass('showred')) && (myObject == this)) {

          }

          else {

              myObject = this;


              var img = $('.topMenuImage', this).attr("src");


              thisImg = img;


              if (img.indexOf("_on_") == -1) {

                  img = thisImg.replace(/0/g, "");
                  img = img.replace(/1/g, "");
                  img = img.replace(/2/g, "");
                  img = img.replace(/3/g, "");
                  img = img.replace(/4/g, "");
                  img = img.replace(/5/g, "");
                  img = img.replace(/6/g, "");
                  img = img.replace(/7/g, "");
                  img = img.replace(/8/g, "");
                  img = img.replace(/9/g, "");

                  img = img.substring(0, (img.length - 6));

                  var imageOn = img + "on_" + thisImg.substring(img.length, thisImg.length);

                  $('.topMenuImage', this).attr('src', imageOn);
              }


              $('.topMenuImage', this).addClass('showred');
              $('.down-list', this).addClass('isdown');




              if (this.id == "dropdown") {
                  $('.down-list', this).slideDown(100);
              }
          }
      },
        function() {

            if ($('.down-list', this).hasClass('isdown') && $('.topMenuImage', this).hasClass('showred')) {

                testObj = $('.down-list', this);
                testObj2 = $('.topMenuImage', this);

                if (this.id == "dropdown") {
                    $('.down-list', this).slideUp(100);
                }

                if (thisImg != '') {
                    $('.topMenuImage', this).attr('src', thisImg);
                }

                thisImg = "";


                setTimeout("testObj.removeClass('isdown')", 100);
                setTimeout("testObj2.removeClass('showred')", 100);
            }
        }
    );


    $(".menuItem").hover(
        function() {

            var img = $('.topMenuImage2', this).attr("src");

            thisImg2 = img;

            if (img.indexOf("_on_") == -1) {


                img = thisImg2.replace(/0/g, "");
                img = img.replace(/1/g, "");
                img = img.replace(/2/g, "");
                img = img.replace(/3/g, "");
                img = img.replace(/4/g, "");
                img = img.replace(/5/g, "");
                img = img.replace(/6/g, "");
                img = img.replace(/7/g, "");
                img = img.replace(/8/g, "");
                img = img.replace(/9/g, "");


                img = img.substring(0, (img.length - 6));


                var imageOn = img + "on_" + thisImg2.substring(img.length, thisImg2.length);


                $('.topMenuImage2', this).attr('src', imageOn);
            }
        }
        ,
        function() {

            $('.topMenuImage2', this).attr('src', thisImg2);
        }
    );

*/


});
