domain = 'http://orient24h.pl/';
shop_domain = domain+'sklep/';

function newXmlHttp(ajaxRequest){

	var ajaxRequestName = ajaxRequest;
	try {
		ajaxRequest = new XMLHttpRequest();
	} 
	catch (e) {
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	if(ajaxRequestName == 'ObiektXMLHttp_loadSwf') {
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				var response = ajaxRequest.responseText;
				var rotator_td = document.getElementById('rotator_td');				
				rotator_td.innerHTML = response;
			}	
		}
	}	
	
	return ajaxRequest;

}

function hide_message(id) {
	alert('hide_message');
	//my_div = document.getElementById("komunikat_produktu_"+id);
  	//my_div.innerHTML = '';
}

/*
function odswiez_koszyk() {
	ObiektXMLHttp = newXmlHttp();
	if(ObiektXMLHttp) {
		zrodlo = "php/ajaxOperations.php?operacja=odswiez_koszyk";
		ObiektXMLHttp.open("GET", zrodlo,false);
		ObiektXMLHttp.send(null); 
		var response = ObiektXMLHttp.responseText;
  	    my_div = document.getElementById("lista_koszyk");
  	    my_div.innerHTML = response;
	}	
}
*/

/*
function stateChanged()
{
if (ObiektXMLHttp.readyState==4 || ObiektXMLHttp.readyState=="complete")
{
	//document.getElementById("txtHint").innerHTML=xmlHttp.responseText
	alert('ok');
}
}
*/

function openClose(id)
{
	//last = id;
	if(document.getElementById) {
		var div_dostawcy = document.getElementById(id);		
	}
	var browser = '';
	if(document.getElementById) {
	element = document.getElementById(id);
	} else if(document.all) {
	element = document.all[id];            
	} else return;
	if(element.style) {
	if(element.style.display == 'block' ){
		element.style.display = 'none';
	} else {
		element.style.display = 'block';
	}
	}
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   return -1;
}

function getSelectedRadioValue(buttonGroup) {
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}

function acceptOrder(form_name,kompletne_dane_faktura,kompletne_dane_wysylka) {

	if (kompletne_dane_faktura != 1) {
		document.getElementById('div_dane_do_faktury').style.display = 'block';
		return false;
	}
	else if (kompletne_dane_wysylka != 1) {
		document.getElementById('div_dane_do_wysylki').style.display = 'block';
		return false;
	} else {
		document.getElementById(form_name + 'Action').value = 'realizeOrder';
		document.forms[form_name].submit();
	}	
}

function changeAddresses(form_name) {
	document.forms[form_name].submit();
}

function continueShopping(url) {
	var form_name = 'shoppingCartForm';
	document.getElementById(form_name+'Action').value = 'continueShopping';
	document.getElementById(form_name+'ActionValue').value = url;	
	document.forms[form_name].submit();
}

function changeShippingMethod(shipping_id,form_name) {
	document.getElementById(form_name+'Action').value = 'changeShipping';
	document.getElementById(form_name+'ActionValue').value = shipping_id;	
	document.forms[form_name].submit();
}

function makeOrderFromShoppingCart() {
	document.getElementById('shoppingCartFormAction').value = 'realize';
	document.forms['shoppingCartForm'].submit();
}

function recalculateShoppingCart() {
	document.getElementById('shoppingCartFormAction').value = 'recalculate';	
	document.forms['shoppingCartForm'].submit();
}

function usun_z_koszyka(id,size_id,color) {
	ObiektXMLHttp = newXmlHttp();
	if(ObiektXMLHttp) {
		zrodlo = shop_domain+'php/ajaxOperations.php?operacja=usun_z_koszyka&id='+id+'&size_id='+size_id+'&color='+color;
		try {
			ObiektXMLHttp.open("GET",zrodlo,false);
		}  catch (e)
   		{
   		 	alert(e);	
   		}
		ObiektXMLHttp.send(null); 
		document.location = shop_domain+'koszyk.html';
	}
}

function usun_z_przechowalni(id,size_id,color) {
	ObiektXMLHttp = newXmlHttp();
	if(ObiektXMLHttp) {
		zrodlo = shop_domain+'php/ajaxOperations.php?operacja=usun_z_przechowalni&id='+id+'&size_id='+size_id+'&color='+color;
		try {
			ObiektXMLHttp.open("GET",zrodlo,false);
		}  catch (e)
   		{
   		 	alert(e);	
   		}
		ObiektXMLHttp.send(null); 
		document.location = shop_domain+'przechowalnia.html';
	}
}

function do_przechowalni(id,color,size_id,quantity) {
	if(quantity == '') quantity = document.getElementById('quantity').value;
	if(size_id == '') size_id = document.getElementById('size_id').value;
	//var color = document.getElementById('color_id').value;		
	ObiektXMLHttp = newXmlHttp();
	if(ObiektXMLHttp) {
		zrodlo = shop_domain+"php/ajaxOperations.php?operacja=do_przechowalni&id="+id+'&quantity='+quantity+'&size_id='+size_id+'&color='+color;
		try {
			ObiektXMLHttp.open("GET",zrodlo,false);
		}  catch (e)
   		{
   		 	alert(e);	
   		}
		ObiektXMLHttp.send(null); 
		document.location = shop_domain+'przechowalnia.html';
	}
	
}

function do_koszyka(id,color) {
	var quantity = document.getElementById('quantity').value;
	var size_id = document.getElementById('size_id').value;	
	//if(quantity == '') quantity = document.getElementById('quantity').value;
	//if(size_id == '') size_id = document.getElementById('size_id').value;
	//var color = document.getElementById('color_id').value;		
	ObiektXMLHttp = newXmlHttp();
	if(ObiektXMLHttp) {
		zrodlo = shop_domain+"php/ajaxOperations.php?operacja=do_koszyka&id="+id+'&quantity='+quantity+'&size_id='+size_id+'&color='+color;
		try {
			ObiektXMLHttp.open("GET",zrodlo,false);
		}  catch (e)
   		{
   		 	alert(e);	
   		}
		ObiektXMLHttp.send(null); 
		document.location = shop_domain+'koszyk.html';
		
		//var response = ObiektXMLHttp.responseText;
  	    //my_div = document.getElementById("komunikat_produktu_"+id);
  	    //my_div.innerHTML = response;
 	    //odswiez_koszyk();  	    
  	    //timeoutID = window.setTimeout('hide_message('+id+')', 2000);
	}
}

function setCookie(name, value, expires, path, domain, secure) {
var varCookie = name + "=" + escape(value) +
((expires)? "; expires=" + expires.toGMTString() : "") +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
((secure)? "; secure" : "");
document.cookie = varCookie;
}

function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

function fixDate(date) {
var base = new Date(0);
var delay = base.getTime();
if (delay > 0)
date.setTime(date.getTime() - delay);
} 





/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


function getCookie(name) {
var varCookie = document.cookie;
var prefix = name + "=";
var begin = varCookie.indexOf(prefix);
if (begin == -1)
return null;
var end = varCookie.indexOf(";", begin);
if (end == -1)
end = varCookie.length;
return Base64.decode(unescape(varCookie.substring(begin + prefix.length, end)));
}
