/**
 * Some functions for the webshop
**/

var wsDelay = 0;

function AddToCart ( pid, qty )
{
	var j = new cAjax ();
	j.open ( 'get', BaseUrl()+'katalog/?action=addtocart&pid='+pid+'&qty='+qty, true );
	j.onload = function ()
	{
		var n = document.createElement ( 'div' );
		n.innerHTML = this.responseText ();
		$('ShoppingCart').parentNode.replaceChild ( n, $('ShoppingCart') );
		CartPosition ();
	}
	j.send ();
}

function RemoveFromCart ( pid )
{
	if ( $('ShoppingCart') )
		AlterQuantity ( pid, '0' );
	else
	{
		var pelement = $('ShoppingCartDetailed');
		var eles = pelement.getElementsByTagName ( 'input' );
		for ( var a = 0; a < eles.length; a++ )
		{
			if ( eles[a].getAttribute ( 'rel' ) == pid )
				eles[a].value = 0;
		}
		UpdateAllQuantities ();
	}
}

function EmptyCart ()
{
	if ( confirm ( i18n ( 'Are you sure?' ) ) )
	{
		var j = new cAjax ();
		j.open ( 'get', BaseUrl()+'katalog/?action=emptycart', true );
		j.onload = function ()
		{
			var n = document.createElement ( 'div' );
			n.innerHTML = this.responseText ();
			$('ShoppingCart').parentNode.replaceChild ( n, $('ShoppingCart') );
			CartPosition ();
		}
		j.send ();
	}
}

function AlterQuantity ( pid, qty, callback )
{
	if ( !callback ) callback = false;
	var j = new cAjax ();
	j.open ( 'get', BaseUrl()+'katalog/?action=alterquantity&pid='+pid+'&qty='+qty, true );
	j.callback = callback;
	j.onload = function ()
	{
		if ( this.callback )
		{
			this.callback();
		}
		else
		{
			var n = document.createElement ( 'div' );
			n.innerHTML = this.responseText ();
			$('ShoppingCart').parentNode.replaceChild ( n, $('ShoppingCart') );
			CartPosition ();
		}
	}
	j.send ();
}

function CheckUpdateAllQtys( e ) 
{
	if ( e.which == 13 )
		UpdateAllQuantities ();
}

function UpdateAllQuantities ( )
{
	var sc, eles;
	if ( !( sc = $('ShoppingCart') ) )
		sc = $('ShoppingCartDetailed');
	if ( eles = sc.getElementsByTagName ( 'input' ) )
	{
		wsDelay = eles.length;
		for ( var a = 0; a < eles.length; a++ )
		{
			var info = eles[a].getAttribute ( 'rel' );
			AlterQuantity ( 
				info, 
				eles[a].value, 
				function ()
				{ 
					if ( --wsDelay == 0 ) 
					{
						document.location.reload();
					}
				} 
			);
		}
	}
}

function CartPosition ()
{
	var st = window.pageYOffset;
	var sc = $('ShoppingCart');
	if ( sc )
	{
		var sht = sc.offsetTop;
		if ( !sc.getAttribute ( 'originaly' ) )
			sc.setAttribute ( 'originaly', sht );
		var osht = parseInt ( sc.getAttribute ( 'originaly' ) );
		
		var off = osht-st;
		if ( off < 0 )
			sc.style.top = (0-(off))+'px';
		else sc.style.top = '0px';
	}
}

function Checkout ()
{
	document.location = BaseUrl()+'katalog/checkout.html';
}

// Restore form values
function RecallCheckoutForm ()
{
	var s = $('CheckoutForm');
	if ( s )
	{ 
		var inputs = s.getElementsByTagName ( 'input' );
		for ( var a = 0; a < inputs.length; a++ )
		{
			var val = GetCookie ( 'order_' + inputs[a].name );
			if ( val.length > 0 )
				inputs[a].value = val;
		}
		var sels = s.getElementsByTagName ( 'select' );
		for ( var a = 0; a < sels.length; a++ )
		{
			var val = GetCookie ( 'order_' + sels[a].name );
			if ( val.length > 0 )
			{
				var opts = sels[a].getElementsByTagName ( 'option' );
				for ( var b = 0; b < opts.length; b++ )
				{
					if ( opts[b].value == val )
						opts[b].selected = "selected";
					else opts[b].selected = "";
				}
			}
		}
	}
}

// Check form and send order
function FinalizeCheckout ()
{
	var checkout = $('CheckoutForm');
	var frm = new Object ();
	var inputs = checkout.getElementsByTagName ( 'input' );
	for ( var a = 0; a < inputs.length; a++ )
		frm[inputs[a].name] = inputs[a];
	
	// Control form
	var important = [ 'Name', 'Telephone', 'Email', 'Address', 'Zip', 'City', 'Country' ];
	var sizes = [ 2, 8, 5, 3, 4, 2, 2 ];
	var names = [ 'navnet ditt', 'telefon nummer', 'e-post adresse', 'adresse',
					'postnummer', 'poststed', 'land' ];
	for ( var a = 0; a < important.length; a++ )
	{
		if ( !frm[important[a]] ) 
		{
			continue;
		}
		if ( frm[important[a]].value.length < sizes[a] )
		{
			alert ( 'Du er nødt å fylle inn ' + names[a] + '.' );
			frm[important[a]].focus ();
			return false;
		}
	}
	var emailmistake = false;
	var eml = frm.Email.value.split ( '@' );
	if ( !eml[1] ) emailmistake = true;
	else 
	{
		eml[1] = eml[1].split ( '.' );
		if ( !eml[1][1] ) emailmistake = true;
	}
	if ( emailmistake )
	{
		alert ( 'Du må skrive inn e-post adressen rett.' );
		frm.Email.focus ();
		return false;
	}
	
	// Remember the order form values
	for ( var a = 0; a < inputs.length; a++ )
	{
		SetCookie ( 'order_' + inputs[a].name, inputs[a].value );
	}
	
	// Send vars to server
	var j = new cAjax ();
	j.open ( 'post', BaseUrl()+'katalog/?action=sendorder' );
	var inputs = checkout.getElementsByTagName ( 'input' );
	for ( var a = 0; a < inputs.length; a++ )
	{
		j.addVar ( inputs[a].name, inputs[a].value );
	}
	j.onload = function ()
	{
		document.body.style.cursor = 'default';
		document.getElementById ( 'Right' ).style.cursor = 'default';
		var response = this.responseText ().split ( '<!--SEPARATE-->' );
		if ( response[0] == 'ok' )
		{
			document.location = response[1];
		}
		else alert ( 'Noe gikk galt med bestillingen:' + this.responseText() );
	}
	j.send ();
	document.body.style.cursor = 'wait';
	document.getElementById ( 'Right' ).style.cursor = 'wait';
}


AddEvent ( 'onscroll', CartPosition );
AddEvent ( 'onload', RecallCheckoutForm );



