var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name){
	if (document.getElementById) {
	  	this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function hideDiv(obj) {
	if (!DHTML) return;
	var x = new getObj(obj);
	x.style.visibility = 'hidden';
}

function showDiv(obj){
	var poz_left
	var poz_top
	var height = 100
	var width = 300
	
	poz_left = Math.round( (screen.width - width) / 2 )
	poz_top = 100

	if (!DHTML) return;
	var x = new getObj(obj);
	x.style.position = 'absolute';
	x.style.border = '1px solid #DCDCDC';
	x.style.width = 300;
	x.style.height = height;
	x.style.top =  poz_top;
	x.style.left =  poz_left;
	x.style.visibility = 'visible';
}

function productCard(id, lang){
	var popupWindow;
	var width = 600;
	var height = 460;
	popupWindow = window.open('index.php?module=product_card&id='+id+'&lang='+lang+'&popup=1', 'Klient', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars=yes, screenX=100, screenY=0, left=100, top=0');	
}

function productRecommend(id, lang){
	var popupWindow;
	var width = 600;
	var height = 460;
	popupWindow = window.open('index.php?module=product_recommend&id='+id+'&lang='+lang+'&popup=1', 'Klient', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars=yes, screenX=100, screenY=0, left=100, top=0');	
}

function productComments(id, lang){
	var popupWindow;
	var width = 410;
	var height = 460;
	popupWindow = window.open('index.php?module=product_comments&id='+id+'&lang='+lang+'&popup=1', 'Klient', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars=yes, screenX=100, screenY=0, left=100, top=0');	
}

function commentDetails(id){
	var popupWindow;
	var width = 550;
	var height = 600;
	popupWindow = window.open('index.php?module=product_comment_details&id='+id+'&popup=1', 'Klient', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars=yes, screenX=100, screenY=0, left=100, top=0');	
}

function redirGallery(field, lang){
	id = selectedValue(field);
	if (id > 0) {
		window.location.href = 'http://bielizna.miran.com.pl/account_gallery,'+ lang +','+id+'.html';
	}
}

/*
* Funkcja sprawdza czt dana zmienna istnieje
*/
function isSet( variable ){
	return( typeof( variable ) != 'undefined' );
}

/*
* Funkcja powoduje wyswietlanie losowych obrazkow na stronie glownej 
* korzysta z arraya imgRotatorArray
*/
function mainImageRotator(){
	//czy istnieje array imgRotatorArray
	try{
		if ( (imgRotatorArray) && (imgRotatorArray.length > 0) && (document.getElementById('imgmain')) ) {
			iterator++;
			if (iterator >= imgRotatorArray.length) {
				iterator=0;				
			}
			imageUrl  = imgRotatorArray[iterator][0];
			imageName = imgRotatorArray[iterator][1];
			activeLink = imageUrl;
			document.getElementById('imgmain').style.background='url(images/products/normal/'+imageName+') no-repeat top';
			setTimeout('mainImageRotator()', 3000);
		}
	} catch (err){}
}

/*
* Funkcja powoduje wyswietlanie losowych obrazkow na stronie glownej 
* dla oferty specjalnej - korzysta z arraya offerRotatorArray
*/
function offerRotator(lang){
	//czy istnieje array imgRotatorArray
	try{
		if ( (offerRotatorArray) && (offerRotatorArray.length > 0) && (document.getElementById('imgmain2')) ) {
			iterator++;
			if (iterator >= offerRotatorArray.length) {
				
				//biezacy element
				var elem = document.getElementById('specialOfferLink_' + iterator);
				elem.removeAttribute('class');
				elem.removeAttribute('className'); //IE6
				
				iterator=0;
			}
			imageUrl  = offerRotatorArray[iterator][0];
			offerName = offerRotatorArray[iterator][1];
			imageName = offerRotatorArray[iterator][2];
			activeLink = imageUrl;
			document.getElementById('imgmain2').style.background='url(images/mainpage/specialoffer/'+imageName+') no-repeat top';
			document.getElementById('imgmain2').onclick = function (evt) {
				window.location.href = 'specialoffer,'+lang+','+imageUrl+',,.html'
			};
			//poprzedni element
			var prev_elem = document.getElementById('specialOfferLink_' + iterator);
			if(prev_elem){
				prev_elem.removeAttribute('class');
				prev_elem.removeAttribute('className');	//IE6
			}

			//nastepny element
			var next_elem = document.getElementById('specialOfferLink_' + (iterator+2));
			if(next_elem){
				next_elem.removeAttribute('class');
				next_elem.removeAttribute('className');//IE6
			}
			
			//biezacy element - dodawanie
			var elem = document.getElementById('specialOfferLink_' + (iterator+1));
			elem.setAttribute('class', 'active');
			elem.setAttribute('className', 'active'); //IE6

			setTimeout('offerRotator(\''+lang+'\')', 4000);
		}
	} catch (err){}
}

function changeOffer(offer_id, lang){
	
	imageUrl  = offerRotatorArray[offer_id][0];
	offerName = offerRotatorArray[offer_id][1];
	imageName = offerRotatorArray[offer_id][2];
	document.getElementById('imgmain2').style.background='url(images/mainpage/specialoffer/'+imageName+') no-repeat top';
	document.getElementById('imgmain2').onclick = function (evt) {
		window.location.href = 'specialoffer,'+lang+','+imageUrl+',,.html'
	};
	
}

function setSelectedIndex(value){
	//uwaga, pierwsza pozycja jest pusta
	var field = document.forms['frmBasketAdd'].intColor;
	
	for(i=1; i < (field.options.length); i++){
		if (field[i].value == value){
			field.selectedIndex = i;
			break;
		}
	}
}

function basketDeleteConfirm(_link){
	if ( confirm( LANG_DELETE_CONFIRM ) ){
		window.location = _link;
	}
}

function populateColors(form, field){
	form.intColor.length = 0;
	//var objDivColors = document.getElementById('divColors');
	//objDivColors.innerHTML = '';
	var index = 1;
	if(selectedValue(field) > 0){
		if (colors.length > 0){
			for(i=0; i<colors.length; i++){
				if (colors[i][0] == selectedValue(field)){
					form.intColor.options[index] = new Option(colors[i][2], colors[i][1]);	
					//if (colors[i][3] != ''){
					//	objDivColors.innerHTML += '<img src="images/colors/thumb/'+ colors[i][3] +'" onMouseOver="stm(tipsmsg_text['+i+'], tipsmsg_style );" onMouseOut="htm();" onClick="setSelectedIndex('+colors[i][1]+');" style="margin:5px;">';
					//}
					index++;
				}
				
			}
		}
	} else {
		form.intColor.options.length = 0;		
	}
}

function populateStates(form, field){
	var objDivStates = document.getElementById('divStates');
	if(selectedValue(field) > 0){
		if(selectedValue(field) != 170){
			objDivStates.style.display = "none";
		} else {
			objDivStates.style.display = "block";
		}
	}
}

//funkcja nadaje ograniczeni na sposoby platnosci w zaleznosci od wybranej formy przesylki
function setPaymentRestrictions(form, currency){

	//wybor przesylki
	var shipment_id = selectedValue(form.shipment_id);

	//wybor platnosci
	var payment_id = selectedValue(form.payment_id);

	//sposob wysylki - odbior osobisty
	//shipment_id = 4, platnosc musi byc gotowka (payment_id = 3)
	if (shipment_id == 4){
		if (form.payment_id.options.length > 1){
			for (i=0;i<form.payment_id.options.length;i++){
				if (form.payment_id[i].value == 3){
					form.payment_id.selectedIndex = i;
					break;
				}
			}
		}
	} else {
		if(payment_id == 3){
			form.payment_id.selectedIndex = 0;
		}
	}
	
	form.elements["basketAction"].value = "BasketRecount";
	form.submit();

}

//funkcja nadaje ograniczeni na formy przesylki w zaleznosci od wybranego sposobu platnosci
function setShipmentRestrictions(form, currency){

	//wybor przesylki
	var shipment_id = selectedValue(form.shipment_id);

	//wybor platnosci
	var payment_id = selectedValue(form.payment_id);

	//forma platnosci - gotowka
	//payment_id = 3, wysylka musi byc Odbior osobisty (shipment_id = 4)
	if (payment_id == 3){
		if (form.shipment_id.options.length > 1){
			for (i=0;i<form.shipment_id.options.length;i++){
				if (form.shipment_id[i].value == 4){
					form.shipment_id.selectedIndex = i;
					break;
				}
			}
		}
	} else {
		if(shipment_id ==4){
			form.shipment_id.selectedIndex = 0;
		}
	}
}

//funkcja przelicza kwoty koszyka
function recountBasket(form, currency){
	
	//wybor platnosci
	var payment_id = selectedValue(form.payment_id);
	
	//zrob prezent
	//nie mozna wybrac platnosci za pobraniem
	//payment_id == 2
	if(form.make_gift.checked == true){
		if (payment_id == 2){
			if (form.payment_id.options.length > 1){
				for (i=0;i<form.payment_id.options.length;i++){
					if (form.payment_id[i].value != payment_id){
						form.payment_id.selectedIndex = i;
						alert( LANG_AUTO_CHANGE_PAYMENT );
						break;
					}
				}
			}
		}
	}

	//jeszcze raz pobieramy payment_id bo mogl zmienic sie w poprzedniej operacji
	payment_id = selectedValue(form.payment_id);
	
	//suma zamowienia
	var orderSum = products_value;
	
	//wybor przesylki
	var shipment_id = selectedValue(form.shipment_id);

	//koszt przesylki
	var shipment_cost = 0;
	for (i=0;i<shipments.length;i++){
		if (shipments[i][0] == shipment_id){
			shipment_cost = shipments[i][2];
			break;
		}
	}
	
	orderSum += shipment_cost;
	
	//koszt platnosci
	var payment_cost = 0;
	for (i=0;i<payments.length;i++){
		if (payments[i][0] == payment_id){
			payment_cost = payments[i][2];
			break;
		}
	}

	orderSum += payment_cost;
	
	//opakowanie prezentowe
	if (form.gift_package.checked == true){
		orderSum += gift_package_value;
	}
	
	var sumField = new getObj('order_sum');
	
	sumField.obj.innerHTML = orderSum.toFixed(2) + " " + currency;
	
}

String.prototype.trim = function() {
    return this.replace( /^\s+|\s+$/, "" );
}

function addClassName (elem, className) {
    removeClassName (elem, className);
    elem.className = (elem.className + " " + className).trim();
}

function removeClassName (elem, className) {
    elem.className = elem.className.replace(className, "").trim();
}
