﻿// initialize on load 100% HTML code
//var browserName = navigator.appName; // Get the Browser Name
var browserCodeName = navigator.appCodeName; // Get the Browser Name

var browserName = "";
if(navigator.userAgent.indexOf("Safari")>0) browserName = "Safari";
else if(navigator.userAgent.indexOf("Firefox")>0) browserName = "Firefox";
else if(navigator.userAgent.indexOf("Opera")>=0) browserName = "Opera";
else if(navigator.userAgent.indexOf("MSIE")>0) browserName = "IE";
else if(navigator.userAgent.indexOf("Chrome")>0) browserName = "Chrome";

function init(){
    upgrade_html();
	update_height();
	updateFirmaNumer();
	cart_submit_after_refresh();
}

function updateFirmaNumer(int)
{
	if(!int)
	{
		setTimeout("updateFirmaNumer(1)",200);
		return;
	}
	//if(String(window.location).substr(urlroot.length) == '/SpecialPages/Register.aspx')
	if(String(window.location).indexOf('/SpecialPages/Register.aspx') > 0)
	{
		//alert("Wykonaj funkcję\nupdateFirmaNumer() z pliku main.js\nDelay: 1500ms");
		pokazFirme();
		pokazNumer();
	}
	return;
}
	
// Make some changes in HTML code for graphics requirements
function upgrade_html(){
    // button-round
    var id; var h; var c; var i; var a; var cl; var tg; var tp; var el_no; var div; var table; var td; var input;
    var e = document.getElementsByTagName("*");
    for(i = 0; i < e.length; i++){
        id = e[i].getAttribute("id");
		cl = e[i].className;
		tg = e[i].tagName.toLowerCase();

		if(tg == "input"){
		
			// wszystkie objekty typu input-checkbox
			if(e[i].getAttribute("type") == "checkbox"){
				e[i].setAttribute("class","chkbox");
			}
			
			// wszystkie objekty typu input-radio
			else if(e[i].getAttribute("type") == "radio"){
				e[i].setAttribute("class","radio");
				e[i].setAttribute("style",e[i].getAttribute("style")+";border: solid 1px red;");
				//alert(e[i].getAttribute("class")); break;
			}
			
			// "dalej" w koszyku - dodaj odswiezenie koszyka przed wykonaniem akcji dalej
			if( id == "plc_lt_zoneContent_SubpagePlaceholder_SubpagePlaceholder_lt_zoneLeft_ShoppingCart_cartElem_btnNext" )
			{
				var PageTitle = document.getElementById('plc_lt_zoneContent_SubpagePlaceholder_SubpagePlaceholder_lt_zoneLeft_ShoppingCart_cartElem_lblStepTitle').innerHTML;
				
				// Zmiany dla kroku 1 z 6
				if(PageTitle.indexOf("1 z 6")>0)
				{
					//alert("krok1");
					//e[i].type = 'button';
					//e[i].setAttribute('type','button');
					/*
					var newO=document.createElement('input');
					newO.setAttribute('type','button');
					newO.setAttribute('name',e[i].getAttribute('name'));
					newO.setAttribute('style',e[i].getAttribute('style'));
					newO.setAttribute('class',e[i].getAttribute('class'));
					newO.setAttribute('id',e[i].getAttribute('id'));
					newO.setAttribute('onclick','cart_submit_with_refresh();');
					e[i].parentNode.replaceChild(newO,e[i]);
					*/
					//newO.focus();
					/*
					alert(e[i].getAttribute('type'));
					*/
					//e[i].setAttribute('onclick','cart_submit_with_refresh();');
					//document.forms['form1'].onsubmit = 'cart_submit_with_refresh();';
					//c = document.getElementById("form1").innerHTML;
					if(browserName == "Firefox" || browserName == "IE")
					{
						c = document.getElementById('ShoppingCartButtonsR').innerHTML;
						c = c.replace(/type=\".*\"|type='.*'|type=submit/g,"type='button' onclickhere");
						c = c.replace(/onclick=\".*\};"|onclick='.*};'/g,"");
						c = c.replace(/onclickhere/g,"type='button' onclick='cart_submit_with_refresh(this.name);'");
						document.getElementById('ShoppingCartButtonsR').innerHTML = c;
					}
					else if(browserName == "Safari" || browserName == "Chrome" || browserName == "Opera")
					{
						var newO=document.createElement('input');
						newO.setAttribute('type','button');
						newO.setAttribute('name',e[i].getAttribute('name'));
						newO.setAttribute('style',e[i].getAttribute('style'));
						newO.setAttribute('class',e[i].getAttribute('class'));
						newO.setAttribute('id',e[i].getAttribute('id'));
						newO.setAttribute('onclick','cart_submit_with_refresh(this.name);');
						e[i].parentNode.replaceChild(newO,e[i]);
					}
				}
			}
		}
		
		// powoduje, ze nazwa użytkownika jest linkiem do panelu preferencji konta
		else if(tg == "span" && cl == "userName"){
			//e[i].setAttribute("style","border: solid 1px red;");
			e[i].innerHTML = '<a href="'+urlroot+'/partner/konto.aspx">'+e[i].innerHTML+'</a>';
		}
		
		// wszystkie elementy typu table
		else if(tg == "table"){
			e[i].setAttribute("cellspacing","0");
		}
		
		// wszystkie elementy typu label
		else if(tg == "label"){
			//e[i].setAttribute("style",e[i].getAttribute("style")+";border: solid 1px red;");
		}
		
        // panel - usun nowe linie i twarde spacje
        if(id == "panel"){
            c = e[i].innerHTML;
            c = c.replace(/\n/g,"");
			c = c.replace(/&nbsp;/g,"");
            e[i].innerHTML = c;
        }
		
		// top menu (rozwijane)
		else if(id == "menu"){
			c = e[i].innerHTML;
			c = c.replace(/Sztuka parzenia kawy/g,"<span id=\"dotted\">Sztuka parzenia kawy</span>");
			c = c.replace(/Etapy parzenia zespresso/g,"<span id=\"dotted\">Etapy parzenia espresso</span>");
			c = c.replace(/Pierwsza na świecie kapsułka z kawą KOPI LUWAK/g,"<span id=\"dotted\">Pierwsza na świecie kapsułka z kawą KOPI LUWAK</span>");
			c = c.replace(/Zobacz film/g,"<span id=\"dotted\">Zobacz film</span>");
			e[i].innerHTML = c;
		}
		
		// left side - box - menu
		else if(id == "partner"){
            c = e[i].innerHTML;
			//alert(c);
			c = c.replace(/<table|<TABLE/g,"<table border='0' cellspacing='0' cellpadding='0'");
            c = c.replace(/CMSTreeMenuTable/g,"");
			c = c.replace(/CMSTreeMenuItem/g,"");
			c = c.replace(/CMSTreeMenuLink/g,"");
			c = c.replace(/CMSTreeMenuSelectedItem/g,"");
			c = c.replace(/CMSTreeMenuSelectedLink/g,"");
			c = c.replace(/menuElem_tid_0_0/g,"");
			c = c.replace(/menuElem_tid_1_0/g,"");
			c = c.replace(/menuElem_tid_2_0/g,"");
			c = c.replace(/menuElem_tid_3_0/g,"");
			c = c.replace(/menuElem_tid_4_0/g,"");
			c = c.replace(/plc_lt_zoneRight_cmstreemenu_tid_0_0/g,"");
			c = c.replace(/plc_lt_zoneRight_cmstreemenu_tid_1_0/g,"");
			c = c.replace(/plc_lt_zoneRight_cmstreemenu_tid_2_0/g,"");
			c = c.replace(/plc_lt_zoneRight_cmstreemenu_tid_3_0/g,"");
			c = c.replace(/plc_lt_zoneRight_cmstreemenu_tid_4_0/g,"");
			c = c.replace(/_item/g,"");
			c = c.replace(/ class=\"\"/g,"");
			c = c.replace(/ class=/g,"");
			c = c.replace(/ id=\"\"/g,"");
			c = c.replace(/ id=/g,"");
			c = c.replace(/ style=\"\"/g,"");
			c = c.replace(/ style=/g,"");
			c = c.replace(/<TBODY>/g,"");
			c = c.replace(/<\/TBODY>/g,"");
			c = c.replace(/\n/g,"");
            e[i].innerHTML = c;
			//alert(c);
		}
		
		else if(id == "title"){
            c = e[i].innerHTML;
            //e[i].innerHTML = "1";
		}
		
        if(id == "Eshop-Table" && e[i].innerHTML.length > 100){
			// Zamianki
			c = e[i].innerHTML;
			c = c.replace(/ class=\"Main\"| class=Main/g," id=\"Main\"");
			c = c.replace(/ class=\"Products\"| class=Products/g," id=\"Products\"");
			c = c.replace(/ id=\"plc_lt_zoneContent_SubpagePlaceholder_SubpagePlaceholder_lt_zoneLeft_ShoppingCart_cartElem_wzdStep4_gridData\"| id=plc_lt_zoneContent_SubpagePlaceholder_SubpagePlaceholder_lt_zoneLeft_ShoppingCart_cartElem_wzdStep4_gridData/g," id=\"Products\"");
			//c = c.replace(/ class=\"CartStepTable\"| class=CartStepTable/g," id=\"CartStepTable\"");
			c = c.replace(/ class=\"th-left\| class=th-left/g," id=\"th-left\"");
			c = c.replace(/ class=\"th-spacer\"| class=th-spacer/g," id=\"th-spacer\"");
			c = c.replace(/ class=\"td-spacer\"| class=td-spacer/g," id=\"td-spacer\"");
			c = c.replace(/ class=\"td-chkbox\"| class=td-chkbox/g," id=\"td-chkbox\"");
			c = c.replace(/ class=\"td-name\"| class=td-name/g," id=\"td-name\"");
			c = c.replace(/ class=\"td-price\"| class=td-price/g," id=\"td-price\"");
			c = c.replace(/ class=\"td-quantity\"| class=td-quantity/g," id=\"td-quantity\"");
			c = c.replace(/ class=\"td-sum\"| class=td-sum/g," id=\"td-sum\"");
			c = c.replace(/<tr><td colspan=\"2\">\n\s{1,}\n\s{1,}<\/td><\/tr>/g,"");
			c = c.replace(/<td>\n\s{1,}\n\s{1,}<\/td>/g,"");
			c = c.replace(/<tr>\n\s{1,}\n\s{1,}<\/tr>/g,"");
			c = c.replace(/plc_lt_zoneContent_SubpagePlaceholder_SubpagePlaceholder_lt_zoneLeft_ShoppingCart_cartElem_wzdStep0_gridData/g,"Products");
			e[i].innerHTML = c;
			
			// przycisk "aktualizuj koszyk" w koszyku ecommerce
			for( a = 0; a < document.getElementsByTagName("input").length; a++ ){
				input = document.getElementsByTagName("input")[a];
				if(input.getAttribute("name").substr(input.getAttribute("name").length-9) == "btnUpdate"){
					var input_refresh = input;
					input.value = "";
					input.style.display = "none";
				}
				if(input.getAttribute("name").substr(input.getAttribute("name").length-7) == "btnBack"){
					input.value = " ";
				}
			}
			
			// td - ustaw padding na 0px;
			for( a = 0; a < e[i].getElementsByTagName("td").length; a++ ){
				td = e[i].getElementsByTagName("td")[a];
				if(td.getAttribute("colspan") == 2){
					if(browserName=="IE"){
						td.setAttribute("style", "");
						td.getAttribute("style").padding = "0";
					} else {
						td.style.padding = "0";
					}
				}
			}
				
			var table_products = document.getElementById("Products");
			var table_products_h = 0;
			var table_sub = document.getElementById("Sub");
			var table_sub_h = 0;
			var table_coffee = document.getElementById("Coffee");
			var table_coffee_h = 0;
			var div_bgbox = document.getElementById("BgBox");
			var div_bgbox_h = 0;
			
			//table_products.innerHTML += input_refresh;
			if(input_refresh){
				var input_refresh_html = ""
					+ "<input"
					+ " type=\""+input_refresh.type+"\""
					+ " class=\""+input.getAttribute("class")+"\""
					+ " name=\""+input_refresh.name+"\""
					+ " value=\""+input_refresh.value+"\""
					+ " id=\""+input_refresh.id+"\""
					+ " />";
				//alert(input_refresh_html);
				document.getElementById("ShoppingCartButtons").innerHTML = ""
					+ "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"padding: 0 5px 0 10px;\">"+document.getElementById("ShoppingCartButtons").innerHTML+"</td>"
					+ "<td>"+input_refresh_html+"</td></tr></table>";
			}
			
			//table_products.style.border = "0";
			//table_products.style.padding = "0";
			//table_products.style.margin = "0";
			
			if(table_products) table_products_h = table_products.offsetHeight;
			if(table_sub) table_sub_h = table_sub.offsetHeight;
			if(table_coffee) table_coffee_h = table_coffee.offsetHeight;
			
			// BgBox - ustal wielkosc
			div_bgbox_h = table_products_h + 20;
			
			// BgBox - ustaw wielkosc
			if(div_bgbox){
				if(browserName=="IE"){
					div_bgbox.setAttribute("style", "");
					div_bgbox.getAttribute("style").height = div_bgbox_h+"px";
					div_bgbox.getAttribute("style").top = "-5px";
					div_bgbox.getAttribute("style").right = "-6px";
				} else if(browserName=="Opera") {
					div_bgbox.style.height = div_bgbox_h+"px";
					div_bgbox.style.top = "5px";
					div_bgbox.style.right = "-7px";
				} else {
					div_bgbox.style.height = div_bgbox_h+"px";
					div_bgbox.style.top = "-10px";
					div_bgbox.style.right = "-10px";
				}
			}
		}
		
		if(cl == "breadbox"){
			c = e[i].innerHTML;
			//e[i].innerHTML = "1";
		}
    }
}


// Dostosuj wysokosc katrki (main content) do wysokosci elementu z boxami (lewa strona)
var left_h_base = 0;
function update_height(int){
	if(!int)
	{
		setTimeout("update_height(1)",500);
		return;
	}
	var left = document.getElementById("left");
	var left_h = left.offsetHeight;
	var data = document.getElementById("data");
	var data_h = data.offsetHeight;
	if(left_h_base == 0) left_h_base = left.offsetHeight;
	//document.write("left: "+left_h+"<br/>");
	//document.write("data: "+data_h+"<br/>");
	if( data_h <= left_h_base )
	{
		data.style.height = left_h_base + 20 + "px";
		document.getElementById("spiral-spiral").style.height = left_h_base - 10 + "px";
		if(browserName=="IE"){
			document.getElementById("spiral-border").style.height = left_h_base + 20 + "px";
		} else {
			document.getElementById("spiral-border").style.height = left_h_base - 7 + "px";
		}
	}
	else if(data_h > left_h_base)
	{
		left.style.height = data_h + 20 + "px";
		document.getElementById("spiral-spiral").style.height = data_h - 30 + "px";
		if(browserName=="IE"){
			document.getElementById("spiral-border").style.height = data_h + "px";
		} else {
			document.getElementById("spiral-border").style.height = data_h - 27 + "px";
		}
	}
	//document.getElementById("spiral-border").innerHTML = data_h;
	//alert("data:"+data_h+", left:"+left_h);
	//update_height();
	return;
}


function isEmpty(mytext) {
    var re = /^\s{1,}$/g; //match any white space including space, tab, form-feed, etc.
    if ((mytext.value.length==0) || (mytext.value==null) || ((mytext.value.search(re)) > -1)) {
        return true;
    }
    else {
        return false;
    }
}

// check to see if value is within min and max
function isWithinRange(val, min, max)
{
    return (val >= min && val <= max);    
}

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function isUnsignedInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}

function codeOf(znak) {    
    return  '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(znak);
}

var verificator_nip=[6,5,7,2,3,4,5,6,7];
var verificator_regon=[8,9,2,3,4,5,6,7]
var verificator_pesel=[1,3,7,9,1,3,7,9,1,3]

var verificator_dowod=[7,3,1,0,7,3,1,7,3,1]

function verify_nip(a)
{
    //a=a.replace(/[^0-9]/g,'');
    if (a.length!=10) return false;            
    var i,n;
    for (i=n=0;i<9;i++) n+=a.charAt(i)*verificator_nip[i];
    n%=11;
    return ( n == a.charAt(9));
}

function verify_regon(a)
{
    if (a.search(/[^0-9]/)>=0 || a.length!=9) return false;
    var i,n;
    for (i=n=0;i<8;i++) n+=a.charAt(i)*verificator_regon[i];
    n%=11;
    return ( n == a.charAt(8));
}

function verify_pesel(a)
{
    if (a.search(/[^0-9]/)>=0 || a.length!=11) return false;
    var i,n;
    for (i=n=0;i<10;i++) n+=a.charAt(i)*verificator_pesel[i];
    n%=10;
    return ( (10-n)%10 == a.charAt(10));
}

function verify_iban(a)
{
    if (a.search(/^\d\d/) >= 0){
        a = 'PL' + a;
    }
    
    if (a.search(/[^0-9A-Z]/)>=0 || a.length < 6 || a.length > 34) return false;
    var i;
    var nIBAN = a.substr(4) + a.substr(0,4);
    var mIBAN = '';
    for (i=0; i<(nIBAN.length); i++) {
        //mIBAN += (nIBAN.charCodeAt(i) < 65) ? nIBAN[i] : nIBAN.charCodeAt(i) - 55;
        mIBAN += (nIBAN.charCodeAt(i) < 65) ? nIBAN.charAt(i) : (nIBAN.charCodeAt(i) - 55);
    }
    
  	while ((mIBAN.length)>2) { mIBAN = (parseInt(mIBAN.substr(0,6), 10) % 97) + mIBAN.substr(6); }
    return (mIBAN==1);
}

/*
function verify_dowod2(a)
{
    if (a.length!=9) return false;
    var i,n, z;
    for (i=n=0;i<9;i++){         
         n+=a.charAt(i)*verificator_dowod[i];
    }
    n%=10;
    return ( (10-n)%10 == a.charAt(10));
}
*/

function verify_dowod(a) {
    a = a.toUpperCase();
    return a.match(/^[A-Z]{3}[0-9]{6}$/);
}

function verify_paszport(a)
{
    return a.length > 8 && a.length < 24;
}

function ShowAllProductDescription(item) {

    //console.log(item)
	var el = jQuery(item.parentNode).find('div')[1];
	if (el.style.display=='block') {
		el.style.display='none';
		item.innerHTML = ' więcej > ';
	} else {
		el.style.display='block';
		item.innerHTML = ' < zwiń';
	}
}

function Skype(type,url) {
	// alert(type+" "+url);
    /*
    var zespresso_url = "";
	advAJAX.get({
    	url: zespresso_url+"/shop/skype."+type+".html",
    	onSuccess : function(obj) {
    		// alert(obj.responseText);
    		document.location.href=url;
		}
	});
    */
    document.location.href=url;
}

/*
Top Navigation
*/
sfHover = function() {
    if (document.getElementById("nav")) {
        var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        for (var i = 0; i < sfEls.length; i++) {
            //var ahref = sfEls[i].getElementsByTagName("A")[0];
            //alert(sfEls[i]);
            sfEls[i].onmouseover = function() {
                this.className += " sfhover";
            }
            sfEls[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
            }
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function topNavCorrect() {
	var eLi = document.getElementById("nav").getElementsByTagName("LI");
	var label;
	// Obejmij znacznkiem <font> label w href-ie
	for (var i=0; i<eLi.length; i++) {
	    label = eLi[i].getElementsByTagName('*')[0];
	    if(label.tagName.toLowerCase()=="a" || label.tagName.toLowerCase()=="span"){
	        eLi[i].getElementsByTagName('*')[0].innerHTML = "<font>"+label.innerHTML+"</font>";
	    }
	}
	// Obejmij znacznkiem <div> podlisty <ul>
	for (var i=0; i<eLi.length; i++) {
		eLi[i].innerHTML = eLi[i].innerHTML.replace(/<UL>/g,"<div><ul>").replace(/<\/UL>/g,"</ul></div>"); // dla IE
		eLi[i].innerHTML = eLi[i].innerHTML.replace(/<ul>/g,"<div><ul>").replace(/<\/ul>/g,"</ul></div>"); // dla Chrome i innych
		//alert(eLi[i].innerHTML);
		//break;
	}
}

function cart_submit_with_refresh(button_name)
{
	var form = document.forms['form1'];
	if(button_name == 'plc$lt$zoneContent$SubpagePlaceholder$SubpagePlaceholder$lt$zoneLeft$ShoppingCart$cartElem$btnNext')
	{
		form.action += '#do_next';
		//form.submit();
		__doPostBack('plc$lt$zoneContent$SubpagePlaceholder$SubpagePlaceholder$lt$zoneLeft$ShoppingCart$cartElem$wzdStep0$btnUpdate','');
	}
	return false;
}

function cart_submit_after_refresh()
{
	var form = document.forms['form1'];
	
	var err = document.getElementById('plc_lt_zoneContent_SubpagePlaceholder_SubpagePlaceholder_lt_zoneLeft_ShoppingCart_cartElem_wzdStep0_lblError');
	if(err)
	{
		form.action = String(form.action).replace('/#do_next/','');
		return;
	}
	
	if(location.hash == "#do_next")
	{
		form.action = String(form.action).replace('/#do_next/','');
		//alert(form.action);
		__doPostBack('plc$lt$zoneContent$SubpagePlaceholder$SubpagePlaceholder$lt$zoneLeft$ShoppingCart$cartElem$btnNext','');
	}
	return;
}
