/* Popups */

function loadXMLDoc(id, url) {
	var xmlhttp;
	if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else { // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var start = xmlhttp.responseText.indexOf('class="Main_TD"') - 4;
			var stop = xmlhttp.responseText.indexOf('id="ShopLayOut_SpaceBottom_TD"') - 4;
			document.getElementById(id).innerHTML = xmlhttp.responseText.substring(start, stop);
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}

function displayServicePopup(id) {
	$('#fade').css('display', 'block');
	if($('#' + id).html() != '') {
		$('#serviceframe').css('display', 'none');
		$('#' + id).css('display', 'block');
		$('#' + id).css('z-index', '8');
	}
	else {
		$('#' + id).css('display', 'none');
		$('#serviceframe').css('display', 'none');
		document.getElementById('serviceframe').contentWindow.location.replace('/shop/cms-kundeservice.html?css=iframes');
		$('#serviceframe').load(function(){
			$('#serviceframe').css('display', 'block');
			$('#serviceframe').css('z-index', '8');
		});
	}
	popupPlacement('servicepopup');
	$('#servicepopup').css('display', 'block');
}

function closeServicePopup() {
	$('#fade').css('display', 'none');
	$('#servicepopup').css('display', 'none');
	$('#serviceframe').css('display', 'none');
	$('#serviceframe').css('z-index', '6');
	$('#contact').css('display', 'none');
	$('#contact').css('z-index', '6');
	$('#freight').css('display', 'none');
	$('#freight').css('z-index', '6');
	$('#return').css('display', 'none');
	$('#return').css('z-index', '6');
	$('#warranty').css('display', 'none');
	$('#warranty').css('z-index', '6');
	$('#shops').css('display', 'none');
	$('#shops').css('z-index', '6');
}

function popupPlacement(id) {
	var wWidth = window.innerWidth;
	var wHeight = window.innerHeight;
	if(navigator.appName == 'Microsoft Internet Explorer') {
		wWidth = document.body.clientWidth;
		wHeight = document.body.clientHeight;
	}
	var pWidth = $('#' + id).width();
	var pHeight = $('#' + id).height();
	var sLeft = $('body').scrollLeft();
	var sTop = $('body').scrollTop();
	var hPlacement = (wWidth - pWidth) / 2 + sLeft;
	var vPlacement = (wHeight - pHeight) / 2 + sTop;
	if(wWidth <= pWidth) {
		hPlacement = 0 + sLeft
	}
	if(wHeight <= pHeight) {
		vPlacement = 0 + sTop
	}
	$('#' +id).css('left', hPlacement);
	$('#' + id).css('margin-left', '0px');
	$('#' + id).css('top', vPlacement);
	$('#' + id).css('margin-top', '0px');
}

function displayFeedbackPopup() {
	$('#fade').css('display', 'block');
	$('#feedbackpopup').css('display', 'block');
	popupPlacement('feedbackpopup');
}

function closeFeedbackPopup() {
	$('#fade').css('display', 'none');
	$('#feedbackpopup').css('display', 'none');
	$('#feedbackframe').attr('src', '/iframes/feedback.html');
}

function displaySizePopup() {
	$('#fade').css('display', 'block');
	$('#sizepopup').css('display', 'block');
	popupPlacement('sizepopup');
}

function closeSizePopup() {
	$('#fade').css('display', 'none');
	$('#sizepopup').css('display', 'none');
	$('#sizerequest').attr('src', '/iframes/sizereq.html');
}

function RemovePopup() {
	document.getElementsByTagName('body')[0].style.overflow='auto';
	document.getElementById('fade').style.display='none';
	document.getElementById('feedbackpopup').style.display='none';
	document.getElementById('sizepopup').style.display='none';
	document.getElementById('buyPopup').style.display='none';
	document.getElementById('servicePopup').style.display='none';
	document.getElementById('contact').style.display='none';
	document.getElementById('freight').style.display='none';
	document.getElementById('return').style.display='none';
	document.getElementById('warranty').style.display='none';
	document.getElementById('shops').style.display='none';
	document.getElementById('defaultpopup').style.display='none';
	document.getElementById('freeProductPopup').style.display='none';
	document.getElementById('noFreeProductPopup').style.display='none';
	document.getElementById('backOrderFreeProductPopup').style.display='none';
	document.getElementById('backDeleteFreeProductPopup').style.display='none';
}


/*
function loadXMLDoc(id, url)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  var button = '<a id="close" onmouseover="cOver(\'' + id + 'img\');" onmouseout="cOut(\'' + id + 'img\');" href="javascript:RemovePopup();"><img id="' + id + 'img" src="/design/buttons/close.png" /></a><br><br><br>';
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
		var start = xmlhttp.responseText.indexOf('class="Main_TD"') - 4;
		var stop = xmlhttp.responseText.indexOf('id="ShopLayOut_SpaceBottom_TD"') - 4;
		document.getElementById(id).innerHTML = button + xmlhttp.responseText.substring(start, stop);
    }
  else {
		document.getElementById(id).innerHTML = 'def';
    }
  }
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
*/

function DisplayPopup(id, place) {
	if (document.getElementById(id).innerHTML == 'def') {
		if (navigator.appName == 'Microsoft Internet Explorer' && place == 'b') {
			document.getElementById(id).style.bottom='5%';
		}
		else if (navigator.appName == 'Microsoft Internet Explorer' && place == 't') {
			document.getElementById(id).style.top='5%';
		}
		document.getElementsByTagName('body')[0].style.overflow='hidden';
		document.getElementById('fade').style.display='block';
		document.getElementById('defaultpopup').style.display='block';
	}
	else {
		if (navigator.appName == 'Microsoft Internet Explorer' && place == 'b') {
			document.getElementById(id).style.bottom='5%';
		}
		else if (navigator.appName == 'Microsoft Internet Explorer' && place == 't') {
			document.getElementById(id).style.top='5%';
		}
		document.getElementsByTagName('body')[0].style.overflow='hidden';
		document.getElementById('fade').style.display='block';
		document.getElementById(id).style.display='block';
	}
	if (navigator.appName == 'Microsoft Internet Explorer') {
		document.getElementById('fade').style.display='none';
	}
}

/*
function RemovePopup() {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		document.getElementById('contact').style.bottom='';
		document.getElementById('freight').style.bottom='';
		document.getElementById('return').style.bottom='';
		document.getElementById('warranty').style.bottom='';
		document.getElementById('shops').style.bottom='';
		document.getElementById('contact').style.top='';
		document.getElementById('freight').style.top='';
		document.getElementById('return').style.top='';
		document.getElementById('warranty').style.top='';
		document.getElementById('shops').style.top='';
	}
	document.getElementsByTagName('body')[0].style.overflow='auto';
	document.getElementById('fade').style.display='none';
	document.getElementById('feedbackpopup').style.display='none';
	document.getElementById('sizepopup').style.display='none';
	$('#servicePopup').css('display', 'none');
	$('#buyPopup').css('display', 'none');
	document.getElementById('contact').style.display='none';
	document.getElementById('freight').style.display='none';
	document.getElementById('return').style.display='none';
	document.getElementById('warranty').style.display='none';
	document.getElementById('shops').style.display='none';
	document.getElementById('defaultpopup').style.display='none';
}
*/

/* Alerts */

var prodMin = MinBuyMsg;
var prodMax = MaxBuyMsg;
var prodSelect = SelectVarMsg;
var prodStock = 'Dit køb overstiger lagerbeholdning !!!';

function alert(param) {
	alertPlacement();
	$('#alertText').html(param);
	$('#alertFade').css('display', 'block');
	$('body').css('overflow', 'hidden');
	$('#stopPopup').css('display', 'none');
	$('#defaultAlert').css('display', 'block');
	$('#alertLinks').css('display', 'block');
	$('#buyMoreText').css('display', 'none');
	$('#buyLinks').css('display', 'none');
	var cookie = getCookie('spb');
	if(document.getElementById('BUYSECTION') != null && param != prodMin && param != prodMax && param != prodSelect && param != prodStock && cookie != 'yes') {
		$('#alertLinks').css('display', 'none');
		$('#buyMoreText').css('display', 'block');
		$('#buyLinks').css('display', 'block');
		$('#stopPopup').css('display', 'block');
		var bText = $('#baskettext').text();
		var bFrom = bText.indexOf('ALT') + 4;
		var bTo = bText.indexOf('Kr.') - 1;
		var mT1 = bText.substring(bFrom, bTo).replace('.', '');
		var mText = mT1.replace(',', '.');
		var mNumber = parseFloat(mText);
		var bLeft = 0;
		if(bText.match('TOM') || mNumber == 0) {
			$('#buyMoreText').html('Fri fragt ved 600 kr.<br/>Gratis cap ved 1000 kr.');
		}
		if (mNumber < 600) {
			bLeft = Math.round((600 - mNumber) * 100);
			$('#buyMoreText').html('Køb for ' + (bLeft / 100) + ' kr. mere, og få fri fragt');
		}
		if (mNumber < 1000 && mNumber >= 600) {
			bLeft = Math.round((1000 - mNumber) * 100);
			$('#buyMoreText').html('Du får nu fri fragt.<br/>Køb for ' + (bLeft / 100) + ' kr. mere, og få en gratis cap');
		}
		if (mNumber >= 1000) {
			$('#buyMoreText').html('Tillykke du får nu både fri fragt<br/> og en gratis cap.');
		}
	}
	else if(document.getElementById('BUYSECTION') != null && param != prodMin && param != prodMax && param != prodSelect && param != prodStock && cookie == 'yes') {
		$('#alertFade').css('display', 'none');
		$('body').css('overflow', 'auto');
		$('#defaultAlert').css('display', 'none');	
	}
}

function checkStopPopup() {
	var checked = $('#stopBox').attr('checked');
	if(checked == true) {
		setCookie('spb', 'yes');
	}
}

function alertPlacement() {
	var dHeight = $('#defaultAlert').height();
	$('#defaultAlert').css('margin-top', '-' + (dHeight / 2 + 20) + 'px');
}


function closeAlert() {
	$('#alertFade').css('display', 'none');
	$('body').css('overflow', 'auto');
	$('#defaultAlert').css('display', 'none');
}
	
function stopPopup() {
	//$('#fade').css('display', 'none');
	//$('body').css('overflow', 'auto'); /* føjes til fade / andre popups ???? */
	//$('#buyPopup').css('display', 'none');
	//if(document.getElementById('stopBox').checked == true) {
		//checked = 1;
	//}
	//if() { location.reload();}
}



/* forms */
/* Workaround mailto */

function nClose() {
    $("label#newsmailError1").hide();  
    $("label#newsmailError2").hide();  
    $("label#subscribe").hide();  
    $("label#subscribeError").hide();  
}

/* Iframes ? */

/*
function checkCss() {
	var css = location.search.substring(location.search.indexOf('=') + 1, location.search.length);
	if(css != ''){
		document.write('<link href="/design/css/' + css + '.css" rel="stylesheet" type="text/css">');
	}
}
*/
/*
function setWidth() {
	var css = location.search.substring(location.search.indexOf('=') + 1, location.search.length);
	if(css != ''){
		$('#ShopContentLayOutTable').width(499);
	}
}
*/

function getIframeContent(fId, url) {
	$('#' + fId).attr('src', url);
}

function openIframePopup(divId) {
	//var source = $('#' + fId).attr('src');
	//if(	source != 'http://undergroundshop.dk/shop/cms-kundeservice.html?css=iframes') {
		$('#fade').css('display', 'block');
		$('#' + divId).css('display', 'block');
	//}
}

function closeIframePopup(divId) {
	$('#' + divId).css('display', 'none');
	$('#fade').css('display', 'none');
}


