/**
 * function goToPage
 * goes to the next page
 * @param pageUrl url to page
 */
function goToPage(pageUrl){
	location.href = pageUrl;
}

function setupMail(containerID, suffix,prefix)
{
	var container = document.getElementById(containerID);
	if(container!=null && typeof(container)!="undefined"){
		container.innerHTML= prefix+"@"+suffix;
		container.innerHTML = '<a h'+
								'ref="'+
								'mail'+
								'to:'+
								container.innerHTML+'">'+
								container.innerHTML+'</a>';
	}
}


function validateForm(fFieldStr){
	//alert(fFieldStr);

	var tmp 	= fFieldStr.split(":");
	var fName	= tmp[0];
	if(tmp[1]==undefined)
		return;
	var fFields = tmp[1].split(",");
	for(var fField in fFields){
		if(fFields[fField] != ""){
			tmp = fFields[fField].split(".");
			if(tmp.length == 2){
				var e = document.forms[fName].elements[""+fName+"["+tmp[0]+"]["+tmp[1]+"]"];
			}else{
				var e = document.forms[fName].elements[""+fName+"["+fFields[fField]+"]"];
			}
			if(e.type == "checkbox"){
				e.style.outline = "1px solid #9F1012";
			}

			e.style.border = "1px solid #9F1012";
		}
	}
}

/**
 * function openWindow
 * opens window with defined parameters
 *
 * @param windowUrl
 * @param width window width
 * @param height window height
 * @param left left position
 * @param top top position
 * @param windowName
 */
function openWindow(windowUrl, width , height , left , top , windowName, scrollbars, resizable){
	if(width == undefined || width == 0)
		width = 680;

	if(height == undefined || height == 0)
		height = 480;

	if(top == undefined)
		top = 100;

	if(left == undefined)
		left = 100;

	if(scrollbars == undefined)
		scrollbars = "yes";
	if(resizable == undefined)
		resizable = "yes";
		
	var windowOpener = window.open(windowUrl, windowName, "resizable="+resizable+",width="+width+",height="+height+",left="+left+",top="+top+",toolbar=no,menubar=no,scrollbars="+scrollbars+"");

	windowOpener.focus();
	return windowOpener;
}


function getMouseCoords(e){
	var r = new Array();
	if(document.all){
		r['posx'] = e.clientX;
		r['posy'] = e.clientY+document.documentElement.scrollTop;
	}else{
		r['posx'] = e.pageX;
		r['posy'] = e.pageY;
	}

	return r;
}


function getWindowCenterPosition(){
	var r = new Array();
	if(document.all){
		//r['posy'] = (document.body.offsetHeight-document.documentElement.scrollTop)/2;
		//r['posy'] = (document.documentElement.clientHeight+document.documentElement.scrollTop)/2;
		r['posx'] 	= (document.documentElement.clientWidth+document.documentElement.scrollLeft)/2;
		r['posy'] 	= (document.documentElement.clientHeight/2+document.documentElement.scrollTop);
	}else{
		r['posy'] = (window.innerHeight/2+window.pageYOffset);
		r['posx'] = window.outerWidth/2;
	}
	
	return r;
}

function getWindowSize(){
	var r = new Array();
	if(document.all){
		//r['posy'] = (document.body.offsetHeight-document.documentElement.scrollTop)/2;
		//r['posy'] = (document.documentElement.clientHeight+document.documentElement.scrollTop)/2;
		r['wd'] 	= document.documentElement.clientWidth;
		r['ht'] 	= document.documentElement.clientHeight;
		r['left'] 	= document.documentElement.scrollLeft;
		r['top'] 	= document.documentElement.scrollTop;
		if(document.body.scrollHeight > document.body.offsetHeight){
			r['zht']	= document.body.scrollHeight;
		}else{
			r['zht']	= document.body.offsetHeight;
		}
		r['zht'] = r['zht'] + 'px';
	}else{
		r['ht'] 	= window.innerHeight;
		r['wd'] 	= window.outerWidth;
		r['left'] 	= 0;
		r['top'] 	= document.body.height;
		r['zht']	= "100%";
	}
	if(document.body.scrollHeight > document.body.offsetHeight){
		r['zht']	= document.body.scrollHeight + 'px';
		r['zwd']	= (document.body.scrollWidth+23) + 'px';
	}else{
		r['zht']	= document.body.offsetHeight + 'px';
		r['zwd']	= (document.body.offsetWidth+23) + 'px';
	}
	r['fht'] = document.body.offsetHeight;
	return r;
}

/**
 * shows content infobox with title and content
 *
 * @param strBoxTitle
 * @param strBoxContent
 */
function showInfoBox(strBoxTitle, strBoxContent, strBoxID, boolShowOnceOnly, posX, posY){

	var cookieVal  = getCookieValueByName("infoBoxSet");
	if(cookieVal == strBoxID && boolShowOnceOnly == true){
		return false;
	}

	var posStyle = "";
	var def_temp_width = 470;
	var def_temp_height = 300;
	if(posX != undefined && posY != undefined){
		posStyle = "left:"+(posX-def_temp_width/2)+"px;top:"+(posY-def_temp_height/2)+"px;";
	}
	posStyle+="width: "+def_temp_width+"Px";

	if(document.all){
		//if ie show iframe behind div -> selectbox bug in ie, what a silly bufix :o//
		var infoBoxDiv = "<iframe src='' style='display: block; height: 140Px; z-index:1;border: 0px;"+posStyle+"' frameborder='0' class='infoBoxContainer' id=\"infoBoxIframe\"></iframe>";
	}else{
		var infoBoxDiv = "";
	}

	infoBoxDiv += "<div class=\"infoBoxContainer\" id=\"infoBox\" style=\"display:block;z-index:2;"+posStyle+"\">"+
						"<div class=\"infoBoxCloser\" style=\"position:absolute;right:5px;top:0px;\"><a href=\"javascript:hideInfoBox()\">[x]<\/a><\/div>"+
						"<h2>"+strBoxTitle+"<\/h2>"+
						strBoxContent+
					"<\/div>";
	/*infoBoxDiv += "<div class=\"infoBoxContainer\" id=\"infoBox\" style=\"display:block;z-index:2;"+posStyle+"\">"+
						"<div class=\"infoBoxCloser\" style=\"position:absolute;right:5px;top:0px;\"><a href=\"javascript:hideInfoBox()\">[x]<\/a><\/div>"+
						"<h2></h2><div class=\"infoBoxHeader\">"+strBoxTitle+"<\/div>"+
						"<div class=\"infoBoxContent\">"+strBoxContent+"<\/div>"+
					"<\/div>";	*/		
	var iH = document.getElementById("infoBoxHolder");
	if(iH){
		iH.innerHTML = infoBoxDiv;
	}else{
		document.body.innerHTML += infoBoxDiv;
	}

	if(boolShowOnceOnly == true){
		document.cookie = "infoBoxSet="+strBoxID;
	}
	return true;
}


function checkForFireFox(){
	if(!document.all){
		var myNotice = "Ihr Browser unterst&uuml;tzt die aktuelle .NET ClickOnce Installation nicht.<br/>Bitte installieren Sie zuerst das ClickOnce Plugin.<br/><a href=\"https://addons.mozilla.org/en-US/firefox/addon/1608\" target=\"_blank\">ClickOnce Plugin installieren</a>";
		var i = showInfoBox("ACHTUNG", myNotice, "clickOnceNotice", true, undefined, undefined);
	}
}

/**
 * hides content infobox
 */
function hideInfoBox(){
	var d = document.getElementById("infoBox");

	if(d){
		d.style.display = "none";

		if(document.all){
			//if ie hide iframe behind div -> selectbox bug in ie
			d = document.getElementById("infoBoxIframe");
			d.style.display = "none";
		}
	}
}

/**
 * helperfunction opens cookie and gets value by name
 *
 * @param strName
 */
function getCookieValueByName(strName){
	if(!document.cookie){
		return false;
	}

	var c   = document.cookie;
	var tmp = c.split(";")
	for(var i in tmp){
		var cookieDesc = tmp[i].split("=");
		if(strName == cookieDesc[0].replace(" ", "")){
			return cookieDesc[1];
		}
	}

	return false;
}

/**
 * function changeCartAmount
 * changes amount of position in cart

 *
 * @param index position
 * @param obj form object
 */
function changeCartAmount(index, obj){
	document.forms['cart'].elements['amount'].value = obj.value;
	document.forms['cart'].elements['index'].value  = index;
	document.forms['cart'].submit();
}

/**
 * function changeCartAmount
 * changes amount of position in cart

 *
 * @param index position
 * @param obj form object
 */
function changeCartProduct(index, obj){
	document.forms['cart'].elements['chproduct'].value = obj.value;
	document.forms['cart'].elements['index'].value  = index;
	document.forms['cart'].submit();
}


/**
 * function changeView
 * changes vibility from object by id
 *
 * @paramobjectID
 */
function changeView(objectID){
	var d = document.getElementById(objectID);
	if(d.style.visibility == "hidden"){
		d.style.visibility = "visible";
		return;
	}

	d.style.visibility = "hidden";
	return;
}

/**
 * function openQuickhelpPreview
 * opens window with quickhelpreview
 *
 * @param imgName
 */
function openQuickhelpPreview2(imgName, imgPath){
	img = new Image();
	img.src = imgPath+imgName;

	if(img.width == 0 || img.width == undefined)
		width = 640;
	else
		width = img.width;

	if(img.height == 0 || img.height == undefined)
		height = 490;
	else
		height = img.height;

	return openWindow('index.php?area=popup&module=service.quickhelp&path='+imgPath+imgName+'', width, height, '100', '100', 'quickhelpreview', 'no');

}


function openQuickhelpPreview(imgName){
		imgPath = "img/default/content/static/quickhelp/big/";
	openQuickhelpPreview2(imgName,imgPath);
}

function openQuickhelpPreviewShop(imgName,shopname){
	if(shopname==undefined){
		shopname='dfg';
	}
	imgPath = "img/"+shopname+"/content/static/quickhelp/big/";
	openQuickhelpPreview2(imgName,imgPath);
}


var editor;
var i;
var z = false;
function openEditor(url, imageNotify, editorInfoBox, editorType){

	var styles= "vertical-align:bottom;	width:402px;	height:21px;	clear:left;	padding-bottom:0px;	";
	var infoBoxTitle 	= 	txt_advise_title;
	var infoBoxContent 	= 	txt_advise_text	;
	infoBoxContent=infoBoxContent.replace(/%buttons%/g, 
							"<ul>"+
								"<li class=\"buttonBox\"><a href=\"ws-myprinteria-registrieren-register,registerstep1.html\"><span></span>"+btn_advise_register+"</a></li>"+
								"<li class=\"buttonBox\"><a href=\"javascript:openEditor('"+url+"')\"><span></span>"+btn_advise_editor+"</a></li>"+
							"</ul>");

	var startEditor		= false;
	var coords = getWindowCenterPosition();
	var sizes  = getWindowSize();


	if(editorInfoBox == true){
		if(showInfoBox(infoBoxTitle, infoBoxContent, "infoBox", true, coords['posx'], coords['posy']) == false){
			startEditor	= true;
		}
	}else{
		startEditor		= true;
	}
	if(editorType == undefined){
		editorType	= '0';
	}
	var resizable	= 'no';
	var scrollable	= 'yes';

	if(editorType == '0'){
		resizable	= 'yes';
		scrollable	= 'no';
	}
	if(startEditor == true){

		/*coords['wd'] = sizes['wd'];//1000;
		coords['ht'] =  sizes['ht'];//700;
		var editorContainer  = document.getElementById('editorContainer');
		editorContainer.style.display = 'block';
		editorContainer.style.width 	= sizes['wd']+"Px";
		editorContainer.style.height 	= sizes['ht']+"Px";
		
		
		var editorFrame  = document.getElementById('editorFrame');
		editorFrame.src = url;
		editorFrame.style.display = 'block';
		editorFrame.style.left 		= (coords['posx']-coords['wd']/2)+"Px";
		editorFrame.style.top 		= (coords['posy']-coords['ht']/2)+"Px";
		
		editorFrame.style.width 	= coords['wd']+"Px";
		editorFrame.style.height 	= coords['ht']+"Px";*/
		
		editor = openWindow(url, 1010, 775, '50', '0', 'Editorwindow', scrollable , resizable);
		////editor = openWindow(url, 790, 585, '50', '0', 'Editorwindow', scrollable , resizable);
		editor.focus();
		hideInfoBox();
		if(editor){
			i = window.setInterval("checkEditorWin("+imageNotify+")", 500);
		}
	}
}



function openInlineEditor(url, imageNotify, editorInfoBox, editorType)
{

	var styles= "vertical-align:bottom;	width:400px;	height:21px;	clear:left;	padding-bottom:0px;	";
	var infoBoxTitle 	= 	txt_advise_title;
	var infoBoxContent 	= 	txt_advise_text	;
	infoBoxContent=infoBoxContent.replace(/%buttons%/g, 
							"<ul>"+
								"<li class=\"buttonBox\"><a  href=\"ws-myprinteria-registrieren-register,registerstep1.html\">"+btn_advise_register+"</a></li>"+
								"<li class=\"buttonBox\"><a  href=\"javascript:openEditor('"+url+"')\">"+btn_advise_editor+"</a></li>"+
							"</ul>");

	var startEditor		= false;
	var coords = getWindowCenterPosition();
	var sizes  = getWindowSize();
	var frameWidth 	= 900;
	var frameHeight = 700;
	/*var frameWidth 	= 503;
	var frameHeight = 450;*/
	/*if(editorInfoBox == true){
		if(showInfoBox(infoBoxTitle, infoBoxContent, "infoBox", true, coords['posx'], coords['posy']) == false){
			startEditor	= true;
		}
	}else{*/
		startEditor		= true;
	//}
	if(editorType == undefined){
		editorType	= '0';
	}
	var resizable	= 'no';
	var scrollable	= 'yes';

	if(editorType == '0'){
		resizable	= 'yes';
		scrollable	= 'no';
	}
	if(startEditor == true){

		//coords['wd'] = sizes['wd']-40;//1000;
		//coords['ht'] =  sizes['ht']-40;//700;
		var editorContainer  = document.getElementById('editorContainer');
		editorContainer.style.display = 'block';
		editorContainer.style.width 	= sizes['wd']+"Px";
		//alert(sizes['ht']+":"+sizes['fht']);
		
		if( parseInt(sizes['ht']) > parseInt(sizes['fht']) ){
			sizes['fht'] = sizes['ht'];
		}
		editorContainer.style.height 	= sizes['fht']+"Px";
		

		
		
		var editorFrame  = document.getElementById('editorFrame');
		editorFrame.src = url;
		editorFrame.style.display = 'block';
		
		
		editorFrame.style.width 	= sizes['wd']	+"Px";
		editorFrame.style.height 	= sizes['ht']	+"Px";
		editorFrame.style.width 	= frameWidth	+"Px";
		editorFrame.style.height 	= frameHeight	+"Px";
		
		var x = parseInt(parseInt(coords['posx'])-parseInt(frameWidth/2));
		var y = parseInt(parseInt(coords['posy'])-parseInt(frameHeight/2));
		if(x<0){x=0;}
		if(y<0){y=0;}
		editorFrame.style.left 		= x+"Px";
		editorFrame.style.top 		= y+"Px";

		//editor = openWindow(url, 1010, 775, '50', '0', 'Editorwindow', scrollable , resizable);
		////editor = openWindow(url, 790, 585, '50', '0', 'Editorwindow', scrollable , resizable);
		//editor.focus();
		hideInfoBox();
		//if(editor){
		//	i = window.setInterval("checkEditorWin("+imageNotify+")", 500);
		//}
	}
	return false;
}


var EDITOR_EXITSTATUS_CANCEL 	= "0";
var EDITOR_EXITSTATUS_BASKET 	= "1";
var EDITOR_EXITSTATUS_PREVIOUS 	= "2";
var EDITOR_EXITSTATUS_NEXT 		= "3";

function closeEditor(status)
{
	window.clearInterval(i);
	
	switch(status){
	case EDITOR_EXITSTATUS_BASKET:
		location.href = "ws-warenkorb-order,cart.html";
		break;
		
	case EDITOR_EXITSTATUS_CANCEL:
	default:
		var new_url 	= location.href;
		var new_urldata = new_url.split("#");
		location.href = new_urldata[0];
	}
}

function closeInlineEditor()
{
	var editorContainer  = document.getElementById('editorContainer');
	editorContainer.style.display = 'none';
	var editorFrame  = document.getElementById('editorFrame');
	editorFrame.style.display = 'none';
	location.href = location.href;
}



/**
 * function checkEditorWin
 * checks editor window if it is closed
 * then reload mainwindow
 */
function checkEditorWin(imageNotify) {
	if(editor == undefined && z == false){
		if(imageNotify)
			openImageNotify();
			
		closeEditor(EDITOR_EXITSTATUS_BASKET);
		/*
		window.clearInterval(i);
		z = "reloaded";

		var new_url 	= location.href;
		var new_urldata = new_url.split("#");
		location.href = new_urldata[0];
		//location.href = "ws-warenkorb-order,cart.html";
		*/
	}

	if (editor.closed  && z == false) {
		if(imageNotify)
			openImageNotify();

		closeEditor(EDITOR_EXITSTATUS_CANCEL);
		/*window.clearInterval(i);
		z = "reloaded";

		var new_url 	= location.href;
		var new_urldata = new_url.split("#");
		location.href = new_urldata[0];
		//location.href = "ws-warenkorb-order,cart.html";*/
	}
}

/**
 * function trim
 * trims the given string on both sides
 */
function trim(str) {
  	str=str.replace(/^\s*(.*)/, "$1");
  	str=str.replace(/(.*?)\s*$/, "$1");
  	return str;
}


function selectImageGroup(item)
{
	var p = document.getElementById('detailImagePreview0');
	var i=0;
	while(p!=null){
		p.style.borderBottomWidth='0Px';
		p.isSelected=false;
		i++;
		p = document.getElementById('detailImagePreview'+i);
	}
	item.style.borderBottomWidth='3Px';
	item.isSelected=true;
}

function hoverImageGroup()
{
	var p = document.getElementById('detailImagePreview0');
	var i=0;
	while(p!=null){
		if(p.isSelected==undefined || p.isSelected!=true) {
			p.style.borderBottomWidth='0Px';
		}
		i++;
		p = document.getElementById('detailImagePreview'+i);
	}
}

function ticker(xpos, msg) {
	var o = " ";
	var c = 1;
	for (c=0 ; c < xpos ; c++) {
		o+=" ";
	}
	xpos--;
	o+=msg;

	window.status=o;
	setTimeout("ticker("+xpos+",'"+msg+"')",100);
}

function startTicker(msg)
{
	if(getCookieValueByName('tickerstarted')) {
		tickerstarted = document.cookie;
		window.status=msg;
	} else {
		document.cookie = "tickerstarted=true;";
		ticker(100, msg);
	}
}


function selectCheckBoxes(rootname,value)
{
	var box_index=0;
	var box_node = document.getElementsByTagName('input');
	for(var i=0;i<box_node.length;i++){
		
		if(box_node[i].type!='checkbox' ||
		   box_node[i].name=='check_select_all' ||
		   box_node[i].disabled==true){
			continue;
		}
		box_node[i].checked = value;
	}
	
}

/**
 * google conversion hack - track it onclick without google analytics
 */
function trackGoogleConversion(){
	if(!window.gConActive){
		return true;
	}

	//document.body.innerHTML += "<img height=\"1\" width=\"1\" border=\"0\" src=\"http://www.googleadservices.com/pagead/conversion/"+window.gConID+"/?label="+window.gConLabel+"&amp;guid=ON&amp;script=0\"/>";
	return true;
}




function showCookiesFrame(frameWidth, frameHeight)
{
	var coords = getWindowCenterPosition();
	var posStyle = "";
	var posX= coords['posx'];
	var posY= coords['posy'];
	
	if(posX != undefined && posY != undefined){
		//posStyle = "left:350px;top:"+(posY-70)+"px;";
		//posStyle = "left:"+(posX-50)+"px;top:"+(posY-70)+"px;";
	}
	
	if(document.all){
		var infoBoxDiv = document.getElementById('cookieframe_iframe_container');
		infoBoxDiv.style.display = 'block';
		infoBoxDiv.style.left    =  (coords['posx']-frameWidth/2)+"Px";
		infoBoxDiv.style.top     =  (coords['posy']-frameHeight/2)+"Px";
		infoBoxDiv.style.width	=	frameWidth+"Px";
		
	}
	
	var d = document.getElementById('cookieframe_container');
	d.style.display = 'block';
	d.style.left    =  (coords['posx']-frameWidth/2)+"Px";
	d.style.top     =  (coords['posy']-frameHeight/2)+"Px";
	d.style.width	=	frameWidth+"Px";
	//d.style.height	=	frameHeight+"Px";
}



function hideCookiesFrame()
{
	var d = document.getElementById('cookieframe_container');
	d.style.display = 'none';
	var infoBoxDiv = document.getElementById('cookieframe_iframe_container');
	infoBoxDiv.style.display = 'none';
}

var checkLockUser = "";




function checkDocumentLocks(sessID, appID, userID, documentID)
{

	var ajax = new AjaxRequest();

	var response = ajax.execute("ajaxReq="+
		'{"sessionid":"'+sessID+'","requestMethod":"checkDocumentLocks","remarkMethod":"responseDocumentLocks","params":{"appID":"'+appID+'","userID":"'+userID+'","documentID":"'+documentID+'"},"data":[]}',
		'POST',
		false
		);
	//alert("checkDocumentLocks locks are "+response.data.locks+"=="+(response.data.locks!=0));
	//checkDocumentLocksResponse();
	if(response==null){
		alert("NO AJAX RESPONSE");
	}
	if(response.data.locks!=0){
		checkLockUser = response.data.message;
		return false;
	}
	return true;
}

function responseDocumentLocks(params)
{
	//alert("responseDocumentLocks locks are "+params.locks+"=="+(params.locks!=0));
	
	return (params.locks!=0);
}


function callDownloadCounter(sessionId)
{
	var ajax = new AjaxRequest();
	ajax.execute("ajaxReq="+
		'{"sessionid":"'+sessionId+'","requestMethod":"setDownloadCounter","remarkMethod":"responseDownloadCounter","params":[],"data":[]}',
		"POST",
		false
		);
}

function responseDownloadCounter(params)
{
	var d = document.getElementById('downloadcounter_container');
	if(typeof(d) !== "undefined" && d!==null ){
		d.innerHTML = params['count'];
	}
}


function testJSON(sessID,categoryID)
{

	var ajax = new AjaxRequest();

	var response = ajax.execute("ajaxReq="+
		'{"sessionid":"'+sessID+'","requestMethod":"getSliderVariants","remarkMethod":"responseJSON","params":{"categoryID":"'+categoryID+'"},"data":[]}',
		'POST',
		false
		);
	//alert("checkDocumentLocks locks are "+response.data.locks+"=="+(response.data.locks!=0));
	//checkDocumentLocksResponse();
	if(response==null){
		alert("NO AJAX RESPONSE");
	}
	return true;
}

function responseJSON(params)
{
	alert(params);
	var d = document.getElementById('test_container');
	if(typeof(d) !== "undefined" && d!==null ){
		d.innerHTML = params;
	}
}

function callOrderHistory(sessionId,nid)
{
	var ajax = new AjaxRequest();
	ajax.execute("ajaxReq="+
		'{"sessionid":"'+sessionId+'","requestMethod":"getHistoryDetails","remarkMethod":"responseOrderHistory","params":[{"id":"'+nid+'"}],"data":[]}',
		"POST",
		false
		);
}

function responseOrderHistory(params)
{
	alert("ST"+params);
	/*var d = document.getElementById('downloadcounter_container');
	if(typeof(d) !== "undefined" && d!==null ){
		d.innerHTML = params['count'];
	}*/
}

