/**
 * This document should only contain scripts that are unique for just this site
 */

if (typeof Panagora != 'object' || typeof Panagora == 'undefined')
	var Panagora = {};
	
if (typeof console != 'object')
	var console = { log: function() {}, info: function() {} }

var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	if($j.browser.mozilla && $j.browser.version <= "1.9"){
		//general stuff
		$j('#content').css({'left' : '-76px'});
	}	
	
	//alert($j.browser.msie);
	if($j.browser.msie && $j.browser.version == "6.0"){
		//alert($j.browser.version);
		$j('#altshipping').click(function(){
			$j('#footer').css({'bottom' : '0'});
		});
	}
	//site-specific jQuery-scripts goes in here..
	
	$j('#newsletter').keypress(function(e){
		if(e.which == 13) {
			if (this.value != "" && this.value != this.defaultValue && isValidEmail(this, errorMessage)) {
				location.href = "/system/user/newsletter_save.asp?newsletter="+this.value;
			}
		}
	});
	$j('#newsletter').focus(function(e){
		if (this.value == this.defaultValue)
			this.value = ''; 
	});
	$j('#newsletter').blur(function(e){
		if (this.value == '') 
			this.value = this.defaultValue;
	});

	$j('#newsletter-signup-button').click(function(e){
		e.preventDefault();
		var nObj = $j("#newsletter");
		console.log(nObj)
		if (nObj[0].value != "" && nObj[0].value != nObj[0].defaultValue && isValidEmail(nObj[0], errorMessage)){
			location.href = "/system/user/newsletter_save.asp?newsletter="+ nObj[0].value;
		}
	});

	function getAddressFromKreditor(e)
	{
		//e.preventDefault();
		e.cancelBubble = true;
		var errorTimeout;
		var pno = $j("#personnummer").val();
		var country = $j("#hidden-country").val();
		//var country = document.
		
		if(pno.length>0 && country.length==2) {
			$j.ajax({
				url: "/system/cart/kreditor_get_address.asp?pno="+pno+"&country="+country,
				success: function(json) {
					if (json instanceof Array) {
						$j("#register-info").show();
						$j("#firstname").val(json[0].firstName);
						$j("#lastname").val(json[0].lastName);
						$j("#address").val(json[0].streetAddress);
						$j("#zip-code").val(json[0].zipCode);
						$j("#city").val(json[0].city);
						$j("#register-email").focus();
					}
				},
				error: function() {
					if (errorTimeout)
						clearTimeout(errorTimeout);
					$j('#address-fetch-error').show();
					errorTimeout = setTimeout(function () { $j('#address-fetch-error').fadeOut(); }, 2000);
				},
				dataType: 'json'
			});
			return false;
		}
	}

	$j('#get-address-link').click(getAddressFromKreditor);
});
