var loginURL = null;

var supersleight = function() {
	var root=false;
	var applyPositioning=true;
	var shim='x.gif';
	var shim_pattern=/x\.gif$/i;
	var fnLoadPngs = function() {
		if(root) {
			root=document.getElementById(root);
		} else {
			root=document;
		}
		for (var i=root.all.length-1,obj=null;(obj=root.all[i]);i--) {
			if (obj.currentStyle.backgroundImage.match(/\.png/i)!==null) {
				bg_fnFixPng(obj);
			}
			if (obj.tagName=='IMG'&&obj.src.match(/\.png$/i)!==null) {
				el_fnFixPng(obj);
			}
			if (applyPositioning&&(obj.tagName=='A'||obj.tagName=='INPUT')&&obj.style.position==='') {
				obj.style.position='relative';
			}
		}
	};
	var bg_fnFixPng = function(obj) {
		var mode='scale';
		var bg=obj.currentStyle.backgroundImage;
		var src=bg.substring(5,bg.length-2);
		if (obj.currentStyle.backgroundRepeat=='no-repeat') {
			mode='crop';
		}
		obj.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='"+mode+"')";
		obj.style.backgroundImage='url('+shim+')';
	};
	var el_fnFixPng = function(img) {
		var src = img.src;
		img.style.width=img.width+"px";
		img.style.height=img.height+"px";
		img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')";
		img.src=shim;
	};
	var addLoadEvent = function(func) {
		var oldonload=window.onload;
		if (typeof window.onload!='function') {
			window.onload=func;
		} else {
			window.onload=function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	return {
		init:function() {
			addLoadEvent(fnLoadPngs);
		},
		limitTo:function(el) {
			root=el;
		},
		run:function() {
			fnLoadPngs();
		}
	};
} ();
//supersleight.init();

function showMeasures(index) {
	checklistItemElement = document.getElementById('checklistItem_' + index);
	if (checklistItemElement) {
		childNodes = checklistItemElement.childNodes;
		for (i=0; i<childNodes.length; i++) {
			if (childNodes[i].className == "hide_measures") {
				childNodes[i].className = "show_measures";
				break;
			}
		}
	} else {
		alert("ERROR: could not find element with ID \"checklistItem_" + index);
	}
}

function hideMeasures(index) {
	checklistItemElement = document.getElementById('checklistItem_' + index);
	if (checklistItemElement) {
		childNodes = checklistItemElement.childNodes;
		for (i=0; i<childNodes.length; i++) {
			if (childNodes[i].className == "show_measures") {
				childNodes[i].className = "hide_measures";
				break;
			}
		}
	} else {
		alert("ERROR: could not find element with ID \"checklistItem_" + index);
	}
}

function setChecklistIcon(index, type) {
	iconElement = document.getElementById('icon_' + index);
	if (iconElement) {
		src = iconElement.src;
//		alert("icon src: "+src);
		i = src.lastIndexOf('/');
//		alert("index: "+i);
		path = src.substring(0,i+1);
//		alert("icon folder: "+path);
		iconElement.src = path + type + ".gif";
	} else {
		alert("ERROR: could not find element with ID \"icon_" + index);
	}
}

function showPrintDialog() {
	window.print();
}

function login() {
	jQuery("#login1 div.error_message").text("");
	jQuery("#login1 div.error_message").hide();
	var cacheKiller = myCacheKiller();
	var username = jQuery("#login_form input[name=un]").val();
	var password = jQuery("#login_form input[name=pw]").val();
	if (username.length > 0 && password.length > 0) {
		$.ajax({
			type: 'POST',
			url: contextPath + "/.magnolia/pages/sbkvPublicServicePage.html",
			data: { 
				command: "checkLogin",
				mgnlUserId: username, 
				mgnlUserPSWD: password,
				ck: cacheKiller,
				url: loginURL
			},
			success: function(data, textStatus, XMLHttpRequest){
				if (data.errors) {
	   				jQuery("#login1 div.error_message").text(getMessageWithDefault("no.access") + "!");
					jQuery("#login1 div.error_message").show();
					jQuery.get("/cms_test/sbkvpub/de/home.html?mgnlLogout=true");
				} else {
					window.location.href = loginURL;
				}
   			},
   			error: function (XMLHttpRequest, textStatus, errorThrown) {
   				alert("Sorry: An error occured!");
				jQuery.get("/cms_test/sbkvpub/de/home.html?mgnlLogout=true");
   			},
			dataType: "json"
		});
	} else {
		jQuery("#login1 div.error_message").text(getMessageWithDefault("enter.username.and.password") + "!");
		jQuery("#login1 div.error_message").show();
	}
	return false;
}

function passwordReminder() {
	jQuery("#password1 div.error_message").text("");
	jQuery("#password1 div.error_message").hide();
	jQuery("#password1 div.message").hide();
	var cacheKiller = myCacheKiller();
	var username = jQuery("#password_retreival_form input[name=username]").val();
	var url = contextPath + "/.magnolia/pages/" + "sbkvPublicServicePage" + ".html";
	if (username.length > 0) {
		jQuery("#password1 input[type=submit]").attr('disabled','disabled');
		$.ajax({
			type: 'POST',
			url: url,
			data: { 
				username: username,
				command: "mailPasswordReminder",
				language: language,
				ck: cacheKiller
			},
			success: function(data, textStatus){
				if (data.errors) {
					var errorHTML = getMessageWithDefault("errors.occurred") + ":";
					errorHTML += "<ul>";
					for (var errorKey in data.errors) {
						errorHTML += "<li>" + getMessageWithDefault(errorKey) + ": " + getMessageWithDefault(data.errors[errorKey]) + "</li>";
					}
					errorHTML += "</ul>";
					
					// loop ofer alll error messages
	   				jQuery("#password1 div.error_message").html(errorHTML);
					jQuery("#password1 div.error_message").show();
					jQuery("#password1 input[type=submit]").removeAttr('disabled');
				} else {
					jQuery("#password1 div.message").text(getMessageWithDefault("password.mailed"));
					jQuery("#password1 div.message").show();
				}
   			},
   			error: function (XMLHttpRequest, textStatus, errorThrown) {
   				jQuery("#password1 div.error_message").text(getMessageWithDefault("errors.occurred") + ": " + errorThrown);
				jQuery("#password1 div.error_message").show();
				jQuery('#password1').dialog('open');
   			},
			dataType: "json"
		});
	} else {
		jQuery("#password1 div.error_message").text(getMessageWithDefault("enter.username") + "!");
		jQuery("#password1 div.error_message").show();
	}
	return false;
}

function getMessageWithDefault(key, defaultMessage) {
	var message = sbkvMessages[key];
	if (!message) {
		message = defaultMessage;
	}
	if (!message) {
		message = key;
	}
	return message;
}

function myCacheKiller() {
	return new Date().getTime();
}
