var http_request = false;
var myFunction = '';
var basketCount = 0;
   
function postRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Kan ikke opprette XMLHTTP instansen. Bytt browser');
	 return false;
  }
  
  http_request.onreadystatechange = hentPHP;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function hentPHP() {
	if ( http_request.readyState == 4 )
	{
		if ( http_request.status == 200 ) 
		{
			result = http_request.responseText;
			
			if ( myFunction == 'getPoststed' )
			{
				document.getElementById( 'place' ).value = result;
				document.getElementById( 'placehid' ).value = result;
			}
			else if ( myFunction == 'addToBasket' )
			{
				if ( result == 1 )
					showDialog();
			}
			else if ( myFunction == 'imglineOn' )
			{
				alert( result );
			}
			else if ( myFunction == 'getVaremerke' )
			{
				document.getElementById( 'vmerkeresult' ).innerHTML = result;	
			}
		} 
	}
}

function getPoststed( id )
{
	var value = $('#' + id ).val();
	
	if ( value.length == 4 )
	{
		if ( isInteger( value ) )
		{
			myFunction = 'getPoststed';
			var poststr = "postnr=" + encodeURI( value );
			postRequest( "/layout/set/ajax/vztlibrary/getpoststed/", poststr );	
		}
		else
		{
			document.getElementById( 'place' ).value = '';
			document.getElementById( 'placehid' ).value = '';
		}
	}
	else
	{
		document.getElementById( 'place' ).value = '';
		document.getElementById( 'placehid' ).value = '';
	}
	

}

function setCorrectBg(id)
{
	var val = $('#' + id ).val();
	if ( val != '' )
	{
		$('#' + id ).css("border-color","#CCCCCC");
		//$('#' + id ).css("background-color","#e9dcd8");
		
		if ( id != 'Store' )
			$('#' + id ).css("background-color","#CCCCCC");
	}
}

function setCorrectText()
{
	
	var id = $('#approve:checked' ).size();
	
	if ( id == 1 )
	{
		$('#approvetext' ).css("color","#000");
	}
	
}

var cancelAction = false;

function validate(form)
{
	if ( cancelAction )
		return true;

	var shop = $('#shop' ).val();
	
	var id = $('#ezcoa-367_fname' ).val();
	
	if ( id == '' )
	{
		$('#ezcoa-367_fname' ).css("border-color","red");
		$('#ezcoa-367_fname' ).css("background-color","white");
		
		return false;
	}
	
	id = $('#ezcoa-368_lname' ).val();

	if ( id == '' )
	{
		$('#ezcoa-368_lname' ).css("border-color","red");
		$('#ezcoa-368_lname' ).css("background-color","white");
		
		return false;
	}

	id = $('#ezcoa-377_postnr' ).val();
	var id2 = $('#place' ).val();
	
	if ( id == '' || id.length != 4 || id2 == '' )
	{
		$('#ezcoa-377_postnr' ).css("border-color","red");
		$('#ezcoa-377_postnr' ).css("background-color","white");
		
		return false;
	}
	
	
	id = $('#ezcoa-374_phone' ).val();
	
	if ( id == '' || id.length != 8 || isInteger(id)!=true)
	{
		$('#ezcoa-374_phone' ).css("border-color","red");
		$('#ezcoa-374_phone' ).css("background-color","white");
		
		return false;
	}	

	id = $('#ezcoa-369_user_account_email' ).val();

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if( reg.test(id) == false ) 
	{
		$('#ezcoa-369_user_account_email' ).css("border-color","red");
		$('#ezcoa-369_user_account_email' ).css("background-color","white");
		
		return false;
	}
	
	id2 = $('#email2' ).val();
	
	if ( id2 )
	{
		if ( id2 != id )
		{
			$('#email2' ).css("border-color","red");
			$('#email2' ).css("background-color","white");
			
			return false;
		}
	}
	
	id = $('#ezcoa-369_user_account_password' ).val();
	id2 = $('#ezcoa-369_user_account_password_confirm' ).val();
	
	if ( id == '' || id2 == '' || id != id2 )
	{
		$('#ezcoa-369_user_account_password' ).css("border-color","red");
		$('#ezcoa-369_user_account_password' ).css("background-color","white");

		$('#ezcoa-369_user_account_password_confirm' ).css("border-color","red");
		$('#ezcoa-369_user_account_password_confirm' ).css("background-color","white");

		return false;
	}

	id = $('#Store' ).val();

	if ( id == '-1' && shop != 1 )
	{
		$('#Store' ).css("border-color","red");
		
		return false;
	}
	
	
	
	/*
	id = $('#approve:checked' ).size();
	
	if ( id == 0 )
	{
		$('#approvetext' ).css("color","red");
		
		return false;
	}
	*/

	var posLink = document.getElementById( 'buttonx' );


	var x = findPosX( posLink );
	var y = findPosY( posLink );

	var newY = y+35;
	var newX = x-10;

	var layer = document.getElementById( 'loaderx' );

	layer.style.left = newX + "px";
	layer.style.top = newY + "px";

	showHide( 'loaderx' );
	
	
	return true;
	
}

function updateBdate()
{
	var day = $('#day' ).val();
	var month = $('#month' ).val();
	var year = $('#year' ).val();
	
	document.getElementById( 'ezcoa-381_bday' ).value = day + "-" + month + '-' + year;
}

function gotoColor( url, color )
{
	this.location = url + "?color=" + color;	
}

function goto( url )
{
	this.location = url;	
}

function showPV()
{
	var div 	= document.getElementById( 'rightfooter' );
	var layer 	= document.getElementById( 'pv' );

	if ( layer.style.display == 'inline' )
		layer.style.display = 'none';
	else
	{
		var x = findPosX( div );
		var y = findPosY( div );
		
		newX = parseInt( x - 100 );
		newY = parseInt( y - 140 );
		
		layer.style.left = newX + "px";
		layer.style.top = newY + "px";
	
		layer.style.display='inline';
		
	}

	
	
}

function addToBasket()
{
	myFunction = 'addToBasket';
	
	var nodeID = $('#size' ).val();
	var count = $('#count' ).val();
	
	if ( isInteger( count ) )
	{
		basketCount = count;
	
		var poststr = "nodeID=" + encodeURI( nodeID ) + "&count=" + encodeURI( count );
		//showDialog();
		postRequest( "/layout/set/ajax/vztlibrary/addtobasket/", poststr);
	}
	else
		document.getElementById( 'count' ).value = 1;	
}

function checkCount()
{
	var count = $('#count' ).val();
	
	if ( !isInteger( count ) )
		document.getElementById( 'count' ).value = '';	

}


function showDialog()
{
	document.getElementById( 'basketcount' ).innerHTML = parseInt( basketCount );

	if ( parseInt( basketCount ) > 1 )
		document.getElementById( 'basketending' ).innerHTML = "er" ;
	else
		document.getElementById( 'basketending' ).innerHTML = '' ;
		
	document.getElementById( 'basketcount2wrap' ).style.display = 'inline';
	var oldVal = document.getElementById( 'basketcount2' ).innerHTML;
	var newVal = parseInt( oldVal ) + parseInt( basketCount );
	document.getElementById( 'basketcount2' ).innerHTML = newVal;
	
	

	posLink = document.getElementById( 'shopbutton' );

	var x = findPosX( posLink );
	var y = findPosY( posLink );

	newY = y-10;
	newX = x-10;

	var layer = document.getElementById( 'addToBasketLayer' );

	layer.style.left = newX + "px";
	layer.style.top = newY + "px";
	showHide( 'addToBasketLayer' );
	vzttimeout = setTimeout(fadeOut, 2000);
}



function setOpacity(level) 
{
	var element = document.getElementById( 'addToBasketLayer' );

	if ( level < 0.1 )
	{
		clearTimeout(vzttimeout);
		element.style.display = 'none';
		newOpac();
	}
	else
	{
		element.style.opacity = level;
		element.style.MozOpacity = level;
		element.style.KhtmlOpacity = level;
		element.style.filter = "alpha(opacity=" + (level * 100) + ");";
	}
}

function fadeOut() 
{
	for (i = 0; i <= 1; i += (1 / 20)) {
		setTimeout("setOpacity(" + (1 - i) + ")", i * 1000);
	}
}


function newOpac()
{
	var element = document.getElementById( 'addToBasketLayer' );
	element.style.opacity = 1;
	element.style.MozOpacity = 1;
	element.style.KhtmlOpacity = 1;
	element.style.filter = "alpha(opacity=100);";		
}

function showHide( div )
{
	var tmpdiv = document.getElementById( div );
	
	if ( tmpdiv.style.display == 'inline' )
		tmpdiv.style.display = 'none';	
	else
		tmpdiv.style.display = 'inline';	
}




function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}


function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
	 var c = s.charAt(i);

	 if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function meldAv()
{
	var wantEpost = document.getElementById( 'ezcoa-406_wish_email' );	
	var wantSMS = document.getElementById( 'ezcoa-407_wish_sms' );	
	var wantPost = document.getElementById( 'ezcoa-408_wish_mail' );	
	if ( wantEpost.checked )
	{
			
	}
	else
	{
		if ( wantSMS.checked )
		{
			
		}
		else
		{
			if ( wantPost.checked )
			{
				
			}
			else
			{
				showHide('delete');
			}
		}
		//alert( "Brukeren kan slettes..." );	
	}
}

function Angre()
{
	document.getElementById( 'ezcoa-406_wish_email' ).checked = 'checked';
	showHide('delete');
}

function removeEpost()
{
	showHide('delete' );	
}

function removeUsers()
{
	document.getElementById('ezcoa-417_will_be_deleted').checked = 'checked';
	showHide('delete');	
}

function setImg( smallimg, largeimg, zoomimg )
{
	document.getElementById( 'smallimage' ).src = smallimg;
	document.getElementById( 'largeimage' ).src = largeimg;
	document.getElementById( 'zoom' ).src = zoomimg;
}

function imglineOn( nodeID, color )
{
	/*
	var box = document.getElementById( 'box' )
	myFunction = 'imglineOn';	
	var poststr = "nodeID=" + encodeURI( nodeID ) + "&color=" + encodeURI( color );
	postRequest( "/layout/set/ajax/vztlibrary/getlineimages/", poststr );		
	*/
}

function killLayer()
{
	var id = document.getElementById( 'mover' );
	id.style.display = 'none';
}

function showLayer()
{
	var id = document.getElementById( 'mover' );
	id.style.display = 'inline';
}

var isFP = 0;

function test()
{
	ifFp = 0;
	$('#basic-modal-content').modal();	
}


function showBrands()
{
	isFP = 1;
	$('#basic-modal-content2').modal();		
}

function changetotsum(frakt)
{
	
	var posten = document.getElementById( 'posten' );
	
	if ( posten.checked )
	{
		var totsum = document.getElementById('totsum');
		var totsumclean = document.getElementById('totsumclean');
		
		var newsum = parseFloat( frakt ) + parseFloat( totsumclean.innerHTML );
		totsum.innerHTML = "kr " + newsum.toFixed(2);
	
		totsum = document.getElementById('totsumex');
		totsumclean = document.getElementById('totsumexclean');
		
		newsum = parseFloat( frakt ) + parseFloat( totsumclean.innerHTML );
		totsum.innerHTML = "kr " + newsum.toFixed(2);
		
		document.getElementById( 'hiddentr' ).style.display = 'table-row';
	}
	else
	{
		var totsum = document.getElementById('totsum');
		var totsumclean = document.getElementById('totsumclean');
		
		var newsum = parseFloat( totsumclean.innerHTML );
		totsum.innerHTML = "kr " + newsum.toFixed(2);
	
		totsum = document.getElementById('totsumex');
		totsumclean = document.getElementById('totsumexclean')
		
		newsum = parseFloat( totsumclean.innerHTML ) - parseFloat( frakt );
		totsum.innerHTML = "kr " + newsum.toFixed(2);
		
		document.getElementById( 'hiddentr' ).style.display = 'none';
	}
}

function getVaremerke( cid )
{
	myFunction = 'getVaremerke';
	var poststr = "cid=" + encodeURI( cid );
	postRequest( "/layout/set/ajax/vztlibrary/getvaremerke/", poststr);
	
}

function isApproved(form)
{
	id = $('#confirm:checked' ).size();
	
	if ( id == 0 )
	{
		document.getElementById( 'approvetext' ).style.color = 'red';
		document.getElementById( 'approvelink' ).style.color = 'red';
		
		return false;
	}
	
	return true;
}
