﻿/*  JavaScript Brancaia
 *  (c) 2007 Thomas Schwyn, emineo AG, Switzerland
 *  
 *
 *--------------------------------------------------------------------------*/
/******************************************************************************************/
/* LOGIN																				  */
/******************************************************************************************/
function ReadSettings() {
	var strcookies = document.cookie;
	var strUserName = readCookie("frmUserName", strcookies);
	if (strUserName != "") {
		$('loginField').value = strUserName;
	}
	if ($('loginField').value == "") {
		$('loginField').focus();
	} else {
		$('passwordField').focus();
	}
}
function WriteSettings() {
	var dtnow = new Date();
	var dtexpireDate = new Date(dtnow.getUTCFullYear() + 1, 12, 31);
	var strUserName = $('loginField').value;
	document.cookie = "frmUserName=" + strUserName + "; expires=" + dtexpireDate.toUTCString();
	return true;
}
function readCookie(name, cookies) {
	var start = cookies.indexOf("frmUserName=");
	if (start == -1) {
		return "";
	}
	start = cookies.indexOf("=", start) + 1;
	var end = cookies.indexOf(";", start);
	if (end == -1) {
		end = cookies.length;
	}
	var value = unescape(cookies.substring(start, end));
	if (value == null) {
		return "";
	} else {
		return value;
	}
}
function IsCookieDisabled() {
	if (navigator.cookieEnabled != null) {
		if (navigator.cookieEnabled == false) {
			alert("You have configured your browser to disable cookies. \n With cookies disabled, we are unable to operate e-Shop.");
			return true;
		}
	}
}
function MM_callJS(jsStr) { //v2.0
	return eval(jsStr);
}

function UnCryptMailto(s, shift) {
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(shift)); 
	}
	return r;
}
function linkTo_UnCryptMailto(s, shift)	{
	location.href=UnCryptMailto(s, shift);
}

/******************************************************************************************/
/* REGISTRATION */
/******************************************************************************************/
var submitAllowedTmp = true;
function submitAllowed() {
  var tmp = submitAllowedTmp;
  submitAllowedTmp = false;
  return tmp;
}

/******************************************************************************************/
/* WEBSITE */
/******************************************************************************************/
function disablePage() {
	var blankDiv = document.createElement("div");
	blankDiv.setAttribute("id", "disabledWebsite");
	blankDiv.style.position = "absolute";
	blankDiv.style.width = "100%";
	blankDiv.style.height = "100%";
	blankDiv.style.zIndex = 10;
	$("container").insertBefore(blankDiv, $("website"));
}
function enablePage() {
	var blankDiv = $("disabledWebsite");
	$("container").removeChild(blankDiv);
}

function submitToUrl(action, formName) {
	var form = $(formName);
	form.action = action;
	form.submit();
}

function formEnterToUrl(event, action, formName) {
	if (!event) {
		event = window.event;
	}
	if (event.which) {
		keycode = event.which;
	} else {
		if (event.keyCode) {
			keycode = event.keyCode;
		}
	}
	if (keycode == 13) {
		var form = $(formName);
		form.action = action;
	}
}

function submitViaEnter(evt) {
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var form = target.form;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13) {
		if(form.onsubmit()==true)
			form.submit();
		return false;
    }
    return true;
}
/******************************************************************************************/
/* AJAX - SKRIPTS */
/******************************************************************************************/
function nop() {
}
function getAjax(url, tagName) {
	//new Ajax.Updater(tagName, url, {method:"get"});
	//startProgBar();
	
	startPopup();
	
	new Ajax.Request(url, 
		{
  			onSuccess: function(transport){
  				var element = $(tagName)
  				element.innerHTML=transport.responseText;
  				stopPopup();
  				$('notification').update('');
				$('notification').removeClassName('notification_success');
				$('notification').removeClassName('notification_failure');
  			},
			on600:function(transfer) {
				stopPopup();
				transfer.responseText.replace(/emineo@(.*?)@emineo/, "");
				$('notification').update(RegExp.$1);
				$('notification').removeClassName('notification_success');
				$('notification').addClassName('notification_failure');
			},
			on601:function(transfer) {
				stopPopup();
				$('notification').update('Der Artikel ist zurzeit nicht in dieser Menge vorhanden, die max. Menge wurde in Ihren Warenkorb verschoben');
				$('notification').removeClassName('notification_success');
				$('notification').addClassName('notification_failure');
				new Ajax.Updater({ success: tagName }, url,
				{
					method:"post", parameters:"forwardKey=" + forward
				});
			},
			on500:function(transfer) {
				stopPopup();
				$('notification').update(transfer.statusText);
				$('notification').removeClassName('notification_success');
				$('notification').addClassName('notification_failure');
			}
  		}
  	);	
}


function colorBasketView()
{
	var ctrl = false;
	var ctrlbr = false;
	
	if(failureId == undefined && notExists == undefined){
		return;
	}
	
	failureId.each(function(elm)
	{
		ctrl = true;
		ctrlbr = true;
		$("amount"+elm).style.backgroundColor = "pink";
		$('notification').removeClassName('notification_success');
		$('notification').addClassName('notification_failure');
	});
	
	if( ctrl )
		$('notification').insert($('errorMessageForLess').innerHTML);
		
	ctrl = false;
	notExists.each(function(elm)
	{
		ctrl =true;
		//$("basket_"+elm).style.backgroundColor = "pink" ;
		$('notification').removeClassName('notification_success');
		$('notification').addClassName('notification_failure');
	});
	
	if( ctrl )
	{
		$('notification').insert((ctrlbr?"<br/>":"") + $('errorMessageForNot').innerHTML);
	}	
}

function colorArticleListView()
{

	//1. recolor all forms
	var test = $A(document.forms)
	test.each(function(form)
	{
		if(form.amount != undefined){
			form.amount.style.backgroundColor = "white";
		}
	});
	
	if(failureId == undefined && failureAmount == undefined && notExists == undefined){
		return;
	}
	
	if(failureId.length > 0){
		if($('articleBasket_'+failureId[0]) != undefined){
			$('articleBasket_'+failureId[0]).amount.value = failureAmount[0];
			$('articleBasket_'+failureId[0]).amount.style.backgroundColor = "pink";
		}
		$('notification').removeClassName('notification_success');
		$('notification').addClassName('notification_failure');
		$('notification').insert($('errorMessageForLess').innerHTML);
	}else if(notExists.length > 0){
		if($('articleBasket_'+notExists[0]) != undefined){
			$('articleBasket_'+notExists[0]).amount.value = 0;
			$('articleBasket_'+notExists[0]).amount.style.backgroundColor = "pink";
		}
		$('notification').removeClassName('notification_success');
		$('notification').addClassName('notification_failure');
		$('notification').insert($('errorMessageForNot').innerHTML);
	}
}

function postAjax(formName, url, forward, tagName) {
	var form = $(formName);
	var params='';
	if(form != undefined){
		 params = Form.serialize(form) + "&forwardKey=" + forward;
	}else{
		 params = "forwardKey=" + forward;
	}
	
	new Ajax.Updater({ success: tagName },url,
		{
			method:"post", parameters:params, evalScripts:true,
			onSuccess:function(transfer){
				$('notification').update('');
				$('notification').removeClassName('notification_success');
				$('notification').removeClassName('notification_failure');				
			},
			on600:function(transfer) {
				transfer.responseText.replace(/emineo@(.*?)@emineo/, "");
				$('notification').update(RegExp.$1);
				$('notification').removeClassName('notification_success');
				$('notification').addClassName('notification_failure');
			},
			on601:function(transfer) {
				transfer.responseText.replace(/emineo@(.*?)@emineo/, "");
				$('notification').update(RegExp.$1);
				$('notification').removeClassName('notification_success');
				$('notification').addClassName('notification_failure');
				new Ajax.Updater({ success: tagName }, url,
				{
					method:"post", parameters:"forwardKey=" + forward
				});
			},
			on500:function(transfer) {
				$('notification').update(transfer.statusText);
				$('notification').removeClassName('notification_success');
				$('notification').addClassName('notification_failure');
			}
		}
	);
}
/******************************************************************************************/
/* UTILS */
/******************************************************************************************/
function getFormattedAmount(amount) {
	if (amount.length == 1) {
		return "0.0" + amount;
	}
	if (amount.length == 2) {
		return "0." + amount;
	}
	if (amount.length > 2) {
		return amount.substring(0, amount.length - 2) + "." + amount.substring(amount.length - 2, amount.length);
	}
}
function enableElement(id, enable) {
	var block = $(id);
	if (enable) {
		block.style.display = "";
	} else {
		block.style.display = "none";
	}
}
function enableBlock(id, enable) {
	var block = $(id);
	if (enable) {
		block.style.display = "block";
	} else {
		block.style.display = "none";
	}
}
function enableAddAddress(id, selectedAddress) {
	var block = $(id);
	if (selectedAddress == -1) {
		block.style.display = "block";
	} else {
		block.style.display = "none";
	}
}

var popup;

function showPopup(title, textDiv, obj){
	if($(textDiv)==undefined){
		return;
	}
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		//alert(curleft+' '+curtop);
	}
	curtop += 15;

	if(popup) 
		popup.destroy();
	
	popup=new Popup({	
			id:'text_popup',			
			modal:false,
			loadingInfo:false,
			closable:true,
			title:title,
			content: $(textDiv).innerHTML
		});
	popup.open(curtop, curleft, 400);
}

var imgPopup;

function showImgPopup(title, textDiv, obj){
	if($(textDiv)==undefined){
		return;
	}
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		//alert(curleft+' '+curtop);
	}
	//curtop += 15;

	if(imgPopup) 
		imgPopup.destroy();
	
	imgPopup=new Popup({	
			id:'img_popup',
			isImgPopup:true,	
			modal:false,
			loadingInfo:false,
			closable:true,
			title:title,
			content: $(textDiv).innerHTML
		});
	//imgPopup.open(curtop, curleft, 400);
	imgPopup.open(document.viewport.getHeight()/3 - 100, curleft, 400);
}

function startPopup(){
	if(popup) 
		popup.destroy();
	
	popup=new Popup({	
			id:'waitForProcessing_popup',			
			modal:false
		});
		
	popup.open(document.viewport.getHeight()/2 - 100, document.viewport.getWidth()/2 - 100,	200);
}

function stopPopup(){
	if(popup)
		popup.destroy();
}
function stopImgPopup(){
	if(imgPopup)
		imgPopup.destroy();
}

function limitImageSize(imgElementName, maxWidth, maxHeight){
	// imgElementName for example: '.resizeimg'
	// maxWidth for example 100
	// maxHeight for example 60

	document.observe('dom:loaded', function( event ){	
		$$(imgElementName).each(
		function( elm )
		{
			try{
				var newWidth = 0
				var newHeight = 0
				
				var imgWidth = elm.getWidth();
				var imgHeight = elm.getHeight();
 
				// 2 situations
				// 1.) only one dimension 
				// 2.) both of them
				
				if( !(imgWidth <= maxWidth && imgHeight <= maxHeight ))
				{
					propWidth = imgWidth / maxWidth;
					propHeight = imgHeight / maxHeight;
					
					if( propWidth > propHeight )
					{
						// breite ist zu gross...
						newWidth = maxWidth;
						newHeight= imgHeight * (1/propWidth) +"px";
					}
					else
					{				
						newHeight = maxHeight;
						newWidth = imgWidth * (1/propHeight)+"px";
					}
					elm.setStyle({
						'height':newHeight
						,'width':newWidth
					});	
				}
			}catch(e){}
		});
		
		Event.stop(event);
	});
}

function callMethodInBackground(url){
	new Ajax.Request(url, 
		{
			asynchronous: false,
  			onSuccess: function(transport){}
  		});
}