/*
	tryggsaker.js
	
	This is a JavaScript file that contains small utility functions for the tryggsaker template.
	It requires the prototype library as a prerequsite.
	
	Written by Erik Lundgren 2009-07-10
*/

function drawPopup( tag, noclose )
{
	ajaxLoadOn();	
	
	new Ajax.Request( "/ajax.php?c=getpanel&id="+tag, { 
			onFailure: function() {
				alert( "Misslyckades med AJAX anrop!" );
			},			
			onSuccess: function( transport ) {
				ajaxLoadOff();
				
				if( !$('ajaxPopupWindow'))
				{
					// Doesn't exist yet, we need to create it
					d = new Element( 'div' );
					d.addClassName( 'ajaxWindow' );
					d.style.display = "none";
					d.id = 'ajaxPopupWindow';
					d.style.width = "335px";
					
					if( !noclose )
						d.style.height = "200px";
					else
						d.style.height = "200px";
												
					// Attach it to the end of the body
					b = $$('body');
					b[0].insert( d );
				}
				
				data = '<div class="ajaxWindowHeader"></div>'+'<div class="ajaxWindowContent">'+transport.responseText+'</div>';
				
				if( !noclose )
					data = data+'<div class="ajaxWindowBottom"><input type="button" name="St&auml;ng" value="St&auml;ng" onclick="ajaxWindowDown();" /></div>';
				
				$('ajaxPopupWindow').innerHTML = data;
				showWindow( $('ajaxPopupWindow'));
				
				$('transparencyLayer').observe( 'click', ajaxWindowDown );
			}
		});	
}

function ajaxWindowDown()
{
	hideWindow( $('ajaxPopupWindow'));	
}

function showWindow( ref )
{
	w = document.viewport.getWidth();
   h = document.viewport.getHeight();

	a=document.viewport.getScrollOffsets();
	w2 = a[0];
	h2 = a[1];
	
	w3 = w+w2;
	h3 = h+h2;
	
	// Before showing the popup dialog window we'll place a layer in between to gray out the background to avoid confusion
	if( !$('transparencyLayer'))
	{
		d = new Element( 'div' );
		d.style.display = "none";
		d.style.position = "absolute";
		d.style.top = 0;
		d.style.left = 0;
		d.id = 'transparencyLayer';
		d.style.background="#FFFFFF";
		
		b = $$('body');
		b[0].insert( d );
	}
	
	// Find the height of the current document (not the viewport)
	b = $$('body')[0];
	
	$('transparencyLayer').style.zIndex = 100;
	$('transparencyLayer').style.height = b.getHeight()+'px';
	$('transparencyLayer').style.width = b.getWidth()+'px';
	$('transparencyLayer').setOpacity( 0.7 );
	$('transparencyLayer').style.display = "block";
	
	t = h2 + h/2 - (ref.getHeight()/2);
	l = w2 + w/2 - (ref.getWidth()/2);
	
	ref.style.top = t+"px";
	ref.style.left = l+"px";
	ref.style.display = "block";
	ref.style.zIndex = 200;
}

function hideWindow( ref )
{
	ref.style.display = "none";
	$('transparencyLayer').style.display = "none";
}

function reloadProduct( e )
{
	prodno=resolveAssocs();
	
	ajaxLoadOn();
	
	if( parseInt( prodno ) >= 1000 )
		window.location.href = "/product.php/"+prodno;
}

function resolveAssocs( noninteractive )
{	
	elems = $$('input,select');
	el1=null;
	el2=null;
	el3=null;
	
	for( i=0; i < elems.length; i++ )
	{
		if( elems[i].name == "associd1" )
			el1 = elems[i];

		if( elems[i].name == "associd2" )
			el2 = elems[i];

		if( elems[i].name == "associd3" )
			el3 = elems[i];
	}
			
	// The above took care of the simple case, however if there are multiple options on this page we may have to spend a little more time
	// to figure out the true case. However first handle the case when there is only one...
	if( el1 && !el2 && !el3 )
		return el1.value;
		
	// Ok, it is the more complex case - we don't handle the triple selection case here so we only have to care about two...	
	x=0;
	result=[];
	
	if( el1 )
	{
		ee1=el1.value.split(',');
		x=1;
	}
	else
		ee1=[];
		
	if( el2 )
	{
		ee2=el2.value.split(',');
		x=2;
	}
	else
		ee2=[];
	
	if( el3 )
	{
		ee3=el3.value.split(',');
		x=3;
	}
	else
		ee3=[];
		
	ea=[];
	
	for( i=0; i < ee1.length; i++ )
	{
		n = ee1[i];
		
		if( ea[n] )
			ea[n]++;
		else
			ea[n] = 1;
	}

	for( i=0; i < ee2.length; i++ )
	{
		n = ee2[i];
		
		if( ea[n] )
			ea[n]++;
		else
			ea[n] = 1;
	}

	for( i=0; i < ee3.length; i++ )
	{
		n = ee3[i];
		
		if( ea[n] )
			ea[n]++;
		else
			ea[n] = 1;
	}

	for( i=0; i < ea.length; i++ )
		if( ea[i] )
		{
			if( ea[i] == x )
				result.push( i );
		}
		
	if( result.length == 1 )
		return result[0];

	if( noninteractive==0 )
		alert( "Den kombination av val som du gjort med t.ex. modell, storlek eller färg finns inte eller är inte tillgänglig.\n\nVänligen välj en annan kombination." );	

	return 0;
}

function IsNumeric( strString )
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if( strString.length == 0 )
		return false;

	for( i=0; i < strString.length && blnResult == true; i++ )
	{
		strChar = strString.charAt(i);

		if (strValidChars.indexOf(strChar) == -1)
			blnResult = false;
	}
			
	return blnResult;
}

function ajaxLoadOn()
{
	d = document.getElementById( 'ajaxLoader' );

	w = window.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
   h = window.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);

	t = h/2 - 60;	
	l = w/2 - 175;
  
	d.style.top = t+"px";
	d.style.left = l+"px";
	d.style.display = "block";
	
	// Block the "next/continue" button
//	document.getElementById( 'buttonOrder' ).disabled=true;
}

function ajaxLoadOff()
{
	d = document.getElementById( 'ajaxLoader' );
	d.style.display = "none";	
//	document.getElementById( 'buttonOrder' ).disabled=false;
}

function currencyChange()
{
	currLoc=window.location.href.split( "\?" );
	window.location = currLoc[0]+"?cmd=setcurrency&currencyid="+$F('currency-box');
}

function langChange()
{
	currLoc=window.location.href.split( "\?" );
	window.location = currLoc[0]+"?cmd=setlang&lang="+$F('lang-box');
}

function assocChange( el )
{
	type=0;
	loadElement=null;
			
	for( a=el.element(); a.parentNode; a = a.parentNode )
	{
		if( a.className == 'catalog-list' )
		{
			type=1;
			break;
		}		
	
		if( a.nodeName == 'LI' )
			loadElement = a;			
	}

	if( !type )
		window.location = "/product.php/"+$F('prodno');
	else
	{
		new Ajax.Request( '/ajax.php', { 
								parameters: { c: 'drawsnippet',
								id: 'categoryProduct',
								a: el.element().value },
								onSuccess: function( transport ) {
									loadElement.innerHTML = transport.responseText;
									
									e = document.getElementsByName( "prodno" );
	
									for( a=0; a < e.length; a++ )
										if( e[a].nodeName == "SELECT" )
											$(e[a]).observe( 'change', assocChange );
											
									allPageTags=document.getElementsByTagName("*");  
	
									for( i=0; i < allPageTags.length; i++ )
										if( allPageTags[i].className=='notify' )
										{
											Element.extend( allPageTags[i] );
											allPageTags[i].observe( 'click', productNotify );
										}
								}
							});
	}
}

function displaycountChange1()
{
	currLoc=window.location.href.split( "\?" );
	window.location = currLoc[0]+"?prodcount="+$F('displaycount1');	
}

function displaycountChange2()
{
	currLoc=window.location.href.split( "\?" );
	window.location = currLoc[0]+"?prodcount="+$F('displaycount2');	
}

function starHandler( ev )
{
	rating=0;
	
	switch( ev.element().className )
	{
		case 'one-star':
			rating=1;
			break;
			
		case 'two-stars':
			rating=2;
			break;

		case 'three-stars':
			rating=3;
			break;

		case 'four-stars':
			rating=4;
			break;

		case 'five-stars':
			rating=5;
			break;
	}

	if( rating > 0 )
	{
		ajaxLoadOn();

		new Ajax.Request( '/ajax.php?ajax=1&c=setvote&id='+$F('prodno')+'&a='+rating, { 
			onSuccess: function( transport ) {
				ajaxLoadOff();
				
				response = transport.responseText.split( "\n" );
						
				if( response[0] != "OK:OK" )
					alert( "Det gick inte att registrera din röst "+transport.responseText );
				else
					alert( "Din röst är registrerad" );
			}
		});
	}
}

function fetchAddr( ev )
{
	pno = $F('qcPno');
			
	if( pno.length != 11 && pno.length != 10 )
	{
		alert( "Ogiltigt personnummer, måste anges som xxxxxx-yyyy" );
		return;
	}
	
	if( pno.length == 10 )
	{
		n1=pno.substr( 0, 6 );
		n2=pno.substr( 6, 4 );
		pno = n1+'-'+n2;
		$('qcPno').value = pno;
	}
	else
	{
		n = pno.split( "-" );
		n1=n[0];
		n2=n[1];
	}
			
	if( n1.length != 6 || !IsNumeric( n1 ))
	{
		alert( "Ogiltigt personnummer, måste anges som xxxxxx-yyyy" );
		return;
	}

	if( n2.length != 4 || !IsNumeric( n2 ))
	{
		alert( "Ogiltigt personnummer, måste anges som xxxxxx-yyyy" );
		return;
	}

	if( $F('usertype') == 'company' )
		rstr = '/ajax.php?ajax=1&c=getaddresses&business=1&id='+pno;
	else
		rstr = '/ajax.php?ajax=1&c=getaddresses&id='+pno;

	ajaxLoadOn();
	
	new Ajax.Request( rstr, { 
			onSuccess: function( transport ) {
				ajaxLoadOff();
				
				response = transport.responseText.split( "\n" );
						
				if( response[0] == "OK:OK" )
				{
					if( $F('usertype') == 'company' )
					{
						$('company').value = response[1];
						$('firstname').value = "";
						$('lastname').value = "";
					}
					else
					{
						$('firstname').value = response[1];
						$('lastname').value = response[2];
					}
			
					$('address').value = response[3];
					$('zipcode').value = response[4];
					$('city').value = response[5];
					
					for( i=0; i < $('qcCountry').length; i++ )
					{
						if( $('qcCountry').options[i].value == response[6] )
							$('qcCountry').selectedIndex = i;									
					}					
				}
				else
					alert( "Det gick inte att hämta information för detta personnummer, kontrollera att du valt privatperson/företag rätt" );
			}
		});
}

function initCashierHooks()
{
	if( $('qcDynamic'))
	{
		allInputs=$('qcDynamic').getElementsByTagName("INPUT");		

		for( i=0; i < allInputs.length; i++ )
		{
			if( allInputs[i].name=='paymentmethod' )
			{
				Element.extend( allInputs[i] );
				allInputs[i].observe( 'click', qcPaymentChange );
			}
		
			if( allInputs[i].name=='freightmethod' )
			{
				Element.extend( allInputs[i] );
				allInputs[i].observe( 'click', qcFreightChange );
			}
		}
	}
	
	if( $('qcashierAddressPane'))
	{
		allInputs=$('qcashierAddressPane').getElementsByTagName("SELECT");		

		for( i=0; i < allInputs.length; i++ )
			if( allInputs[i].name=='country' )
			{
				Element.extend( allInputs[i] );
				allInputs[i].observe( 'change', qcCountryChange );
			}
	}
	
	if( $('qcDeliveryOther'))
		$('qcDeliveryOther').observe( 'click', qcDeliveryChange );
		
	if( $('adPreviousSelect'))
		$('adPreviousSelect').observe( 'change', adPreviousSelectFunc );		
}

function doQAjaxCall( ev, ajaxType )
{
	ajaxLoadOn();
	
	new Ajax.Request( '/ajax.php?ajax=1&c=set'+ajaxType+',drawsnippet&id='+ev.element().value+',qcDynamic', { 
				onSuccess: function( transport ) {
					ajaxLoadOff();
					
					// Check the return status, the first three characters must be 'OK:' or an error has occured
					if( transport.responseText.substring( 0, 3 ) != 'OK:' )
					{
						alert( "The "+ajaxType+" method could not be changed (comm error).\nData returned: "+
									transport.responseText.substring( 0, transport.responseText.indexOf( '\n' )));
					}
					else
					{
						$('qcDynamic').innerHTML = transport.responseText.substring( transport.responseText.indexOf( '\n' ));
						initCashierHooks();
					}
				}
			});	
}

function qcCountryChange( ev )
{
	doQAjaxCall( ev, 'country' );
}

function qcPaymentChange( ev )
{
	doQAjaxCall( ev, 'payment' );
}

function qcFreightChange( ev )
{
	doQAjaxCall( ev, 'freight' );
}

function qcDeliveryChange( ev )
{
	if( $('qcDeliveryAddressBox'))
	{
		if( $('qcDeliveryOther').checked  )
			$('qcDeliveryAddressBox').style.display = "block";
		else
			$('qcDeliveryAddressBox').style.display = "none";
	}
}

/*
function productNotify( ev )
{	
	relem=ev.element().up( '.popUpInfo' ).down( '.popUpNotify' );	
	selem=relem.down( 'select' );
	
	if( !selem )
		relem = relem.down( 'input' );
	else
		relem = selem;
	
	if( relem == null )
		return;
		
	ajaxLoadOn();
		
	new Ajax.Request( '/ajax.php?ajax=1&c=drawsnippet&id=notifyPopup&a='+$F(relem), { 
				onSuccess: function( transport ) {
					notifyWidth = 370;
					notifyHeight = 146;
					
					ajaxLoadOff();
		
					middleX = document.viewport.getWidth()/2;
					middleY = document.viewport.getHeight()/2;
					newX = Math.round(middleX-(notifyWidth/2));
					newY = Math.round(middleY-(notifyHeight/2));
					
					// Now add the scroll offset to ensure it can be seen within our viewport
					newX = newX + document.viewport.getScrollOffsets().left;
					newY = newY + document.viewport.getScrollOffsets().top;
					
					newX = newX+"px";
					newY = newY+"px";
		
					$('notifyPopup').style.left = newX;
					$('notifyPopup').style.top = newY;
					$('notifyPopup').style.zIndex = 200;
					$('notifyPopup').innerHTML = transport.responseText;
					$('notifyPopup').style.display = 'block';
					
					notifyInit();
				}
			});
	
	return false;
}
*/
function productNotify( ev )
{
	var prodno=0;
	
	// First assume we're on the product details page
	relem=ev.element().up( '.product-box' );	
	
	if( relem )
		relem = relem.select( 'input,select' );
		
	if( !relem )
		alert( "Must be on a category page, fail!" );
		
	for( i=0; i < relem.length && !prodno; i++ )
	{
		if( relem[i].name == "prodno" )
			prodno=relem[i].value;
			
		if( relem[i].name == "associd1" )
			prodno=resolveAssocs( 1 );			
	}
	
	if( prodno == 0 )
		return;
		
	ajaxLoadOn();

	new Ajax.Request( '/ajax.php?ajax=1&c=drawsnippet&id=notifyPopup&a='+prodno, { 
				onFailure: function( transport ) {
					alert( "AJAX Call Failure..." );
				},
				onSuccess: function( transport ) {
					notifyWidth = 370;
					notifyHeight = 146;
					
					ajaxLoadOff();

					middleX = document.viewport.getWidth()/2;
					middleY = document.viewport.getHeight()/2;
					newX = Math.round(middleX-(notifyWidth/2));
					newY = Math.round(middleY-(notifyHeight/2));
					
					// Now add the scroll offset to ensure it can be seen within our viewport
					newX = newX + document.viewport.getScrollOffsets().left;
					newY = newY + document.viewport.getScrollOffsets().top;
					
					newX = newX+"px";
					newY = newY+"px";

					$('notifyPopup').style.left = newX;
					$('notifyPopup').style.top = newY;
					$('notifyPopup').style.zIndex = 200;
					$('notifyPopup').innerHTML = transport.responseText;
					$('notifyPopup').style.display = 'block';

					notifyInit();
				}
			});
	
	return false;
}

function notifyInit()
{
	if( $('notifyPopupPane'))
	{
		$('notifyPopupOk').observe( 'click', notifySend );
		$('notifyPopupCancel').observe( 'click', notifyDown );
	}
}

function notifyDown()
{
	$('notifyPopup').style.zIndex = -100;
	$('notifyPopup').style.display = "none";
}

function notifySend()
{
	new Ajax.Request( '/ajax.php?ajax=1&c=productnotify&e='+$F( 'notifyEmail' )+'&id='+$F( 'notifyProdno' ), { 
				onSuccess: function( transport ) {
					response = transport.responseText.split( ":" );
				
					if( response[0] != "OK" )
						alert( "Det gick inte att lägga till e-post adressen, kontrollera att du skrivit rätt.\n\nServern svarade: "+response[1] );
					else
					{
						notifyDown();
						alert( "Tack för visat intresse.\nDu kommer att få ett mail så snart varan åter finns i lager." );
					}
				}
			});
}

function initPage()
{
	var n = document.getElementById("category");
	if (n)
	{
		var lis = n.getElementsByTagName("li");
		for (var i = 0; i < lis.length; i++)
		{
			if (lis[i].getElementsByTagName("ul").length)
			{
				var a = lis[i].getElementsByTagName("a").item(0);
				if (a)
				{
					a.onclick = function ()
					{
						var p = this.parentNode;
						if (p.className.indexOf("active") != -1)
							p.className = p.className.replace("active", "");
						else
							p.className += " active";
							
						return true;
					}
				}
			}
		}
	}
}

function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{		
		if (sets[i].className.indexOf("tabset") != -1)
		{			
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if(c)
					{
						if(links[j].className.indexOf("active") != -1)
							c.style.display = "block";
						else
							c.style.display = "none";
					}

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}

function addBonusProduct( w )
{
	var prodno=0;
	var blpid=0;
	
	// We need to locate which product to add, fortunately that is not too hard - we walk up one level in the DOM tree and
	// then scan downwards again for a select or input field named 'prodno'
	// Begin by extending the current event object	
	Element.extend( w );
	
	elems = w.up().up().select( '.f3 input', '.f3 select' );
	
	for( i=0; i < elems.length; i++ )
	{
		if( elems[i].name == "prodno" )		
			prodno = elems[i].value;	

		if( elems[i].name == "blpid" )
			blpid = elems[i].value;	
	}
	
	ajaxLoadOn();

	new Ajax.Request( '/ajax.php', { 
							parameters: { c: 'bonusadd,drawsnippet',
							id: blpid+',qcDynamic',
							a: prodno+',' },
							onFailure: function () {
								alert( "AJAX anrop misslyckades!" );
							},
							onSuccess: function( transport ) {
										ajaxLoadOff();
					
										// Check the return status, the first three characters must be 'OK:' or an error has occured
										if( transport.responseText.substring( 0, 3 ) != 'OK:' )
										{
											alert( "Ett fel uppstod i anropet mot servern, mottagen feltext: "+
															transport.responseText );
										}
										else
										{
											$('qcDynamic').innerHTML = transport.responseText.substring( transport.responseText.indexOf( '\n' ));
											initCashierHooks();
										}									
								}
						});
}

function ajaxCartAdd( prodno )
{
	ajaxLoadOn();

	new Ajax.Request( '/ajax.php', { 
							parameters: { c: 'add,drawsnippet',
							id: prodno+',qcDynamic',
							a: '1,' },
							onFailure: function () {
								alert( "AJAX anrop misslyckades!" );
							},
							onSuccess: function( transport ) {
										ajaxLoadOff();
					
										// Check the return status, the first three characters must be 'OK:' or an error has occured
										if( transport.responseText.substring( 0, 3 ) != 'OK:' )
											alert( "Ett fel uppstod i anropet mot servern, mottagen feltext: "+transport.responseText );
										else
										{
											$('qcDynamic').innerHTML = transport.responseText.substring( transport.responseText.indexOf( '\n' ));
											initCashierHooks();
										}									
								}
						});
}

function ajaxCartRemove( prodno )
{
	ajaxLoadOn();

	new Ajax.Request( '/ajax.php', { 
							parameters: { c: 'remove,drawsnippet',
							id: prodno+',qcDynamic',
							a: '1,' },
							onFailure: function () {
								alert( "AJAX anrop misslyckades!" );
							},
							onSuccess: function( transport ) {
										ajaxLoadOff();
					
										// Check the return status, the first three characters must be 'OK:' or an error has occured
										txt = transport.responseText.substring( 0, 3 );
										
										if( txt != 'OK:' && txt != 'RD:' )
											alert( "Ett fel uppstod i anropet mot servern, mottagen feltext: "+transport.responseText );
										else
										{
											if( txt == "RD:" )
											{
												response = transport.responseText.substring( 0, transport.responseText.indexOf( '\n' ));
												elems = response.split( ':' );
												loc = elems[1]+':'+elems[2];
												window.location.href = loc;
											}
											else
											{
												$('qcDynamic').innerHTML = transport.responseText.substring( transport.responseText.indexOf( '\n' ));
												initCashierHooks();
											}
										}									
								}
						});
}

function ajaxCartBonusRemove( blpid )
{
	ajaxLoadOn();

	new Ajax.Request( '/ajax.php', { 
							parameters: { c: 'bonusdel,drawsnippet',
							id: blpid+',qcDynamic',
							a: '1,' },
							onFailure: function () {
								alert( "AJAX anrop misslyckades!" );
							},
							onSuccess: function( transport ) {
										ajaxLoadOff();
					
										// Check the return status, the first three characters must be 'OK:' or an error has occured
										if( transport.responseText.substring( 0, 3 ) != 'OK:' )
											alert( "Ett fel uppstod i anropet mot servern, mottagen feltext: "+transport.responseText );
										else
										{
											$('qcDynamic').innerHTML = transport.responseText.substring( transport.responseText.indexOf( '\n' ));
											initCashierHooks();
										}									
								}
						});
}

function initKodmyran02()
{
	initPage();
	initTabs();
	
	if( $('currency-box') )
		$('currency-box').observe( 'change', currencyChange );

	if( $('lang-box') )
		$('lang-box').observe( 'change', langChange );

	e = document.getElementsByName( "prodno" );
	
	for( a=0; a < e.length; a++ )
		if( e[a].nodeName == "SELECT" )
			$(e[a]).observe( 'change', assocChange );

	if( $('displaycount1') && $('displaycount1').nodeName == "SELECT" )
		$('displaycount1').observe( 'change', displaycountChange1 );

	if( $('displaycount2') && $('displaycount2').nodeName == "SELECT" )
		$('displaycount2').observe( 'change', displaycountChange2 );
		
	// Find the productNotify objects
	allPageTags=document.getElementsByTagName("*");  
	
	for( i=0; i < allPageTags.length; i++ )
		if( allPageTags[i].className=='notify' )
		{
			Element.extend( allPageTags[i] );
			allPageTags[i].observe( 'click', productNotify );
		}
		
	// Attach an event to the headbar operations dropdown
	if( $('headOperations'))
	{
		$('headOperations').observe( 'change', function() {
				hr = $F('headOperations');	
	
				if( hr.length > 0 )
					window.location.href = $F('headOperations');								 
		});
	}
		
	// Attach an event to the usertype selection when creating a new user
	if( $('usertype'))
	{
		if( $F('usertype') == "company" )
		{
			$('companynamerow').style.display = "block";

			if( $('customerreferencerow'))
				$('customerreferencerow').style.display = "block";
		}
		else
		{
			$('companynamerow').style.display = "none";

			if( $('customerreferencerow'))
				$('customerreferencerow').style.display = "none";
		}

		$('usertype').observe( 'change', function() {
			if( $F('usertype') == "company" )
			{
				$('companynamerow').style.display = "block";				

				if( $('customerreferencerow'))
					$('customerreferencerow').style.display = "block";
			}
			else
			{
				$('companynamerow').style.display = "none";

				if( $('customerreferencerow'))
					$('customerreferencerow').style.display = "none";
			}
		});
	}
	
	initCashierHooks();
	
	if( $('qcFetchAddr'))
		$('qcFetchAddr').observe( 'click', fetchAddr );
		
	// Hooks on the product page
	if( $('star-rating'))
	{
		elems = $('star-rating').getElementsByTagName("A");
		
		for( i=0; i < elems.length; i++ )
		{
				Element.extend( elems[i] );
				elems[i].observe( 'click', starHandler );
		}
	}
	
	// Make navigation in the left menu a bit smoother by not reloading every time
	var txq = $$('.black-box li.has-sub a');
	
	for( txi=0; txi < txq.length; txi++ )
	{
		txq[txi].observe( 'click', function( ev ) {
			var tt = ev.element();
			
			// Back up to the closest surrounding 'li' tag
			tt = tt.up( 'li' );
			
			if( tt.hasClassName( 'has-sub' ))
			{			
				if( tt.hasClassName( 'active' ))
					tt.removeClassName( 'active' );
				else
					tt.addClassName( 'active' );
			
				ev.stop();
			}
		});		
	}
}

function selectBrand( t )
{
	if( t.value > 1 )
		$('brandForm').submit();
}

function adPreviousSelectFunc()
{	
	xo = $F('adPreviousSelect');
	
	if( xo=="" )
		return;
		
	addrs = $$('#altaddrs ul');
	adb = addrs[xo].select( 'li' );
	
	elems = $$('input,select');
		
	for( i=0; i < elems.length; i++ )
	{
		if( elems[i].name == "adContact" )
			elems[i].value = adb[0].innerHTML;

		if( elems[i].name == "adAddress1" )
			elems[i].value = adb[1].innerHTML;

		if( elems[i].name == "adAddress2" )
			elems[i].value = adb[2].innerHTML;

		if( elems[i].name == "adZip" )
			elems[i].value = adb[3].innerHTML;

		if( elems[i].name == "adCity" )
			elems[i].value = adb[4].innerHTML;

		if( elems[i].name == "adCountry" )
			elems[i].value = adb[5].innerHTML;
	}
}

if (window.addEventListener)
	window.addEventListener("load", initKodmyran02, false);
else if (window.attachEvent)
	window.attachEvent("onload", initKodmyran02 );

