function menu(id){
	if(document.getElementById(id).style.display == 'inline')
		document.getElementById(id).style.display = 'none';
	else 	
		document.getElementById(id).style.display = 'inline';
}

function show(name,width,height) {
		
	no=window.open('', 'zoomv', 'width='+width+', height='+height+', toolbar=no, menubar=no,'+
				'scrollbars=no, resizable=no, copyhistory=no');
	with (no.document)
	{
		open();
		writeln("<html>\n<title>Fotogalerie</title>\n<body topmargin='0' leftmargin='0' rightmargin='0' bottommargin='0' bgcolor='#000000' style='_text-align:center;'>\n");
		writeln('<img src="'+name+'" border="0" ></a>');
		writeln("\n</body>\n</html>");
		close();
	}
}

function select_all() {
	
	users_select = document.getElementById('email_select');
	
	for(i = 0; i < users_select.length; i++) {
		users_select[i].selected = true;
	}

}


function unselect_all() {
	
	users_select = document.getElementById('email_select');
	
	for(i = 0; i < users_select.length; i++) {
		users_select[i].selected = false;
	}

}

function delete_children() {
	
	var payment_table = document.getElementById('payment');
	
	var kids = payment_table.childNodes;
	var numKids = kids.length;
	var i = 0;
	
	//alert(numKids)
	
	for(i = numKids; i > 1; i--) {
		
		//alert(kids[i]+'node '+i+' pruchod '+i);
		if(kids[i] != null) payment_table.removeChild(kids[i]);
	}
}


function postalPrice(price) {
	
	var postal_price = document.getElementById('postal_price');
	postal_price.value = price;
	
}

function setPayment(price,currency_name) {

	var payment = document.getElementById('payment_price');
	payment.value = price+' '+currency_name;
	
	update_price_all(currency_name)
	
}

//funkce aktualizuje celkovou cenu
function update_price_all(currency_name) {
	
	var products_price = document.getElementById('products_price');
	var postal_price = document.getElementById('postal_price');
	var payment_price = document.getElementById('payment_price');
	var price_all = document.getElementById('price_all');
	
	var products = products_price.value;
	var postal = postal_price.value;
	var payment = payment_price.value;
	var all = 0;
	
	products = products.split(' ');
	postal = postal.split(' ');
	payment = payment.split(' ');
	
	all += parseFloat(products[0]);
	all += parseFloat(postal[0]);
	all += parseFloat(payment[0]);
	
	price_all.value = all+' '+currency_name;
}



//funkce generuje tabulku moznosti typu plaby
function getPayment(id,price,currency_name) {
	var select = document.getElementById('select');
	
	if(select.value != 0) {
		var invisible = document.getElementById('payment_'+select.value);
		invisible.style.display = 'none';
	}

	var payment = document.getElementById('payment_'+id);
	payment.style.display = 'inline';
	
	select.value = id;
	
	postalPrice(price+' '+currency_name);
	update_price_all(currency_name);
	
}


//zkontroluje zda jsou vybrane vsechny moznosti
function checkData(message) {
	
	var postal = document.getElementById('step_2');
	var checked = false;
	
	for(i = 0; i < postal.elements['poslat'].length; i++) {
		
		if(postal.elements['poslat'][i].checked == true) checked = true;
	}
	
	
	
	if(postal.elements['payment'] != null) {
		checked = false;
		
		/*for(i = 0; i < postal.elements['payment'].length; i++) {
			
			alert('Ahoj 2');
			if(postal.elements['payment'][i].checked == true) checked = true;
		}*/
		
		if(postal.elements['payment'].checked == true) checked = true;
	}
	
	
	if(checked == true) return true;
	else {
		alert(message);
		return false;
	}
}

function copy_address() {
	document.getElementById('pname').value = document.getElementById('name').value;
	document.getElementById('psurname').value = document.getElementById('surname').value;
	document.getElementById('paddress').value = document.getElementById('address').value;
	document.getElementById('ptown').value = document.getElementById('town').value;
	document.getElementById('pcompany').value = document.getElementById('company').value;
	document.getElementById('pzip').value = document.getElementById('zip').value;
	document.getElementById('pcountry').value = document.getElementById('country').value;
	
}

function changeDescription(id, count) {
	/*
	var pole = eval('{pole:[{"id":"1","name":"name1","description":"description1"},'
		+ '{"id":"6","name":"name6","description":"description6"},'
		+ '{"id":"4","name":"name4","description":"description4"},'
		+ '{"id":"22","name":"name22","description":"<table><tr><th colspan=\"2\">test tabulka</th></tr><tr><td>prvni</td><td>druhy</td></tr></table>"}]}');
	
	var poleIndex = null;
	for(var i = 0; i < pole.length; i++) {
		if(pole[i].id == id) {
			poleIndex = i;
			break;
		}
	}
	
	var div = document.getElementById('secondDescription');
	div.innerHTML = pole[poleIndex].description;
	*/
		
	
	//var descriptions = eval(document.getElementById('secondDescription').value);
	//alert(descriptions.length);
	
	for(var i = 0; i < count; i++) {
		
		document.getElementById('multiDescription_' + i).style.display = 'none';
	}
	
	document.getElementById('multiDescription_' + id).style.display = 'block';
	
	return false;
}


function changeMenu() {

	var select = document.getElementById('selectMenu');
	var  elem = select.options[select.selectedIndex]; 
	var url = elem.value;
	
	window.location.href=url;
}

function changePhoto(newUrlMiddle, newUrlBig) {
	document.getElementById('mainPhotoA').href = newUrlBig;
	document.getElementById('mainPhoto').src = newUrlMiddle;
}

window.onload = onLoadStars;

function onLoadStars() {
	var starsDivList = getStarsDiv();
	
}

function getStarsDiv() {
	var divList = document.getElementsByTagName('div');
	
	if(divList.length > 0) {
		var starsDivList = Array();
		for(var i = 0; i < divList.length; i++) {
			if(divList[i].className == 'star' || divList[i].className == 'star3') {
				//starsDivList.push(divList[i]);
				starsDivList.push(new ClassStars(divList[i]));
			}
		}
		return starsDivList;
	}else{
		return null;
	}
}

function ClassStars(starsDiv) {
	this.starsDiv = starsDiv;
	this.widthDiv = starsDiv.parentNode;
	this.origWidth = this.widthDiv.style.width;
	this.productId = this.getProductId();
	
	this.stars;
	
	this.initStars();
}

ClassStars.prototype.getProductId = function() {
	//this.productId = this.starsDiv.id.substr(4);
	return this.starsDiv.id.substr(4);
}

ClassStars.prototype.initStars = function() {
	this.stars = this.starsDiv.getElementsByTagName('a');
	if(this.stars.length > 0) {
		
		for(var i = 0; i < this.stars.length; i++) {
			var star = new Star(this.stars[i],i,this);
			event(star, 'onMouseOver');
			event(star, 'onMouseOut');
			event(star, 'onClick');
		}
	}
}

/**
*	Dle toho nad kterou jsem hvezdickou nastavi delku podbarvovaciho divu
*/
ClassStars.prototype.setWidthByStars = function(starPosition) {
	var width = starPosition * 16.6; //16 - zirka jedne hvezdicky
	
	this.widthDiv.style.width = width+'px';
}

ClassStars.prototype.setOriginalWidth = function() {
	this.widthDiv.style.width = this.origWidth;
}

ClassStars.prototype.starClicked = function(starPosition) {
	//ajaxem predam hodnoceni
	//alert('/admin/ajax_set_rate.php?productId='+this.productId+'&rate='+starPosition);
	objectAjax('/admin/ajax_set_rate.php?productId='+this.productId+'&rate='+starPosition,this);
	
	
}

ClassStars.prototype.ajax = function(req) {
	//alert(req);
	var json = eval('(' + req + ')');
	this.origWidth = (json.avgRate * 17)+'px';
}

function Star(aElement,position,classStars) {
	this.a = aElement;
	this.position = position + 1;
	this.classStars = classStars;
}

Star.prototype.onMouseOver = function(e) {
	this.classStars.setWidthByStars(this.position);
}

Star.prototype.onMouseOut = function(e) {
	this.classStars.setOriginalWidth();
}

Star.prototype.onClick = function(e) {
	this.classStars.starClicked(this.position);
	
}

Star.prototype.getElement = function() {
	return this.a;
}

function event(element, event) {
	
	switch(event) {
		
	case 'onMouseOver':
		try {	//ff + opera
			element.getElement().addEventListener('mouseover', function(e) {element.onMouseOver(e)},false);
		}catch(e){
			//ie
			element.getElement().attachEvent('onmouseover', function(e) {element.onMouseOver(e)});
		}
	break;
	case 'onMouseOut':
		try {
			element.getElement().addEventListener('mouseout', function(e) {element.onMouseOut(e)},false);
		}catch(e){
			element.getElement().attachEvent('onmouseout', function(e) {element.onMouseOut(e)});
		}
	break;
	case 'onClick':
		try {
			element.getElement().addEventListener('click', function(e) {element.onClick(e)},false);
		}catch(e){
			element.getElement().attachEvent('onclick', function(e) {element.onClick(e)});
		}
		//toto mozna pujde predelat jinak, potrebuji, kdyz kliknu na <a>, aby se to neodeslalo
		if(element.getElement().nodeName == 'A') {
			element.getElement().onclick = function() {return false;};
		}
	break;
		
	}
}

function changeMultiDescription(elem,identif) {
	var oldElem = getOldMenuMD(identif);
	if(elem != oldElem) {
		changeMD(elem,oldElem,identif);
	}
	return false;
}

function showMD(elemId) {

	if(elemId) {
		var  elem = document.getElementById('multiDescription_' + elemId);
		elem.style.display = 'block';
	}

}

function hideMD(elemId) {
	if(elemId) {
		var  elem = document.getElementById('multiDescription_' + elemId);
		elem.style.display = 'none';
	}
}

function getMDHash(url) {
	if(url.indexOf("#") >= 0) {
		var urlParts = url.split('#');
		if(urlParts.length == 2) {
			return urlParts[1];
		}
	}
	return null;
}

function getElemMDId(elem) {
	if(elem) {
		return getMDHash(elem.href);
	}

	return null;
}

function changeMD(newElement, oldElement, identif) {
	newElement.className = 'multiDescriptionActive'+identif;
	showMD(getElemMDId(newElement));
	if(oldElement) {
		oldElement.className = 'multiDescriptionUnactive'+identif;
		hideMD(getElemMDId(oldElement));
	}


}

function getOldMenuMD(identif) {
	//var actualHashP = document.location.hash;
	//if(actualHashP) {
	//	return actualHash = actualHashP.substr(1);
	//}else{
		var changeMenu = document.getElementById("multiDescriptionChange"+identif).getElementsByTagName("a");
		for(var i = 0; i < changeMenu.length; i++) {
			if(changeMenu[i].className == 'multiDescriptionActive'+identif) {
				return changeMenu[i];
			}
		}
		return null;
	//}
}

function getMDElemByHash(hash, identif) {
	var changeMenu = document.getElementById("multiDescriptionChange"+identif).getElementsByTagName("a");
	for(var i = 0; i < changeMenu.length; i++) {
		if(getElemMDId(changeMenu[i]) == hash) {
			return changeMenu[i];
		}

	}
	return null;
}

