/**
 * Functions that may be used in all shop pages.
 *
 * Contains data from former vodafone.global.shop.js and catalogUtils.js.
 *
 */

/**
 * Object to store language strings
 */
if(!window.VFLang) {
	VFLang = new Object();
}



/**
 * Functional access to popup interface. May be used by the application,
 * but also by RedDot content snippets.
 */
function inlinePopupInterface(url, type, iframeWidth, iframeHeight, classes) {

	// Check whether required jquery plugi is available
	if ($().inlinePopup && !navigator.userAgent.match(/(iPhone|iPod|iPad)/)) {
		$('<a></a>').inlinePopup({
			isAjax : (type == 'ajax'),
			isIframe : (type != 'ajax'),
			isPrintButtons : (type == 'ajax'),
			targetUrl : url,
			isOnLoad : true,
			iframeWidth : iframeWidth,
			iframeHeight : iframeHeight,
			classes : classes
		});
	}

	// Fallback to no-plugin version if required
	else {
		popup_zentriert(url, "popupInterfaceFallbackWindow", (iframeWidth ? iframeWidth : 800), (iframeHeight ? iframeHeight : 600), "scrollbars=1");
	}

}



/**
 * Popup functions.
 */
function Fenster(Link,WinWidth,WinHeight,WindowName,posLeft,posTop) {
  newWindow=top.window.open(Link,WindowName,'toolbar=0,width='+WinWidth+',left=230,top=100,height='+WinHeight+',location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0');
  newWindow.focus();
}

function openPopup(url,wName,w,h) {
	var wl = (screen.width-w)/2;
	var wt = (screen.height-h)/2;
	var params  ='height='+h+',width='+w+',top='+wt+',left='+wl;
	params +=",scrollbars,resizable";
	popWin = window.open(url, wName, params);
	return false;
}

function popup_zentriert(url, name, width, height, parms) {
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (parms) { winParms += "," + parms; }
   var win = window.open(url, name, winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}



/**
 * Shopping cart / proposition manipulation functions.
 *
 */

/* Variable to know if the "In Den Warenkorb" button has already been clicked so that there is no double submission.  */
var alreadyClicked = false;

/* Function to Alert the User that the current Shop has changed before submitting the Form. */
function alertShopChange(formId, switchedShop)  {

	var addForm = document.forms[formId];

    if (!alreadyClicked) {
        if (switchedShop==true) {
            input_box = confirm(unescape(VFLang.shopSwitched));


            if (input_box==true)  {
                /*we only want to pop it once - So now we can put the switchShop cookie to done -- if we dont hit cancel*/
                //setCookie("ShopSwitched","false");
                /*we poped the message once --no need to do it again unless we switch shops*/
                setCookie("ShopSwitched","done");

                addForm.submit();
            }

        } else {

            addForm.submit();
        }
    }
}

/* Function to Alert the User that the current Shop has changed before viewing the cart. */
function alertShopChangeForCart(formId, switchedShop)  {
    var addForm = document.forms[formId];
    if (!alreadyClicked) {
        if (switchedShop==true) {
        	input_box = confirm(unescape(VFLang.shopSwitched));
            if (input_box==true)  {
                /*we only want to pop it once - So now we can put the switchShop cookie to done -- if we dont hit cancel*/
                //setCookie("ShopSwitched","false");
                /*we poped the message once --no need to do it again unless we switch shops*/
                setCookie("ShopSwitched","done");
                //We want to stay in the same page
                addForm.action = location.href;
                addForm.submit();
            }

        } else {
            addForm.submit();
        }
    }
}

/* Disables the possibility to press the "In Den Warenkorb" button */
function disableAddToCart() {
    alreadyClicked = true;
}

/* Function to refresh the page when changing a selection (for example with radio buttons)  */
function reloadOnChangedSelection(formId) {

    var form = document.forms[formId];

    if(form) {
      form.submit();
      disableAddToCart();
    }

}



/**
 * Cookie functions
 *
 */

/*Function to set a cookie */
function setCookie(name, value, expires, path, domain, secure) {

    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/*Functions to manage the cookies - Switch shop*/
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/*this deletes the cookie when called*/
function deleteCookie( name, path, domain ) {

    if ( getCookie( name ) )
        document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



/**
 * Shop selection cookie handling.
 *
 */

/*Function to set the shop cookie to either business or the default shop - if we switch shops we set the ShopSwitched to true*/
function setCookieForShop(shop){

    if (shop == getCookie("Shop")){
        setCookie("ShopSwitched","false");
    } else {

        setCookie("Shop",shop);
        if(getCookie("CARTCOUNT")>0){
            setCookie("ShopSwitched","true");
        }

    }

}

/*Get the value of switch cookie to verify if we switched shops*/
function getSwitchedValue(value){
    if(getCookie('ShopSwitched')=="done"){
        switched =false;
    }
    else{
        switched = value;
    }
    return switched;
}

/**
 * Empty function that can be overriden by inline function to implement tracking functionality.
 * Having the empty function in here prevents javascript errors.
 *
 */
function triggerTrackingRequest(PixelCode, PixelComment) {
}

/**
 * Modify content of cssClassName (e.g. ".priceTag"), superscript decimal price part
 * Currently not used because of some layout problems
 */

function styleDecimalPrice(cssClassName) {
	$(cssClassName).each(function(){
		var strPrice = $.trim($(this).text());
		var strResult = strPrice.substring(0, strPrice.indexOf(",")+1) + "<span>" + strPrice.substr(strPrice.indexOf(",")+1, 2) + "</span>" + strPrice.substring(strPrice.indexOf(",")+3, strPrice.length);
		$(this).html(strResult);
	});
}
