var host = "";

/*----------------------ADMIN PROMOTION------------------------*/
function add_product_pr(){
	alert("TEST");
	elem = document.promotion.products;
	elem_s = elem.options[elem.selectedIndex].value;
	document.promotion.products.options[elem.selectedIndex].setAttribute('disabled', '');
	//document.promotion.products.selectedIndex.setAttribute('disabled', '');
	document.promotion.products.selectedIndex = 0;
	
	var XMLHttpRequestObject = false;
	result = "";
      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new
          ActiveXObject("Microsoft.XMLHTTP");
      }

     if(XMLHttpRequestObject) {         
          XMLHttpRequestObject.open("GET", host+"ajax/getProduct.php?id_prod="+elem_s);

          XMLHttpRequestObject.onreadystatechange = function()
          {
             if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                 result = eval(XMLHttpRequestObject.responseText);   
                 //name
                 name = document.createElement("span");
                 newText = document.createTextNode(result[0]);
                 name.appendChild(newText);
                 //sum
                 sum = document.createElement("div");
                 newText = document.createTextNode(result[1]);
                 sum.appendChild(newText);
                 sum.className = "sum";
                 //remove
                 elem_rem = document.createElement("a");
                 newText = document.createTextNode("remove");
                 elem_rem.appendChild(newText);
                 
                 
                 //input
                 elem_inp = document.createElement("input");
                 elem_inp.size = 1;
                 elem_inp.setAttribute('maxlength', 2);
                 elem_inp.type = "text";
                 elem_inp.name="prod"+elem_s;
                 elem_inp.value = 1;
                 
                 //input hidden
                 elem_inp_h = document.createElement("input");
                 elem_inp_h.type = "hidden";
                 elem_inp_h.value = result[1];
                                  
                 //div
                 elem_div = document.createElement("div");
                 elem_div.className = "clear";
                 
                 if( elem_rem.addEventListener){
               		  elem_rem.addEventListener("click", remove_product, false);
               		  elem_inp.addEventListener("blur", refresh_sum, false);
                 }else if( elem_rem.attachEvent ){
               		  elem_rem.attachEvent ("click", remove_product);
               		  elem_inp.attachEvent("blur", refresh_sum);
                 }else{
                      elem_rem.onclick = remove_product;
                      elem_inp.onblur = refresh_sum;                      
                 }
                 
                 elem_li = document.createElement("li");
                 elem_li.appendChild(name);                 
                 elem_li.appendChild(elem_rem);
                 elem_li.appendChild(elem_inp);
                 elem_li.appendChild(sum);
                 elem_li.appendChild(elem_inp_h);
                 elem_li.appendChild(elem_div);
                 document.getElementById("ins_prod").appendChild(elem_li);
                 
                 get_t_sum();                 
             }
           }
           XMLHttpRequestObject.send(null);
        }	
        
}

function remove_product(evt){
	var elem = (evt.target)? evt.target : evt.srcElement;
	childs = elem.parentNode.childNodes;
	for (i=0;i<childs.length;i++)
		if((childs[i].tagName == "INPUT") && (childs[i].name.indexOf("prod") == 0)){			
			elem_id = childs[i].name.replace("prod", "");
		}
	elem_select = document.promotion.products;
	for(i=0;i<elem_select.options.length ;i++)
		if(elem_select.options[i].value == elem_id)
			document.promotion.products.options[i].removeAttribute("disabled")
	document.getElementById('ins_prod').removeChild(elem.parentNode);	
	get_t_sum();
}

function refresh_sum(evt){
	var elem = (evt.target)? evt.target : evt.srcElement;
	childs = elem.parentNode.childNodes;
	for (i=0;i<childs.length;i++)
		if((childs[i].tagName == "INPUT") && (childs[i].name.indexOf("prod") != 0)){			
			elem_sum = childs[i].value;
		}
	for(i=0;i<childs.length;i++)
		if(childs[i].className == "sum")
			childs[i].innerHTML = parseInt(elem.value) * elem_sum;
	get_t_sum();
}

function refresh_t_sum(obj){
	discount = parseFloat(document.promotion.discount.value);
	t_sum = document.getElementById("t_sum").firstChild.nodeValue;
	document.getElementById("t_prom_sum").innerHTML = t_sum - discount;
}

function get_t_sum(){
	t_sum = 0;
	elem_ul = document.getElementById("ins_prod");
	for(i=0;i<elem_ul.childNodes.length; i++)
		for(j=0; j<elem_ul.childNodes[i].childNodes.length; j++)
			if(elem_ul.childNodes[i].childNodes[j].className == "sum"){
				t_sum += parseFloat(elem_ul.childNodes[i].childNodes[j].firstChild.nodeValue);
			}
	document.getElementById("t_sum").innerHTML = t_sum;	
	discount = parseFloat(document.promotion.discount.value);
	document.getElementById("t_prom_sum").innerHTML = t_sum - discount;
}

/*----------------------ADMIN PROMOTION------------------------*/
function change_state(obj){
	id_country = document.getElementById("country").options[document.getElementById("country").selectedIndex].value;
	getGeoData(id_country, 0);
}

function getGeoData(s_country, s_county){
if(document.getElementById("country")){
		var XMLHttpRequestObject = false;
	
		if (window.XMLHttpRequest) {
		    XMLHttpRequestObject = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
		    XMLHttpRequestObject = new
		    ActiveXObject("Microsoft.XMLHTTP");
		}
	if(XMLHttpRequestObject) { 
		obj = document.getElementById("country");
		id_country = 0;
		if(s_county)
			id_country = s_country;
		//if(!s_county && !s_country)
			//id_country = document.getElementById("country").options[document.getElementById("country").selectedIndex].value;
			if(document.getElementById("country").options.length > 2)
				id_country = s_country;
			
	  if(id_country > 0)
	  	XMLHttpRequestObject.open("GET", 'ajax/getGeoData.php?id_country='+id_country);
	  else
	  	XMLHttpRequestObject.open("GET", 'ajax/getGeoData.php');
	  
	  XMLHttpRequestObject.onreadystatechange = function(){
	  	
	     if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
	       allGeo = eval(XMLHttpRequestObject.responseText);
	       if(id_country > 0)
	      	 document.getElementById("state").innerHTML = "";
	       for(i=0; i<allGeo.length; i++){
	       	s_option = document.createElement("option");
	       	s_option.setAttribute("value", allGeo[i][0]);
	       //	s_option.setAttribute("text", allGeo[i][1]);
	       	//s_option.value = allGeo[i][0];
	       	s_option.innerHTML = allGeo[i][1];
	       
	       	if(id_country > 0){
	       		if(allGeo[i][0] == s_county)
	       			//s_option.selected = "selected";
	       			s_option.setAttribute("selected", "selected");
	       		document.register.state.appendChild(s_option);       		
	       	}else{
	       		if(allGeo[i][0] == s_country)
	       			//s_option.selected = "selected";
	       			s_option.setAttribute("selected", "selected");
	       		document.register.country.appendChild(s_option);
	       	}
	       }
	       if(document.getElementById("stateLoader"))
	       	document.getElementById("stateLoader").style.display = "none";       
	       	if(s_county)
	       		clearTimeout(delayCountry);	
		}else
			 if(document.getElementById("stateLoader"))
				document.getElementById("stateLoader").style.display = "block";
				
			
	  }
	  
	   XMLHttpRequestObject.send(null);	   
	}
}
}

function actualizeaza1(){
	document.comanda.actualizeaza.value = 1;	
	document.comanda.submit();
}
function continua_cump()
{
	var ReferringUrl = "";
	if (document.referrer)
  		ReferringUrl = document.referrer.toString();
	if (window.opener && window.opener.location)
	  ReferringUrl = window.opener.location.toString();
	var pagina="http://www.etigari.ro/index.php?page=";
	if( ReferringUrl.search('prod')>0)
	{
	  pagina=pagina+"produse";
	}
	else if(ReferringUrl.search('promotion')>0)
	{
	  pagina=pagina+"promotions";
	}
	else
	{
		 pagina=pagina+"home";
	}
	  	
	window.location=pagina;
	
}
function comanda(){	
	document.comanda.submit();
}

function show_affliate(affname){
	/*elem = document.getElementById("aff_"+affname).style.display;
	if(!elem || elem=="none")
		document.getElementById("aff_"+affname).style.display = "block";
	else
		document.getElementById("aff_"+affname).style.display = "none";
	*/
}
var prePayment = 'itransfer_bancar';
function payment_meth(id){
	if(id == prePayment) return;
	$(prePayment).style.display = 'none';
	prePayment = id;
	$(prePayment).style.display = 'block';
}

function showTR(id_tr){
	$(id_tr).toggle();
}

function get_banner1(obj, code){
	img_elem = obj.value;
	temp = img_elem.split("_");
	i_width = temp[0];
	i_height = temp[1];
	

	str = '<!-- etigari.ro --><a href="http://www.etigari.ro/index.php?aff_code='+code+'" target="_blank"><img src="http://www.etigari.ro/images/affiliate/'+img_elem+'.gif" width="'+i_width+'" height="'+i_height+'" border="0" alt="Tigara Electronica" title="Tigara Electronica" /></a><!-- etigari.ro -->';
	//$('get_banner').innerHTML = str;
	
	document.banner.b_textarea.value = str;
	
	banner1=document.getElementById("afisare_ban");
	text_banner1=document.getElementById("atentionare_banner");
	
			banner1.innerHTML='<img src="http://www.etigari.ro/images/affiliate/'+img_elem+'.gif" width="'+i_width+'" height="'+i_height+'" border="0"/>';
	
	if(img_elem !="gol"){
		
		//banner1.style.border="1px solid #7f9db9";
		if(i_width < 487 && i_height < 251){
			banner1.style.width=parseInt(i_width);
			banner1.style.height=parseInt(i_height);
		//	banner1.innerHTML='<img src="http://localhost/etigari/images/affiliate/'+img_elem+'.gif" width="'+i_width+'" height="'+i_height+'" border="0"/>';
			text_banner1.innerHTML="";
		}else{
			if(i_width>486){
				banner1.style.height="";
				banner1.style.width="90%";
				n_width="100%";
			}
			if(i_height>250){
						banner1.style.height="";
				banner1.style.width="20%";
				n_width="50%";
			}
			banner1.innerHTML='<a href="http://www.etigari.ro/images/affiliate/'+img_elem+'.gif" rel="lightbox" ><img src="http://www.etigari.ro/images/affiliate/'+img_elem+'.gif" width="'+n_width+'"  /></a>';
			text_banner1.innerHTML="<p>Da click pe imagine pt a o vedea la dimensiunea reala.</p>";
		}
	}else{
		banner1.style.border="1px solid white";
		banner1.innerHTML="";
		text_banner1.innerHTML="";
	}
	
	
}

function ban_prom_prod(obj, code){
	cale = obj.value;
	t_area=document.getElementById("get_prom_prod");
	
	var selIndex = obj.selectedIndex;

	txt=obj.options[selIndex].text;
		
	if(cale !="gol"){
		t_area.value ="<a href='http://www.etigari.ro/index.php?aff_code="+code+cale+"' target='_blank'>"+txt+"</a>";
	}else{
		t_area.value="";
	}
	
}


function openImageWin(imageName)
{
	var imageWin = window.open();
	imageWin.document.open();
	imageWin.document.writeln("<img src='"+imageName+"' width=400 height=400>");
	imageWin.document.close();
}
function submit_clienti(){
	document.clienti.all_aff.value=1;
	document.clienti.submit();
}

function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
}

function addNewCommission(){
	newElem = '<div>&euro;<input type="text" name="user_comm[]" value="" maxlength="8" size="8"> : <input type="text" name="user_procent[]" value="" maxlength="5" size="5">%</div>';
	document.getElementById("user_commission").innerHTML += newElem;
}