//doplnime funkci trim
function trim(string_to_trim)
{
	return string_to_trim.replace(/^\s+|\s+$/g,"");
}


//kopirovani adres v registraci
//kopie kontaktni adresy do dodaci
function copyContactAddressToDelivery( checkbox, isChecked )
{
	if ( isChecked == true)
	{
		with (window.document.fRegistration) {
			person2.value = trim(name.value + " " + surname.value);
			street2.value = trim(street1.value);
			city2.value = trim(city1.value);
			zip2.value = trim(zip1.value);
			country2.value = trim(country1.value);
			checkbox.checked = false;
		}
	}	
}
//kopie kontaktni adresy do fakturacni
function copyContactAddressToInvoice( checkbox, isChecked )
{
	if ( isChecked == true)
	{
		with (window.document.fRegistration) {
			firmName.value = trim(name.value + " " + surname.value);
			person3.value = trim(name.value + " " + surname.value);
			street3.value = trim(street1.value);
			city3.value = trim(city1.value);
			zip3.value = trim(zip1.value);
			country3.value = trim(country1.value);
			checkbox.checked = false;
		}
	}	
}
//kopie fakturacni adresy do dodaci
function copyInvoiceAddressToDelivery( checkbox, isChecked )
{
	if ( isChecked == true)
	{
		with (window.document.fRegistration) {
			person2.value = trim(person3.value);
			firmname2.value = trim(firmName.value);
			street2.value = trim(street3.value);
			city2.value = trim(city3.value);
			zip2.value = trim(zip3.value);
			country2.value = trim(country3.value);
			checkbox.checked = false;
		}
	}	
}
//funkce isNull - jestli hodnota objektu je null nebo prazdna vrati true
function isNull(obj)
{
	return ( obj.value == null || obj.value == "" ) ? true : false;
}
//kontrola, jestli jsou spravne vyplneny udaje registrace
function checkRegistration()
{
	message = "";
	formValidation = true;
	with (window.document.fRegistration) {
	
		//kontrola kontaktni adresy
		if ( isNull(userName) ) { message += "Nebylo vyplněno uživatelské jméno.\n"; formValidation = false; }
		if ( userPass.value != userPass2.value ) { message += "Potvrzující heslo nesouhlasí. Pokud nechcete měnit existující heslo, nechte obě pole prázdná.\n"; formValidation = false; }
		if ( isNull(name) ) { message += "U kontaktní adresy nebylo vyplněno jméno.\n"; formValidation = false; }
		if ( isNull(surname) ) { message += "U kontaktní adresy nebylo vyplněno příjmení.\n"; formValidation = false; }
		if ( isNull(street1) ) { message += "U kontaktní adresy nebyla vyplněna ulice.\n"; formValidation = false; }
		if ( isNull(city1) ) { message += "U kontaktní adresy nebylo vyplněno město.\n"; formValidation = false; }
		if ( isNull(zip1) ) { message += "U kontaktní adresy nebylo vyplněno PSČ.\n"; formValidation = false; }
		if ( isNull(country1) ) { message += "U kontaktní adresy nebyl vyplněn stát.\n"; formValidation = false; }
		//kontrola jestli je zadano cokoliv z fakturacni, aby byla fakturacni kompletni
		if ( (!isNull(firmName)) || (!isNull(street3)) || (!isNull(city3)) || (!isNull(zip3)) )
		{
			if ( isNull(firmName) ) { message += "U fakturační adresy nebyl vyplněn název firmy.\n"; formValidation = false; }		
			if ( isNull(street3) ) { message += "U fakturační adresy nebyla vyplněna ulice.\n"; formValidation = false; }
			if ( isNull(city3) ) { message += "U fakturační adresy nebylo vyplněno město.\n"; formValidation = false; }
			if ( isNull(zip3) ) { message += "U fakturační adresy nebylo vyplněno PSČ.\n"; formValidation = false; }
			if ( isNull(country3) ) { message += "U fakturační adresy nebyl vyplněn stát.\n"; formValidation = false; }
			if ( isNull(ico) ) { message += "U fakturační adresy nebylo vyplněno IČ.\n"; formValidation = false; }
		}
		//kontrola jestli je zadano cokoliv z dodaci, aby byla dodaci kompletni
		if ( (!isNull(person2)) || (!isNull(street2)) || (!isNull(city2)) || (!isNull(zip2)) )
		{
			if ( isNull(person2) ) { message += "U dodací adresy nebylo vyplněno jméno.\n"; formValidation = false; }		
			if ( isNull(street2) ) { message += "U dodací adresy nebyla vyplněna ulice.\n"; formValidation = false; }
			if ( isNull(city2) ) { message += "U dodací adresy nebylo vyplněno město.\n"; formValidation = false; }
			if ( isNull(zip2) ) { message += "U dodací adresy nebylo vyplněno PSČ.\n"; formValidation = false; }
			if ( isNull(country2) ) { message += "U dodací adresy nebyl vyplněn stát.\n"; formValidation = false; }
		}
	}
	if ( formValidation == false )
	{ 
		alert(message);
		return false;
	}
	else 
		return confirm('Zkontrolujte prosím, jestli je Váš e-mail správně zadán: ' + window.document.fRegistration.email.value + "?");
}

//kontrola, jestli jsou dostatecne vyplneny fakturacni udaje a prihlasovaci udaje
function checkOrderRegistration()
{
	message = "";
	formValidation = true;
	with (window.document.fRegistration) {

		//kontrola prihlasovacich udaju
		if ( isNull(userName) ) { message += "Nebylo vyplněno přihlašovací jméno.\n"; formValidation = false; }
		if ( isNull(userPass) ) { message += "Musíte vyplnit heslo.\n"; formValidation = false; }
		if ( userPass.value != userPass2.value ) { message += "Potvrzující heslo nesouhlasí se zadaným heslem. \n"; formValidation = false; }
		// kontrola jestli je dostatecne zadana fakturacni adresa
		// !!! giraffy.cz maji ve formulari registrace pri objednavce pojmenovanou fakturacni jako kontaktni (kvuli uzivatelum)
		if ( isNull(person3) ) { message += "U kontaktní adresy nebyla vyplněna kontaktní osoba.\n"; formValidation = false; }
		if ( isNull(street3) ) { message += "U kontaktní adresy nebyla vyplněna ulice.\n"; formValidation = false; }
		if ( isNull(city3) ) { message += "U kontaktní adresy nebylo vyplněno město.\n"; formValidation = false; }
		if ( isNull(zip3) ) { message += "U kontaktní adresy nebylo vyplněno PSČ.\n"; formValidation = false; }
		if ( isNull(country3) ) { message += "U kontaktní adresy nebyl vyplněn stát.\n"; formValidation = false; }
	}
	if ( formValidation == false )
	{
		alert(message);
		return false;
	}
	else
		return confirm('Zkontrolujte prosím, jestli je Váš e-mail správně zadán: ' + window.document.fRegistration.email.value + "?");
}

//kontrola, jestli jsou vyplneny prihlasovaci udaje pri registraci
function checkLoginData()
{
	message = "";
	formValidation = true;
	with (window.document.fRegistration) {
		if ( isNull(userName) ) { message += "Nebylo vyplněno přihlašovací jméno.\n"; formValidation = false; }
		if ( isNull(userPass) ) { message += "Musíte vyplnit heslo.\n"; formValidation = false; }
		if ( userPass.value != userPass2.value ) { message += "Potvrzující heslo nesouhlasí se zadaným heslem. \n"; formValidation = false; }
	}
	if ( formValidation == false )
	{
		alert(message);
		return false;
	}
	else
	    return true;
}

var preloaded = new Array();
//funkce na preload obrazku
function preload_images( imgsUrlArray ) {
    for (var i = 0; i < imgsUrlArray.length; i++){
        preloaded[i] = document.createElement('img');
        preloaded[i].setAttribute('src',imgsUrlArray[i]);
    };
};

var domain = "";
var k = 0;
var image_url = new Array();
for( var i = 1; i <= 6; i++ )
{ 
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/content_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/menuitem_01_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/menuitem_02_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/menuitem_03_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/menuitem_04_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/menuitem_05_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/menuitem_06_bg.gif"; k++;
	image_url[k] = domain + "/graphics/images/menu_0" + i + "/top_menu_bg.gif"; k++;	
}
preload_images(image_url);


//kontroly datovych typu
function objectValue(val)
{
	this.value = val;
	this.isInteger = isInteger;
	this.notNull = notNull;
}

function isInteger(value)
{
	var intregexp  = /(^-?\d\d*$)/;
	return ( !intregexp.test(value) ) ? false : true;
}

function notNull(value)
{
	return ( value == null || value == "" ) ? false : true;
}

function inRange(value, min, max)
{
	return ( value > maxvalue || value < minvalue ) ? false : true;
}

//pole hodnot formulare
function checkInputData( validation )
{
	i = 0;
	while ( i < 4 )//values.length )
	{
		/*component = document.getElementById(values[i][0]);
		val = new objectValue( component.value );*/
		alert("checked = " + validation[i][1] + ";");
		eval("checked = " + validation[i][1] + ";");
		if ( checked == false )
		{
			validation[i][0].focus();
			alert( validation[i][2] );
			return false;
		}
		i++;
	}	
	return true;		
}



//fce na otevreni noveho okna bez ovladacich listicek - w=sirka okna, h=vyska okna, x=x-ova souradnice, y-ova
function windowOpen(content,title,w, h){
	newWindow = window.open('','','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h);
	newWindow.moveTo(0 + (screen.width - w)/2,0 + (screen.height - h)/2);	
	newDoc = newWindow.document;
	newContent = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">';
	newContent += '<title>'+title+'</title><link rel="StyleSheet" href="http://www.levnepocitace.cz/new/graphics/css/styles03.css" type="text/css">';
	newContent += '</head><body id="popup">';
	newContent += '<p class="elementDescpHeader">'+title+':</p>';
	newContent += '<p class="elementDescp">'+content+'</p></body></html>';
	newDoc.write(newContent);
	newDoc.close();
}

//fce na otevreni noveho okna s danym detailem produktu, ktery je oznacen v comboboxu
function windowOpenProduct(objectId){
	component = document.getElementById(objectId);
	componentProductId = component[component.selectedIndex].value;
	productId = compProductIds[componentProductId];
	if ( productId != 0 )
	{
		categoryId = compProductCategories[componentProductId];
		newWindow = window.open('?view=showDetail&cid=' + categoryId + '&pid='+productId,'Produkt');
		//newWindow.moveTo(0 + (screen.width - w)/2 , 0 + (screen.height - h)/2);
	}
	else
	{		
		windowOpen( compProductDesc[componentProductId], 'N?pov?da ke komponent?', 400, 200);
	}
}



//fce na otevreni noveho okna s danym url a na dane pozici
function windowOpenURL(content,title,w, h){
	newWindow = window.open(content,title,'toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h);
	newWindow.moveTo(0 + (screen.width - w)/2 , 0 + (screen.height - h)/2);
}


//fce na zmenu id objektu
function switchObjectId(source_id,target_id){
	obj = document.getElementById(source_id);
	obj.id = target_id;
}

//aktualne vybrana sekce menu (css trida)
var actualClass = "";

//fce na zmenu css class objektu
function switchCSSClass(source_id,css_class){
	obj = document.getElementById(source_id);
	obj.className = css_class;
}

//fce na zmenu css class objektu a soucasne ulozeni predchozi css class do actualClass
function switchCSSClassWithHistory(source_id,css_class){
	obj = document.getElementById(source_id);
	actualClass = obj.className;
	obj.className = css_class;
}


/**************************************
? Autor: Jonas Raoni Soares Silva
? Site: http://www.joninhas.ath.cx:666
**************************************/

fmtMoney = function( n, c, d, t ) {
	var m = ( c = Math.abs( c ) + 1 ? c : 2, d = d || ",", t = t || ".", /(\d+)(?:(\.\d+)|)/.exec( n + "" ) ), x = m[1].length % 3;
	return ( x ? m[1].substr( 0, x ) + t : "" ) + m[1].substr( x ).replace( /(\d{3})(?=\d)/g, "$1" + t ) + ( c ? d + ( +m[2] ).toFixed( c ).substr( 2 ) : "" );
};



//fce na spocitani ceny konfigurace
function calculatePrice( formId, priceTagId, vatpriceTagId, vatType, vat, discount )
{
	if ( vatType == 0 )
	{
		price = defaultPrice;
		componentPrice = 0;
		form = document.getElementById(formId);		
		for ( i = 0; i < components.length; i++ )
		{
			component = document.getElementsByName(components[i]);
			switch ( component[0].type )
			{
				case "select-one" :	componentPrice = compProductPrices[ component[0][component[0].selectedIndex].value ]; break;
				case "radio" : 
				{
					for ( k = 0; k < component.length; k++ )
						if ( component[k].checked == true )
							componentPrice = compProductPrices[ component[k].value ];
				}
				break;
			}			
			price += componentPrice * ( (100 - discount) / 100);						
		}	
		vatPrice = price * ( 1 + ( vat / 100 ) );
		priceElem = document.getElementById(priceTagId);
		priceElem.value = fmtMoney(Math.round(price),0,',','.');
		vatpriceElem = document.getElementById(vatpriceTagId);
		vatpriceElem.value = fmtMoney(Math.round(vatPrice),0,',','.');		
	}
	else if ( vatType == 1 )
	{
		price = defaultPrice;
		vatprice = defaultPrice  * ( 1 + ( vat / 100 ) );;
		componentPrice = 0;
		form = document.getElementById(formId);		
		for ( i = 0; i < components.length; i++ )
		{
			component = document.getElementsByName(components[i]);
			switch ( component[0].type )
			{
				case "select-one" :
				{
					componentPrice = compProductPrices[ component[0][component[0].selectedIndex].value ];
					componentVat = compProductVats[ component[0][component[0].selectedIndex].value ];
				}
				break;
				case "radio" : 
				{
					for ( k = 0; k < component.length; k++ )
						if ( component[k].checked == true )
						{
							componentPrice = compProductPrices[ component[k].value ];
							componentVat = compProductVats[ component[k].value ];
						}
				}
				break;
			}
			price += componentPrice * ( (100 - discount) / 100);
			vatprice += componentPrice  * ( 1 + (componentVat / 100) );
		}	
		priceElem = document.getElementById(priceTagId);
		priceElem.value = fmtMoney(Math.round(price),0,',','.');
		vatpriceElem = document.getElementById(vatpriceTagId);
		vatpriceElem.value = fmtMoney(Math.round(vatPrice),0,',','.');
	}
}


//fce na nastaveni zakladni ceny konfigurace
function setDefaultPrices(priceTagId, vatpriceTagId )
{
	priceElem = document.getElementById(priceTagId);
	priceElem.value = fmtMoney(Math.round(startPrice),0,',','.');
	vatpriceElem = document.getElementById(vatpriceTagId);
	vatpriceElem.value = fmtMoney(Math.round(startVatPrice),0,',','.');	
}
