var RITTAL = RITTAL || {}; // make sure RITTAL is available

RITTAL.rics = {
		
	internalRequestCustomerRicsAccessLevel : function(ricsCallUrl, requestAccessLevelUrl, shopParam,language, country, p06, gotoStart) {
		if(gotoStart){
			ricsCallUrl=ricsCallUrl.replace('index.html','start.html');
			ricsCallUrl=ricsCallUrl.substring(0,ricsCallUrl.indexOf('p_06='))+'p_06=';
		}
		//ricsCallUrl=ricsCallUrl.replace('index.html','homeindex.html');
		ricsCallUrl = ricsCallUrl.replace('p_03=SE','p_03=SV').replace('p_03=DK','p_03=DA').replace('p_03=SE','p_03=SV').replace('p_03=HR','p_03=EN').replace('p_03=KK','p_03=RU').replace('p_03=HR','p_03=EN').replace('p_03=RO','p_03=EN');		
		jQuery.ajaxSetup({xhrFields: { withCredentials: true } });
		
		var protocol = window.location.protocol.replace(/:/g,'')
		var param = '';
		
		if (protocol) {
			param = '?p1=' + protocol;
		}
		requestAccessLevelUrl = requestAccessLevelUrl.replace('shop',country+'-'+language + '/shop');
requestAccessLevelUrl=requestAccessLevelUrl.replace('/se/','/sv/').replace('/dk/','/da/').replace('/no/','/nb/');
language=language.replace('se','sv').replace('dk','da');
country=country.replace('uk','gb');
jQuery.ajax({
    type: 'GET',
    cache: false,
    timeout: 15000,
    url: requestAccessLevelUrl + param,

    beforeSend: function() {
    },
    success: function(response) {
                        var accessLevel = '';
                        var login = null;
                        if(response.indexOf('loginStatus')!=-1){
                                accessLevel = '&'+response.split('&')[1];
                                login = response.split('&')[2].split('=')[1];
                        }else{
                                accessLevel = ''+response;
                        }
                          var ricsCallUrlWithAccess = ricsCallUrl + accessLevel ;
                        if(-1 == accessLevel.indexOf('&p_01')){
                                accessLevel = '&p_01=0.00.00.00.00';
                                ricsCallUrlWithAccess = ricsCallUrl + accessLevel ;
                        }
                          RITTAL.rics.callOpenRICSConfiguration(ricsCallUrlWithAccess, shopParam, language, country, true, login, p06, gotoStart);

    },
    error: function(xhr, ajaxOptions, thrownError) { // if error occured
                var accessLevel = '&p_01=0.00.00.00.00';
                 var ricsCallUrlWithAccess = ricsCallUrl + accessLevel ;
                 RITTAL.rics.callOpenRICSConfiguration(ricsCallUrlWithAccess, shopParam, language, country,shopParam, false, gotoStart);

    },
    complete: function() {
    },
    dataType: 'html'
});

		
		},
	
	callOpenRICSConfiguration : function(ricsUrl, shopParam, language, country, showP7, login, p06, gotoStart) {
		if (ricsUrl) {		
			// http://rics.de.flg.int/latest/index.html?p_02=DE&p_03=DE&p_14=WEBSHOP&p_13=U&p_10=N_bjOS4cQKiPD7GE_1W71Q&p_09=956&p_01=0.00.00.00.00
			var paramArr = RITTAL.rics.getUrlVars(ricsUrl, shopParam, language, country, showP7, login, p06,gotoStart);
			var codeForHash = "";
			
			for (var i = 0; i < 24; i++) {
				var pPrefix = "p_";	
				
				if (i < 10) {
					pPrefix = pPrefix.concat("0");
				}
				
				var reqP = pPrefix.concat(i);
				var reqPValue = paramArr[reqP];	
				if (reqPValue)  {
					codeForHash = codeForHash.concat(reqPValue.toUpperCase());
				}
			}
			var currenturl = window.location.hostname.replace('http','https');

			var hashCode = '';
			var shopUrl='';			
			if(shopParam && showP7){
				var shopUrl= '&p_07=https://'+ currenturl +'/'+country+'-'+language+'/shop/cart';
			}
			if(login == 'online'){
				shopUrl=shopUrl+ '&p_22=01';
			}
			//shopUrl=shopUrl+'&p_06='+p06;
			ricsUrl = ricsUrl+ shopUrl + '&p_99=' + hashCode.concat(CryptoJS.SHA1(codeForHash)).toUpperCase();					
			var ricsWindows = window.open(ricsUrl, "_blank");
			
			if (ricsWindows) {
				ricsWindows.focus();
			}
		}
	},
	
	getUrlVars : function(ricsUrl, shopParam, language, country, showP7, login,p06,gotoStart) {
		var currenturl = window.location.hostname.replace('http','https');
	    var vars = {};
		if(shopParam && showP7){
			vars["p_07"]='https://'+ currenturl +'/'+country+'-'+language+'/shop/cart';
		}
		if(login == 'online'){
			vars["p_22"]='01';
		}
		if(gotoStart){
			vars["p_06"] = "";
		}else{
			vars["p_06"] = p06;
		}
	    var parts = ricsUrl.replace(/[?&]+([^=&]+)=([^&]*)/gi,    
	    function(m,key,value) {
	      vars[key] = value;
	    });
	    return vars;
	}	
	
};
