var requestSubmitted = false;
/*
 * Use this function to prevent double click handling.
 * return true when request can be submitted and false if request should not be submitted because it already has been executed.
 */
function canSubmitRequest() {
    if (!requestSubmitted ) {
        requestSubmitted  = true;
        return true;
    }
    return false;

}

/*
 * sets the cookie 'ugsPersistentSessionId' if it does not yet exist
 */
function ugs_setPersistentSessionId(){
	var cookieName = "ugsPersistentSessionId";
	if (ugs_readCookie(cookieName)== undefined) {
		// generate persistentSessionId
		var cookieValue = (new Date().getTime() * Math.floor(1000000 * Math.random()+1)).toString(16);
		ugs_createCookie(cookieName, cookieValue, 20*365 /* 20 years*/ );
	}
}

function ugs_createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function ugs_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 undefined;
}

/* 
 * Opens every popup in a new window with the dimensions 600x200 pixels and which
 * is positioned 200 pixels from the left.
 */
 function ugs_openPopUp(url, params){
	var newwindow = window.open(url,'name','width=600,top=200,left=200,'+params);
	if (window.focus) {newwindow.focus()}
	return false;
}

/* 
 * Checks a radio button field, in case it exist
 */
 function ugs_checkRadio(radioId){
	var radio_button = document.getElementById(radioId);
	if (radio_button){
		radio_button.checked = true;
	}
}

/* 
 * The function <code>ugs_showHideElement</code> displays an element or hides it. 
 * It depends on the current value of the <code>display</code> property. 
 */
function ugs_showHideElement(elementId){
	var obj = document.getElementById(elementId);
	if(obj.style.display == 'block') {obj.style.display='none';}
	else {obj.style.display='block';}
}

/* 
 * The following two functions <code>ugs_deselectedDifferentShippingAddress</code> and
 * <code>ugs_selectedDifferentShippingAddress</code> disable and accordingly enable the 
 * different shipping address fields
 */
function ugs_deselectedDifferentShippingAddress() {
	document.getElementById("shipping_address").style.display="none";
	document.getElementById("lnk_shipping_address_on").style.display="block";
	document.getElementById("useShippingAddressField").value="false";
}

function ugs_selectedDifferentShippingAddress() {
	document.getElementById("shipping_address").style.display="block";
	document.getElementById("lnk_shipping_address_on").style.display="none";
	document.getElementById("useShippingAddressField").value="true";
}

/* 
 * The following function <code>ugs_redirectOpener</code> 
 * reloads opener window.
 */
function ugs_redirectOpener() {
	window.opener.location.reload();
	window.close();
}

/* 
 * The following two functions <code>ugs_deselectedPaymentTypeElv</code> and
 * <code>ugs_selectedPaymentTypeElv</code> disable and accordingly enable the 
 * Elv transfer fields
 */
function ugs_deselectedPaymentTypeElv() {
	document.getElementById("payment.blz").className = "unobtrusive";
	document.getElementById("payment.accountNo").className = "unobtrusive";
	document.getElementById("fld_elvbin").disabled = "disabled";
	document.getElementById("fld_elvaccountnumber").disabled = "disabled";
}

function ugs_selectedPaymentTypeElv() {
 	document.getElementById("payment.blz").className = "";
	document.getElementById("payment.accountNo").className = "";
	document.getElementById("fld_elvbin").disabled = "";
	document.getElementById("fld_elvaccountnumber").disabled = "";
}

/*
 * This function checks the url for the lkz parameter. If it exists,
 * it writes a cookie with name = "lkz" and the request parameter.
 */
function writeLKZCookie()
{
  var lkz = null;
  var regexS = "[\\?&]lkz=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null ){
    return;
  } 
  ugs_createCookie("lkz",results[1], 30);
}

/* 
 * The function <code>ugs_initialize</code> contains everything which has to be loaded
 * on every page, in any case. It consists of four sections: disableFields, externalLinks
 * showNotice and addThis
 */
function ugs_initialize(){
	//disableFields
	var elv_field = document.getElementById('chk_elv');
	if (elv_field){
		if (elv_field.checked == false)
			ugs_deselectedPaymentTypeElv();
	}
	var useShippingAddressField = document.getElementById('useShippingAddressField');
	if (useShippingAddressField){
		if (useShippingAddressField.value == 'true'){
			ugs_selectedDifferentShippingAddress();
		}
	}
	//externalLinks
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
	//showNotice
    var name = "__test_cookie__";
    document.cookie = name;
    var cookieEnabled=(document.cookie.indexOf(name) != -1)? true : false;
    if (!cookieEnabled){
    	var loc = document.location.host;
    	if (loc.indexOf("www.") != -1)
    		loc = loc.substr(4,loc.length-4);
    	document.body.style.padding = '40px 0 0 0';
    	document.body.innerHTML += '<div id="site_info"><img src="/wcsstore/TTStorefrontAssetStore/images/site_info_icon.png" alt="" /><span>Bitte aktivieren Sie Cookies um diese Seite zu verwenden. Ihr Team von '+ loc +'</span></div>';
    }
	//addThis
	var addthis_config = {data_track_clickback: true};	
	
	ugs_setPersistentSessionId();
	
	//check lkz and write cookie
	writeLKZCookie();
}

/* 
 * Starts the function <code>ugs_initialize</code> <em>after</em> the dom content is loaded.
 * This is necessary because opera does not handle <code>window.onload</code> correctly
 */ 
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", ugs_initialize, false);
}

/* 
 * <code>window.onload</code> starts the initialize function for all other browsers.
 * Necessary, because <abbr title="Internet Explorer">IE</abbr> cannot handle 
 * <code>addEventListener</code>
 */
window.onload = ugs_initialize;

