var server_path = " http://86.121.18.196:85/";

var cfg_component 	= new Array("cpu", "mb", "mem1", "mem2", "mem3", "video", "hdd1", "hdd2", "case", "source", "optic", "sound", "network", "mouse", "keyboard", "monitor", "printer", "speakers", "tvtuner", "os");
var cfg_loaded		= false;
var cfg_select 		= new Array();
var cfg_price 		= new Array();
var cfg_img 		= new Array();
var cfg_msg 		= new Array();
var cfg_img_path 	= new Array();

function configurator_onLoad()
{
	if ( cfg_loaded )
		return;
	cfg_loaded=true;
	for(i=0; i<cfg_component.length; i++)
	{
		cfg_select[ cfg_component[i] ] = document.getElementById( 'sel_'+cfg_component[i] );
		cfg_img[ cfg_component[i] ] = document.getElementById( "img_"+cfg_component[i] );
		cfg_msg[ cfg_component[i] ] = document.getElementById( "msg_"+cfg_component[i] );
		cfg_price[ cfg_component[i] ] = document.getElementById( "price_"+cfg_component[i] );
		cfg_img_path[ cfg_component[i] ] = "img/icons/icon_"+cfg_component[i]+".gif";

		if ( cfg_select[ cfg_component[i] ].options[ cfg_select[ cfg_component[i] ].selectedIndex ].value > 0 )
			cfg_img[ cfg_component[i] ].src = cfg_img_path[ cfg_component[i] ];
	}
	cfg_price['total_price'] = document.getElementById( 'total_price' );
}

function trigger_select( _select )
{
	var _component = _select.id.substring(4);
	cfg_select = new Array();
	for(i=0; i<cfg_component.length; i++)
		cfg_select[ cfg_component[i] ] = document.getElementById( 'sel_'+cfg_component[i] );

	if ( !cfg_loaded )
		configurator_onLoad();

	// Daca valoarea este mai mare ca 0 inseamna ca am selectat un produs asa ca ii afisam imaginea in dreapta
	if ( _select.options[ _select.selectedIndex ].value > 0 )
		cfg_img[ _component ].src = cfg_img_path[ _select.id.substring(4) ];
	else
		cfg_img[ _component ].src = "img/icons/icon_na.gif";

	// Trimitem parametrii catre server si vedem in functie de raspuns ce select-uri updatam si ce mesaje dam
	var ajax_path = "/configuratie.php?category="+ _select.id+"&productId="+_select.options[ _select.selectedIndex ].value;
	for(i=0; i<cfg_component.length; i++)
	{
		ajax_path += "&"+cfg_component[i]+"=";
		ajax_path += cfg_select[ cfg_component[i] ].options[ cfg_select[ cfg_component[i] ].selectedIndex ].value;
	}

	// In functie de produsul selectat cerem informatii despre produs si le afisam in partea de jos
	var xmlhttp = itcCreateXMLHttpRequestObject();
	xmlhttp.open("GET", ajax_path,true);

	var xmlhttp2 = itcCreateXMLHttpRequestObject();
	xmlhttp2.open("GET", "/descriereProdus.php?ProductID="+_select.options[ _select.selectedIndex ].value,true);

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState!=4)
			return;
		if (xmlhttp.status != 200)
		{
			alert("There was a problem retrieving the XML data:\n");
			return;
		}

		var cf_select = "";
		var cf_xml_options = "";

		// Verificam ce mesaje de informare/eroare avem si le afisam



		// Verificam care e pretul fiecarei componente si il afisam
		for(i=0; i<cfg_component.length; i++)
		{
			cf_xml_options = xmlhttp.responseXML.getElementsByTagName( cfg_component[i]+'_price' );
			if ( cf_xml_options[0].childNodes[0].nodeValue > 0 )
				cfg_price[ cfg_component[i] ].innerHTML = cf_xml_options[0].childNodes[0].nodeValue;
			else
				cfg_price[ cfg_component[i] ].innerHTML = "";

			cf_xml_options = xmlhttp.responseXML.getElementsByTagName( cfg_component[i]+'_msg' );
			if ( cf_xml_options[0].childNodes[0].nodeValue != '.' )
				cfg_msg[ cfg_component[i] ].innerHTML = cf_xml_options[0].childNodes[0].nodeValue;
			else
				cfg_msg[ cfg_component[i] ].innerHTML = "";
		}

		// Afisam pretul total
		cf_xml_options = xmlhttp.responseXML.getElementsByTagName( 'total_price' );
		cfg_price['total_price'].innerHTML = cf_xml_options[0].childNodes[0].nodeValue;

		// Verificam care e pretul sistemului si il afisam

		// Daca s-a schimat procesorul sau placa de baza luam atitudine, daca nu, nu
		if ( _select.id != 'sel_cpu' && _select.id != 'sel_mb' )
			return;

		// Daca am primit informatii despre placi de baza updatam campul
		var cfg_change_id = new Array( "mb", "mem1", "mem2", "mem3", "video", "hdd1", "hdd2" );
		for(sel=0; sel<cfg_change_id.length; sel++)
		{
			cfg_select = document.getElementById( 'sel_'+cfg_change_id[sel] );
			cf_xml_options = xmlhttp.responseXML.getElementsByTagName( cfg_change_id[sel] );

			if ( cf_xml_options.length<1)
				continue;

			try
			{
				var defaultValue = cfg_select.options[ cfg_select.selectedIndex ].value;
				cfg_select.selectedIndex = 0;
				for(i=0; i<cf_xml_options.length; i++)
				{
					var sel_value="";
					var sel_text="";
					var sel_style="";

					if ( cf_xml_options[i].childNodes.length>0)
						sel_value = cf_xml_options[i].childNodes[0].childNodes[0].nodeValue;
					if ( cf_xml_options[i].childNodes.length>1)
						sel_text = cf_xml_options[i].childNodes[1].childNodes[0].nodeValue;
					if ( cf_xml_options[i].childNodes.length>2)
						sel_style = cf_xml_options[i].childNodes[2].childNodes[0].nodeValue;

					updateSelect( cfg_select, i, sel_value, sel_text, sel_style, defaultValue);
				}
				compactSelect( cfg_select, cf_xml_options.length );
			}
			catch(err)
			{
				alert(err+">"+i+">");
			}

		}
	}

	xmlhttp2.onreadystatechange=function()
	{
		if (xmlhttp2.readyState!=4)
			return;
		if (xmlhttp2.status != 200)
		{
			alert("There was a problem retrieving the XML data:\n");
			return;
		}

		cfg_select = document.getElementById( 'windowContent1' );
		if (!cfg_select)
		{
			customFunctionCreateWindow(false, 220, 300, 0, 0);
			cfg_select = document.getElementById( 'windowContent1' );
		}
		/*if (xmlhttp2.responseText != '')
		{
			cfg_select.style.visibility = 'visible';
		}
		else
		{
			cfg_select.style.visibility = 'hidden';
		}*/
		cfg_select.innerHTML = xmlhttp2.responseText;
	}

	xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	xmlhttp.setRequestHeader( 'Accept', 'message/x-formresult' );
	xmlhttp.send(null);

	xmlhttp2.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	xmlhttp2.setRequestHeader( 'Accept', 'message/x-formresult' );
	xmlhttp2.send(null);
	return false
}

