var xmlHttp;
var spanIDglobal;
var deschis;
var clicked_image;

function HideShowTotusi()
{
	var row1 = document.getElementById("row_space");
	var row2 = document.getElementById("row_text");
	
	//alert(deschis);
	
	if (deschis != "nu")
		deschis = getVar("deschis");
	
	if ( (row1 != null) && (row2 != null) )
	{
		if ((row1.style.display == "none")||(deschis=="da"))
			{
				row1.style.display = "";
				row2.style.display = "";
				document.getElementById("imgtotusi").src = "stylepics/window_hide.gif";
				document.getElementById("de_ascuns").innerHTML = "inchide aici &raquo;";
			}
		else
			{
				row1.style.display = "none";
				row2.style.display = "none";
				document.getElementById("imgtotusi").src = "stylepics/window_unhide.gif";
				document.getElementById("de_ascuns").innerHTML = "deschide aici &raquo;";
			}
	}
	deschis = "nu";
}

function acceptStateChange() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		{
			if (xmlHttp.responseText == 'error_accept')
			{
				alert("Operatie accepta/respinge nereusita!");
				return;
			}
			else
			{
				alert (xmlHttp.responseText);
				document.location.reload(true);
			}
		}
	}
}

function accept(ID)
	{
		//alert (ID);
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		var url="accept.php";
		url=url+"?action=accept&ID="+ID;
		//alert(url);
		//return;
		xmlHttp.onreadystatechange = acceptStateChange;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null); 
	}

function reject(ID)
	{
		//alert (ID);
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		var url="accept.php";
		url=url+"?action=reject&ID="+ID;
		//alert(url);
		//return;
		xmlHttp.onreadystatechange = acceptStateChange;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null); 
	}
function register()
{
	
		if (document.getElementById("nrcard").value == "")
			{
				alert ("Camp obligatoriu: *Nr. card VIVASAN");
				return;
			}
		if (document.getElementById("name").value == "")
			{
				alert ("Camp obligatoriu: *Nume, prenume");
				return;
			}
		if (document.getElementById("address").value == "")
			{
				alert ("Camp obligatoriu: *Adresa");
				return;
			}
		if (document.getElementById("phone").value == "")
			{
				alert ("Camp obligatoriu: *Nr. telefon");
				return;
			}
		if (document.getElementById("email").value == "")
		{
			alert ("Camp obligatoriu: *E-mail");
			return;
		}
		if (document.getElementById("parola").value == "")
		{
			alert ("Camp obligatoriu: *Parola");
			return;
		}
		if (document.getElementById("confirmaparola").value == "")
		{
			alert ("Camp obligatoriu: *Confirmare parola");
			return;
		}
		if (document.getElementById("confirmaparola").value != document.getElementById("parola").value)
		{
			alert ("Parola de confirmare gresita!");
			return;
		}
	
	//let's send form
	//alert("Alo!");
	document.getElementById("formRegister").submit();
	//alert("Alo!");
}

function Affect(AffID,spanID)
	{
		//alert("("+document.getElementById(spanID).innerHTML+")");
		if (document.getElementById(spanID).innerHTML == "")
		{
			xmlHttp = GetXmlHttpObject();
			//alert(xmlHttp);
			if (xmlHttp==null)
			  {
			  alert ("Browser does not support HTTP Request");
			  return;
			  }
			
			var url="aff.php";
			url=url+"?AffID="+AffID;
			//alert(url);
			//return;
			spanIDglobal = spanID;
			xmlHttp.onreadystatechange = AffStateChanged;
			//alert("X");
			xmlHttp.open("GET",url,true);
			//alert(url);
			xmlHttp.send(null); 
		}
		else
		{
			if (document.getElementById(spanID).style.display == "none")
				document.getElementById(spanID).style.display = "block" ;
			else
				document.getElementById(spanID).style.display = "none";
		}
		
	}
function AffStateChanged() 
	{ 
	//alert(xmlHttp.status);
	//document.getElementById("txtlogin").innerHTML = "!";
	//return;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		  {
			  //alert(spanIDglobal+":"+ xmlHttp.responseText + ":");
			  if (xmlHttp.responseText == '')
				  {
					return;
				  }
			  else
			  	  {
					  document.getElementById(spanIDglobal).innerHTML  = xmlHttp.responseText;
				  }
		  }
	 }
	}
function zoom_X(imgid,w,h)
{
	//alert(imgid);
	document.getElementById(imgid).width = w;
	document.getElementById(imgid).height = h;
}

function search_X()
{
	if (document.getElementById("searchtext").value != "" )
		document.getElementById("fSearch").submit();
}

function key_pressed(e)
{
	var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	
	//alert(unicode);
	
	if (unicode == 13)
		login();
}
function hide_trSecond()
{
	if (document.getElementById("trSecond"))
	{
		document.getElementById("trSecond").style.display = "none"
	}
}
function show_harta()
{
	//alert(document.getElementById("harta").style.display);
	if (document.getElementById("harta").style.display == "none")
	{
		document.getElementById("harta").style.display = "";
	}
	else
	{
		document.getElementById("harta").style.display = "none";
	}
	
}

function order()
{
	if (document.getElementById("txtLogged").value != 1)
	{
		//alert ("Nu e logat");
		if (document.getElementById("txtName").value == "")
			{
				alert ("Camp obligatoriu: *Nume,prenume");
				return;
			}
		if (document.getElementById("txtAddress").value == "")
			{
				alert ("Camp obligatoriu: *Adresa");
				return;
			}
		if (document.getElementById("txtPhone").value == "")
			{
				alert ("Camp obligatoriu: *nr. telefon");
				return;
			}
		if (document.getElementById("txtEmail").value == "")
			{
				alert ("Camp obligatoriu: *e-mail");
				return;
			}

      //alert(document.getElementById("posesorCard").checked);
		
		if (document.getElementById("posesorCard").checked == false)
			{
				if (document.getElementById("txtCardID").value == "")
					{
					alert ("Camp obligatoriu: numar card");
					return;
					}
			}
				
		else
		{
			if (document.getElementById("txtSecondAnswer").value == "")
				{
					//alert(document.getElementById("selectSource").checked);
					if ( ( (document.getElementById("selectSource").selectedIndex == 2) && (document.getElementById("checkboxPersonID").checked == true) ) 
						|| (document.getElementById("selectSource").selectedIndex == 0) )
						{
						}
						else
						{
							alert ("Camp obligatoriu: "+document.getElementById("divSecondQuestion").innerHTML);
							return;
						}
				}
		}
	}
	//let's send form
	document.getElementById("formOrder").submit();
}
function checkAgree()
{
	var trLunch;
	var checkboxAgree;
	var indx;
	
	trLunch = document.getElementById("trLunch");
	checkboxAgree = document.getElementById("checkboxAgree");
	indx = checkboxAgree.checked;
	//alert (indx);
	
	if (indx)
		{
			trLunch.style.display = "";;
		}
	else
		{
			trLunch.style.display = "none";
		}
}
function select_first_answer()
{
	var trSecond;
	var trPersonID;
	var txt;
	var selObj;
	var txtSecondAnswer;
	var indx;
	
	trSecond = document.getElementById("trSecond");
	txt = document.getElementById("divSecondQuestion");
	trPersonID = document.getElementById("trPersonID");
	selObj = document.getElementById("selectSource");
	txtSecondAnswer = document.getElementById("txtSecondAnswer"); 
	trPersonID.style.display = "none";
	
	indx = selObj.selectedIndex;
	//alert (trSecond.style.display);
	txtSecondAnswer.innerHTML = "";
	if (indx == 0)
		{
			txt.innerHTML = "";
			txtSecondAnswer.style.visibility = "hidden";
			trSecond.style.display = "none";
		}
	else if (indx == 1)
		{
			txt.innerHTML = "*Ce cuvinte ati cautat:";
			txtSecondAnswer.style.visibility = "visible";
			trSecond.style.display = "";
		}
	else if (indx == 2)
		{
			txt.innerHTML = "*Numar card Vivasan al persoanei:";
			txtSecondAnswer.style.visibility = "visible";
			trSecond.style.display = "";
			trPersonID.style.display = "";
		}
	else if (indx == 3)
		{
			txt.innerHTML = "*Indicati expozitia:";
			txtSecondAnswer.style.visibility = "visible";
			trSecond.style.display = "";
		}
	else if (indx == 4)
		{
			txt.innerHTML = "*Indicati site-ul:";
			txtSecondAnswer.style.visibility = "visible";
			trSecond.style.display = "";
		}
	else if (indx == 5)
		{
			txt.innerHTML = "*Cum se numeste revista:";
			txtSecondAnswer.style.visibility = "visible";
			trSecond.style.display = "";
		}
	else if (indx == 6)
		{
			txt.innerHTML = "*Ce continea anuntul:";
			txtSecondAnswer.style.visibility = "visible";
			trSecond.style.display = "";
		}
}

function AddProductStateChangeNoLoad() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		{
			if (xmlHttp.responseText == 'error_cos')
			{
				alert("Operatie cos nereusita!");
				return;
			}
			else
			{
				var response = xmlHttp.responseText;
				var responseArr = response.split(":");
				document.getElementById("txtNrProduseCos").innerHTML = responseArr[0];
				document.getElementById("txtCostCos").innerHTML = responseArr[1];
				
				if (responseArr[0] != 1)
				{
					document.getElementById("txtProdusText").innerHTML = "<a href=\"index.php?action=Cos\" class=\"scris\" title=\"Deschide cos\">produse</a>";
				}
				else
				{
					document.getElementById("txtProdusText").innerHTML = "<a href=\"index.php?action=Cos\" class=\"scris\" title=\"Deschide cos\">produs</a>";
				}
				//alert("Produs adaugat!");
			}
		}
	}
}
	
function add_product_noload(ProductID,img_id)
	{
		
		xmlHttp=GetXmlHttpObject();
		//alert(document.getElementById(img_id).style.display);
		document.getElementById(img_id).style.display = "";
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		var url="cos.php";
		url=url+"?action=add&ProductID="+ProductID;
		
		xmlHttp.onreadystatechange = AddProductStateChangeNoLoad;
		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null); 
	}
	
function AddProductStateChange() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		{
			if (xmlHttp.responseText == 'error_cos')
			{
				alert("Operatie cos nereusita!");
				return;
			}
			else
			{
				document.location.reload(true);
			}
		}
	}
}

function add_product(ProductID)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		var url="cos.php";
		url=url+"?action=add&ProductID="+ProductID;
		//alert(url);
		//return;
		xmlHttp.onreadystatechange = AddProductStateChange;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null); 
	}
	
function rem_product(ProductID)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		var url="cos.php";
		url=url+"?action=rem&ProductID="+ProductID;
		//alert(url);
		//return;
		xmlHttp.onreadystatechange = AddProductStateChange;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null); 
	}
	
function change_currency()
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		var url="currency.php";
		url=url+"?currency="+document.getElementById("selectCurrency").value;
		//alert(url);
		//return;
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null); 
	}

function login()
	{
	if ( (document.getElementById("nrcard").value == "" ) || (document.getElementById("parola").value == "" ))
		return;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  } 
	var url="login.php";
	url=url+"?nrcard="+document.getElementById("nrcard").value;
	url=url+"&parola="+document.getElementById("parola").value;
	document.getElementById("nrcard").value = "";
	document.getElementById("parola").value="";
	//document.getElementById("txtlogin").innerHTML=url;
	//return;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	} 
	
function logout()
	{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  } 
	var url="login.php";
	url=url+"?logout=da"
	//document.getElementById("txtlogin").innerHTML=url;
	//return;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	//alert(url);
	xmlHttp.send(null);
	}
	
function stateChanged() 
	{ 
	//document.getElementById("txtlogin").innerHTML = "!";
	//return;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		  {
			  if (xmlHttp.responseText == 'invalid')
			  {
				alert("Date invalide!");
				return;
			  }
			  else
			  	if (xmlHttp.responseText == 'error_cos')
				{
					alert("Operatie cos nereusita!");
					return;
				}
			  	else
					document.location.reload(true);
		  }
	 }
	}

function GetXmlHttpObject()
	{
	var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 // Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	 //alert(xmlHttp);
	return xmlHttp;
	}
	
function OpenReducere()
{
	window.location = "index.php?action=Reducere";
}
function getVar(name)
         {
         get_string = document.location.search;         
         return_value = '';
         
         do { //This loop is made to catch all instances of any get variable.
            name_index = get_string.indexOf(name + '=');
            
            if(name_index != -1)
              {
              get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
              
              end_of_value = get_string.indexOf('&');
              if(end_of_value != -1)                
                value = get_string.substr(0, end_of_value);                
              else                
                value = get_string;                
                
              if(return_value == '' || value == '')
                 return_value += value;
              else
                 return_value += ', ' + value;
              }
            } while(name_index != -1)
            
         //Restores all the blank spaces.
         space = return_value.indexOf('+');
         while(space != -1)
              { 
              return_value = return_value.substr(0, space) + ' ' + 
              return_value.substr(space + 1, return_value.length);
							 
              space = return_value.indexOf('+');
              }
          
         return(return_value);        
         }

function ContactAskStateChange() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		{
			if (xmlHttp.responseText == 'ok')
			{
				alert("Mesaj Trimis!");
				return;
			}
			else
			{
				var response = xmlHttp.responseText;
				alert(response);
			}
		}
	}
}
	
function ContactAsk()
	{
		var name;
		var email;
		var question;
		var url="mail/ask.php";
		
		name = document.getElementById("Name").value;
		email = document.getElementById("Email").value;
		telefon = document.getElementById("Telefon").value;
		question = document.getElementById("Question").value;
		
		//alert(name);
		//alert(email);
		//alert(telefon);
		//alert(question);
		
		url=url+"?Name="+name+"&Email="+email+"&Telefon="+telefon+"&Question="+question;
		
		if ( (name == "") || (email == "") || (question == "") || (telefon == ""))
			{
				alert("Completarea tuturor campurilor este obligatorie.");
				return;
			}
		
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		//alert(url);
		xmlHttp.onreadystatechange = ContactAskStateChange;
		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null); 
	}
	
function RegisterNewsStateChange() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert("-" + xmlHttp.responseText + "-");
		if ( xmlHttp.status == 200 )
		{
			if (xmlHttp.responseText == 'ok')
			{
				alert("Inregistrat!");
				return;
			}
			else
			{
				var response = xmlHttp.responseText;
				alert(response);
			}
		}
	}
}
	
function RegisterNews()
	{
		var name;
		var email;
		var url="RegisterNews.php";
		
		name = document.getElementById("news_name").value
		email = document.getElementById("news_email").value
		
		url=url+"?Name="+name+"&Email="+email;
		
		if ( (name = "") || (email = ""))
			{
				alert("Completarea tuturor campurilor este obligatorie.");
			}
		
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		
		//alert(url);
		xmlHttp.onreadystatechange = RegisterNewsStateChange;
		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null); 
	}
