var last_open_art_div = null;
var search 	= new asearch();
var art 	= new artikel();
var vor 	= new Vorbestellbar();
var kon 	= new akonto();
var win 	= new Fenster();
var l		= new alog();
var html	= "";
var order 	= new ajaxorder();
var as = new aservice();
var switches = null;

function setReachArt(typ)
{
	kon.setRechArt(typ);
	location.href = location.href;
}

function CloseSetReachArt(typ)
{
	$("#BillTypChanger").dialog("destroy");
	setReachArt(typ);	
}

function loadRechArt()
{
	$("#BillTypChanger").dialog(
		{ 
			modal: true,
			resizable: false,
			width: 400,
			height: 250,
			title: 'Anzeige der Preise',
			bgiframe: true,
			autoOpen: false			
		}
	);
	
	$("#BillTypChanger").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
	if(!$("#BillTypChanger").dialog("isOpen"))
	{			
		$("#BillTypChanger").dialog("open");				
	}		

	$("#BillTypChanger").html(kon.loadRechart());
	$("button, input:submit").button();	
	
}

function openIframe(url, titel)
{
	closeAllDialogs();
	
	$("#dialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: '90%',
			height: 550,
			title: titel,
			bgiframe: true,
			autoOpen: false			
		}
	);
	
	$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, Ihre Anfrage wird bearbeitet.');
	if(!$("#dialog").dialog("isOpen"))
	{			
		$("#dialog").dialog("open");				
	}	
	var html = as.openiFrame(url);
	$("#dialog").html(html);	
	$("button, input:submit").button();	
}

function openCart()
{
	closeAllDialogs();
	
	$("#CartDialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: '90%',
			height: 700,
			title: 'Ihr Warenkorb',
			bgiframe: true,
			autoOpen: false
		}
	);

	$("#CartDialog").html('<img src="/img/loading.gif" /> Bitte warten, Ihre Anfrage wird bearbeitet.');
				
	if(!$("#CartDialog").dialog("isOpen"))
	{			
		$("#CartDialog").dialog("open");				
	}	
		
	$.ajax({
	  url: '/index.php?modul=Warenkorb&aktion=ZeigeWarenkorb&PDF=1',
	  success: function(data) {
	    $('#CartDialog').html(data);
		$("button, input:submit").button();			    
	  }
	});		
}

function setCableValuesCat(val)
{
	art.saveCableValues(5, val);
	openCableConfig2();
}


function openCableConfig()
{
	closeAllDialogs();
	
	$("#CabelConfigDialog").dialog(
		{ 
			modal: true,
			resizable: false,
			title: 'Kabel Konfigurator - Kategorie',
			bgiframe: true,
			width:  900,
			height: 400,			
			autoOpen: false,
			position: ['center', 30]
		}
	);

	$("#CabelConfigDialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
				
	if(!$("#CabelConfigDialog").dialog("isOpen"))
	{			
		$("#CabelConfigDialog").dialog("open");				
	}	
	
	$("#CabelConfigDialog").html(art.CabelConfig());
			
}

function closeAllDialogs()
{
	if ($("#dialog").dialog("isOpen")) 
	{
		$('#dialog').dialog('destroy');
	}

	if ($("#CartDialog").dialog("isOpen")) 
	{
		$('#CartDialog').dialog('destroy');
	}

	if ($("#artinfoDigalog").dialog("isOpen")) 
	{
		$('#artinfoDigalog').dialog('destroy');
	}

	if ($("#CabelConfigDialog").dialog("isOpen")) 
	{
		$('#CabelConfigDialog').dialog('destroy');
	}		
} 

function openCableConfig2()
{
	closeAllDialogs();
	
	$("#CabelConfigDialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: '95%',
			height: 710,
			title: 'Netzwerkkabel-Schnellsuche',
			bgiframe: true,
			autoOpen: false,			
			position: 'center',
			closeText: 'weiter Einkaufen',
			close: function(event, ui) { $('#CabelConfigDialog').dialog('destroy');  },
			position: ['center', 30]
		}
	);


	$("#CabelConfigDialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
				
	if(!$("#CabelConfigDialog").dialog("isOpen"))
	{			
		$("#CabelConfigDialog").dialog("open");				
	}	
	
	$("#CabelConfigDialog").html(art.CabelConfig2());	
	order.getSmallCart2();
			
}

function OpenArtinfoDigalog(artnr)
{
	closeAllDialogs();
	
	$("#artinfoDigalog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: '95%',
			height: 700,
			title: 'Artikeldetails',
			bgiframe: true,
			autoOpen: false,			
			position: 'center'
		}
	);

	$("#artinfoDigalog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
				
	if(!$("#artinfoDigalog").dialog("isOpen"))
	{			
		$("#artinfoDigalog").dialog("open");				
	}	

	$.ajax({
	  url: '/index.php?loadArt='+artnr+'&PDF',
	  success: function(data) {
	    
		$("#artinfoDigalog").html(data);	
	    $("button, input:submit").button();
	  }
	});	

		
}

function reloadCart()
{

		
	$("#CartDialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
	$.ajax({
	  url: '/index.php?modul=Warenkorb&aktion=ZeigeWarenkorb&PDF=1',
	  success: function(data) {
	    $('#CartDialog').html(data);
		$("button, input:submit").button();
		//$("form.new").jqTransform();			    
	  }
	});		
}
  
function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		
		if (c.indexOf(nameEQ) == 0) 
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}		
  		function saveFormArtImage()
  		{
  			document.getElementById('artImageUploadForm').style.display = 'none';
  			document.getElementById('artImageUpload').style.display = 'block';
  			
  			return true;
  		}
  		
  		function undosaveFormArtImage()
  		{
  			document.getElementById('artImageUploadForm').style.display = 'block';
  			document.getElementById('artImageUpload').style.display = 'none';
  		}
  		
  		function expandForEdit()
  		{
			return true;
  			var id = artImageUploadForm.parentNode.id;
  			var el = document.getElementById(id);
  			
  			if(el)
  			{
  				el.style.height = '690px';
  				document.getElementById('ArtImageFrame').style.height = '600px';  				
  			}			
  		}
  		
  		function closeArtImage(artnr)
  		{
   			var id = artImageUploadForm.parentNode.id;
  			var el = document.getElementById(id);
  			
  			if(el)
  			{
  				el.style.height = '';
  				document.getElementById('ArtImageFrame').style.height = '';  				
  			} 		
  			
  			order.closeImageDiv();	
  		}

function open_art_div(div, art, num, title, catorder)
{
	div = 'dialog';
	el = document.getElementById(div);
	if(el)
	{
		HTML_AJAX.defaultServerUrl = 'ajax.php';			

		el.innerHTML = '<img src="/img/loading.gif" />'
		el.style.display = 'block';
		order.addorder(art, num, div, false, catorder);

		$("#dialog").dialog(
			{ 
				position: 'center', 
				modal: true,
				resizable: false,
				width: 800,
				height: 490,
				title: title,
				bgiframe: true,
				autoOpen: false
			}
		);
		
		$("#dialog").dialog("open");
		
		//ret = HTML_AJAX.replace('PrevCart', 'ajaxorder', 'getCart');					
		//ret = HTML_AJAX.replace('smallCart', 'ajaxorder', 'getSmallCart');
		
		order.getSmallCart2();		
	}
}

function open_art_var_div(art, num, title)
{
	div = 'dialog';
	el = document.getElementById(div);
	if(el)
	{
		el.innerHTML = '<img src="/img/loading.gif" />'
		el.style.display = 'block';
		

		$("#dialog").dialog(
			{ 
				position: 'center', 
				modal: true,
				resizable: false,
				width: 800,
				height: 490,
				title: title,
				bgiframe: true,
				autoOpen: false
			}
		);
		
		$("#dialog").dialog("open");
		
		var html = order.selectVar(art, num);	
		$("#dialog").html(html);	
		
	}
}

function open_art_div_image(title, art, num)
{
	el = document.getElementById('dialog');
	if (el) 
	{
		HTML_AJAX.defaultServerUrl = 'ajax.php';

		$("#dialog").dialog(
			{ 
				position: 'center', 
				modal: true,
				resizable: false,
				width: 800,
				height: 490,
				title: title,
				bgiframe: true,
				draggable: false			
			}
		);		
		el.innerHTML = '<img src="/img/loading.gif" />'
		order.imageUpload(art, num, 'dialog');

	}	
}

function close_art_div_image(artnr)
{
	$('#dialog').dialog('close');
	location.href=location.href; 	
}

function close_last_open_art_div(div)
{
	var el = document.getElementById(last_open_art_div);
	if(last_open_art_div && el && last_open_art_div != div)
	{
		el.style.display = 'none';
	}
}

function initRightMenu()
{
	var div_r = document.getElementById('rightTD');
	var div_m = document.getElementById('middle');		
	var menu  = document.getElementById('MenuSwitcherButton');
	var userm = readCookie('RightMenu');
			
	if(!div_r)
	{
		if(menu)
			menu.innerHTML = '';
	}
	else if((!div_r == 'none' || userm == 1) && showright)
	{
		if(menu)
			menu.innerHTML = 'Rechtes Menu ausblenden';
	}			
	else
	{
		if(menu)
			menu.innerHTML = 'Rechtes Menu einblenden';
			
			div_r.style.display = 'none';		
	}
} 

function switchRightMenu()
{  		
	var div_r = document.getElementById('rightTD');
	var div_m = document.getElementById('middle');			
	var menu  = document.getElementById('MenuSwitcherButton');  			  			  			
	
	if(div_r.style.display == 'none')
	{
		div_r.style.display = 'block';
		menu.innerHTML = 'Rechtes Menu ausblenden';
		document.cookie="RightMenu=1;expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();
	}
	else
	{
		div_r.style.display = 'none';
		//div_m.style.marginRight = '10px';
		menu.innerHTML = 'Rechtes Menu einblenden';
		document.cookie="RightMenu=0;expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();
	}
	
}  	  	

function getStyle(oElm, strCssRule)
{
	return true;
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle)
	{
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle)
	{
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function System_NiftyCheck()
{
	if(NiftyCheck())
	{	
		var color = "#FFF";
					
		color = getStyle(document.getElementById('menuHolderBlock'),'background-color' );
		Rounded("div.block_new","top","transparent","FFFFFF","border #C0C0C0");
		Rounded("div.block_new","bottom","transparent",color,"big border #C0C0C0");					

		color = getStyle(document.getElementById('top2'),'background-color' );
		Rounded("div#top","top","transparent","FFFFFF","big border #C0C0C0");
		Rounded("div#top","bottom","transparent",color,"smooth border #C0C0C0");
	}
}

function formatAsMoney(mnt) 
{
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	return (mnt == Math.floor(mnt)) ? mnt + ',00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
}	

function artInfo(id)
{
	if ($("#dialog").dialog("isOpen")) 
	{
		$('#dialog').dialog('destroy');
	}
		
	$("#dialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: '450',
			title: 'Artikelinformationen',
			bgiframe: true,
			autoOpen: false,
			position: ['center', 30]
		}
	);	
	$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
	$('#dialog').dialog('open');	
	
	$("#dialog").html(art.ArtInfo(id));
}



function mailOrder()
{
	var mail = prompt(unescape('Sie wollen unverbindlich den Warenkorb zur weiteren Verwendung zugesandt bekommen, dann geben Sie bitte hier Ihre E-Mail-Adresse ein:'), '');
	
	if(mail)
		order.getEMailOrder(mail);			
}

function cleanCart()
{
	if(confirm('Wollen Sie den Warenkorb wirklich leeren?'))
	{
		order.cleanCart();
		location.href=location.href;
	}
}

function addQuickOrder(search, fields)
{
	$("#dialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: 800,
			height: 490,
			title: 'Schnellbestellung',
			bgiframe: true,
			autoOpen: false,
			buttons: 
			{
				"zurück zum Warenkorb": function() 
				{
					$( this ).dialog( "destroy" );
				}
			},
		   close: function(event, ui) { $( this ).dialog( "destroy" ); }		
		}
	);
	var html = order.addQuickOrder(search, fields);
				
	if(!$("#dialog").dialog("isOpen"))
	{			
		$("#dialog").dialog("open");				
	}	
		
	$("#dialog").html(html);
										
}
		
function sendToLieAdress()
{
	val = kon.countLieferadressen();
	
	if(val)
	{
		$("#dialog").dialog(
			{ 
				modal: true,
				resizable: false,
				width: 800,
				height: 600,
				title: 'Lieferung an Lieferadresse / Packstation',
				bgiframe: true,
				autoOpen: false,
				buttons: 
				{
					"abbrechen": function() 
					{
						$( this ).dialog( "destroy" );
					}
				},
			   close: function(event, ui) { $( this ).dialog( "destroy" ); }										
			}
		);
		
		$("#dialog").dialog("open");			
		$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
		var html = kon.sendToLieAdress();								
		$("#dialog").html(html);
	}
	else
	{
		location.href='/index.php?modul=Konto&aktion=Lieferanschrift';
	}
				
}

	function unsetLie()
	{
	$( "#dialog" ).dialog({
		modal: true,
		width: 400
	});
	$("#dialog").dialog("open");
		$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
		
		order.setLieAdress();
		  				
		//location.href=location.href;

	if ($("#dialog").dialog("isOpen")) 
	{
		$('#dialog').dialog('close');
	}  				
	reloadCart();  			
	} 		

function DialogChangeValue(artnr, num)
{
	$("#dialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: 500,
			height: 350,
			title: 'Artikel Menge',
			bgiframe: true,
			autoOpen: false
		}
	);
	
	$("#dialog").dialog("open");			
	$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
	var html = order.DialogChangeValue(artnr, num);								
	$("#dialog").html(html);
				
}  		

function removeCartItem(artnr)
{
	if(confirm('Artikel wirklich aus dem Warenkorb entfernen?'))
	{
		order.changeValue(artnr, 0);
		
		if ($("#dialog").dialog("isOpen")) 
		{
			$('#dialog').dialog('close');
		}  				
		reloadCart();
	}  
}

function toCash()
{
	if(kon.allowOrder())
		document.GOORDER.submit();
	else
	{
		if ($("#dialog").dialog("isOpen")) 
		{
			$('#dialog').dialog('close');
		}
	
		if ($("#CartDialog").dialog("isOpen")) 
		{
			$('#CartDialog').dialog('close');
		}
	
		if ($("#CabelConfigDialog").dialog("isOpen")) 
		{
			$('#CabelConfigDialog').dialog('close');
		}
			
		$("#doCashDiv").dialog(
			{ 
				modal: true,
				resizable: false,
				width: '90%',
				height: 700,
				title: 'Anmelden',
				bgiframe: true,
				autoOpen: false
			}
		);
	
		$("#doCashDiv").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
					
		if(!$("#doCashDiv").dialog("isOpen"))
		{			
			$("#doCashDiv").dialog("open");				
		}
		
		$("#doCashDiv").html(kon.toCash());						
	}				
				
}		

function setZahlweise(z)
{
	order.setZahlweise(z);
	reloadCart();
}

function setAlternateZahlweise(el)
{
	val = el.value;
	order.setAlternateZahlweise(val);
}

function showKaufAufRechnung()
{
	var url = '/index.php?datei=Infos RG-Check&MenuId=225&Gruppe=0&Untergruppe=0&modul=HTMLText&aktion=ZeigeHTMLText&PDF';

	if ($("#dialog").dialog("isOpen")) 
	{
		$('#dialog').dialog('close');
	}

	$("#dialog").dialog(
		{ 
			modal: true,
			resizable: false,
			width: 600,
			height: 400,
			title: 'Kauf auf Rechnung',
			bgiframe: true,
			autoOpen: false,
			buttons: 
			{
				"abbrechen": function() 
				{
					$( this ).dialog( "destroy" );
				}
			}					
		}
	);

	$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
				
	if(!$("#dialog").dialog("isOpen"))
	{			
		$("#dialog").dialog("open");				
	}			
				
	$('#dialog').html(order.showKaufAufRechnung());
	$(".chkRgZahler").button();			    
}

function checkPAYRGCHECK()
{
	$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
	
	order.checkPAYRGCHECK();

	if ($("#dialog").dialog("isOpen")) 
	{
		$('#dialog').dialog('destroy');
	}		
	
	reloadCart();
		
}

function doLogin(name, pass, reload)
{					
	if(kon.login(name, pass))
	{
		if(reload = 1)
			location.href=location.href;
		else
			location.href='/index.php?modul=Bestellung&aktion=Step1';													
	}
	else
	{
		$( "#dialog" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			},
			resizable: false
		});							
		
		$("#dialog").html('Ihre Anmeldung war <strong>nicht</strong> erfolgreich.<br/><br/><button onClick="location.href=\'/index.php?modul=Anmeldung&aktion=forgetPassword\'">Passwort vergessen?</button>');
		$("button").button();
		if(!$("#dialog").dialog("isOpen"))
		{			
			$("#dialog").dialog("open");				
		}								
	}
}

function doLogout()
{
	kon.logout();
	location.href="/index.php";
}


function removeInfoTextFromInput(el, text)
{
	if(el.value==text)
		el.value = '';				
	
	el.style.color =  '#000';	
}


function checkCookie() 
{
	
	document.cookie="OK=1";
	var test = readCookie("OK");
	
	if(!test) 
	{
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		$( "#dialog" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
		$("#dialog").html('Sie müssen Cookies erlauben, damit Sie im Shop etwas bestellen können');
	}
}

function loadContent()
{
	$("#dialog").html('<img src="/img/loading.gif" /> Bitte warten, ihre Anfrage wird bearbeitet.');
}




function SwitchSelectorStart()
{
	switches = new aswitch();	
	$("#SwitchSelectorStart").html('<img src="/img/loading.gif" /> Bitte einen Augenblick Gedult, der Switch Configurator wird geladen ...');
	$("#SwitchSelectorStart").html(switches.loadStartContent());
}

function SwitchSelectorSearch()
{
	$("#SwitchSelectorSearch").html('<img src="/img/loading.gif" /> Bitte einen Augenblick Gedult, es wird nach passenden Switches gesucht ...');
	$("#SwitchSelectorSearch").html(switches.loadSearchContent());
	
}

