function myXMLHttpRequest() {
  var xmlHttp;
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlHttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
return (xmlHttp);	
}

function addToCart(item) {
		var quantity = document.getElementById ('quantity_' + item).value;
        var url = "addToCart.php?produs=" + item + "&cantitate=" + quantity;
        var updateDisplay = document.getElementById ('update_display_' + item);

        var xmlHttp = myXMLHttpRequest();

        if (xmlHttp) {
                xmlHttp.onreadystatechange = function() {
						if (updateDisplay != null && typeof updateDisplay != "undefined")
							updateDisplay.innerHTML = xmlHttp.responseText;
                }
                xmlHttp.open("GET", url ,true);
                xmlHttp.send(null);
        }
}

function adjustCartAmount(item,increment,reloadPage,quant) {
		if (quant + increment <= 0) {
			if (!confirm("Sunteti sigur ca vreti sa scoateti din cos acest element ?"))
				return;
		}
        var url = "adjustCartAmount.php?produs=" + item + "&increment=" + increment;

        var xmlHttp = myXMLHttpRequest();

        if (xmlHttp) {
                xmlHttp.onreadystatechange = function() {
						if (xmlHttp.responseText == "OK") {
							window.location = window.location;
						}
                }
                xmlHttp.open("GET", url ,true);
                xmlHttp.send(null);
        }
}

function removeFromCart(item,confirmare,reloadPage) {
		if (confirmare) {
			if (item == 'all') {
				if (!confirm("Sunteti sigur ca vreti sa goliti cosul ?"))
					return;
			} else {
				if (!confirm("Sunteti sigur ca vreti sa scoateti din cos acest element ?"))
					return;
			}
		}
        var url = "removeFromCart.php?produs=" + item;
        var updateDisplay = document.getElementById ('update_display_' + item);

        var xmlHttp = myXMLHttpRequest();

        if (xmlHttp) {
                xmlHttp.onreadystatechange = function() {
					if (xmlHttp.responseText == "OK") {
						if (updateDisplay != null && typeof updateDisplay != "undefined")
							updateDisplay.innerHTML = "&nbsp;";
						if (reloadPage) 
							window.location = window.location;
					}
                }
                xmlHttp.open("GET", url ,true);
                xmlHttp.send(null);
        }
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if ((str.indexOf(at) == -1) ||
		(str.indexOf(at)==-1 || str.indexOf(at)===0 || str.indexOf(at)==lstr) ||
		(str.indexOf(dot)==-1 || str.indexOf(dot)===0 || str.indexOf(dot)==lstr) ||
		(str.indexOf(at,(lat+1)) != -1) ||
		(str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) ||
		(str.indexOf(dot,(lat+2)) == -1) ||
		(str.indexOf(" ") != -1)) {
		return false;
	}

	return true;			
}

function validateForm() {
	var elem = document.getElementById('adresa');
	if (elem === null || elem.value.length < 3) {
		alert("Nu ati introdus adresa");
		return false;
	}
	elem = document.getElementById('telefon');
	if (elem === null || elem.value.length < 10) {
		alert("Nu ati introdus un numar de telefon");
		return false;
	}
	elem = document.getElementById('email');
	if (elem === null || elem.value.length === 0) {
		alert("Nu ati introdus o adresa de email");
		return false;
	} else if (!echeck(elem.value)) {
		alert("Adresa de email este invalida");
		return false;
	}
	return true;
}
