var xmlHttp
ESHOP_URL = "http://www.lip-poljcane.si/spletna-trgovina/";

function addEvent(event, call) {
	if (this.addEventListener) {
		this.addEventListener (event, call, false);
		this.addEventListener (event, call, false);
	} else if (this.attachEvent) {
		this.attachEvent ("on"+event, call);
		this.attachEvent ("on"+event, call);
	}
}

//Get appropriate address details <div>s, overload their object with buildAddress method.
function checkAddresses() {
	billing_id = document.getElementById("billing_address_id").value;
	delivery_id = document.getElementById("delivery_address_id").value;
	
	billing_div = document.getElementById("billing_details");
	delivery_div = document.getElementById("delivery_details");
	
	billing_div["buildAddress"] = buildAddress;
	delivery_div["buildAddress"] = buildAddress;
	
	billing_div.buildAddress(billing_id);
	delivery_div.buildAddress(delivery_id);
}

//Generate a DOM output of the address details. innerHTML is not a part of the W3C DOM specification.
function buildAddress(aid) {
	while(this.hasChildNodes()){
		this.removeChild(this.lastChild);
	}
	
	var br = document.createElement('br');
	var name = document.createTextNode(addresses[aid]['last_name'] + ", " + addresses[aid]['first_name']);
	this.appendChild(name);
	this.appendChild(br);
	var address = document.createTextNode(addresses[aid]['address'] + ", " + addresses[aid]['post_code'] + " " + addresses[aid]['post']);
	this.appendChild(address);
	this.appendChild(br.cloneNode(false));
	if (addresses[aid]['company']) {
		var company = document.createTextNode(addresses[aid]['company']);
		this.appendChild(company);
		this.appendChild(br.cloneNode(false));
	}
	var country = document.createTextNode(addresses[aid]['country_name']);
	this.appendChild(country);
	if (!(addresses[aid]['vat_obligated'] == 0)) {
		this.appendChild(br.cloneNode(false));
		var vat_number = document.createTextNode("ID za DDV: " + addresses[aid]['vat_number']);
		this.appendChild(vat_number);
	}
}

function toggleNew() {
	var new_div = document.getElementById("new_address");
	var new_delivery_button = document.getElementById("new_delivery_button");
	var new_billing_button = document.getElementById("new_billing_button");
	if (new_div.style.display == "none") {
		new_div.style.display = "block";
		new_delivery_button.style.display = "none";
		new_billing_button.style.display = "none";
	}
	else {
		new_div.style.display = "none";
		new_delivery_button.style.display = "block";
		new_billing_button.style.display = "block";
	}
}

function addProducts(oid, pvid, q)
{
	xmlHttpAddProduct=GetXmlHttpObject()
	if (xmlHttpAddProduct==null)
	{
		alert (lang_HTTP_request_error)
		return
	}
	
	var url=CART_URL+"ajax/add.php"
	url = url+"?oid="+oid
	url = url+"&pvid="+pvid
	url = url+"&q="+q
	xmlHttpAddProduct.open("GET",url,false)
	xmlHttpAddProduct.send(null)
}

function totalProducts(oid)
{
	xmlHttpTotalProduct=GetXmlHttpObject()
	if (xmlHttpTotalProduct==null)
	{
		alert (lang_HTTP_request_error)
		return
	}
	
	var url=CART_URL+"ajax/listTotals.php"
	url = url+"?oid="+oid
	xmlHttpTotalProduct.open("GET",url,false)
	xmlHttpTotalProduct.send(null)
}

function sendOrderMail(oid)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert (lang_HTTP_request_error)
		return
	}
	
	var url=CART_URL+"ajax/mails.php"
	url = url+"?oid="+oid
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function buildPostRequest() {
	var post_data = "nlAction=execute" +
					"&first_name=" + encodeURI( document.getElementById("first_name").value ) +
					"&last_name=" + encodeURI( document.getElementById("last_name").value ) +
					"&company=" + encodeURI( document.getElementById("company").value ) +
					"&address=" + encodeURI( document.getElementById("address").value ) +
					"&post=" + encodeURI( document.getElementById("post").value ) +
					"&post_code=" + encodeURI( document.getElementById("post_code").value ) +
					"&country_id=" + encodeURI( document.getElementById("country_id").value ) //+
					//"&vat_obligated=" + encodeURI( document.getElementById("vat_obligated").checked ) +
                    //"&vat_number=" + encodeURI( document.getElementById("vat_number").value );
    return post_data;
}

function updateAddress()
{
	var data =  buildPostRequest()
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert (lang_HTTP_request_error)
		return
	}
	
	var url=CART_URL+"ajax/addressDropdowns.php"
	xmlHttp.onreadystatechange=addressStateChanged
	xmlHttp.open("POST",url,true)
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", data.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data)
}

function addressStateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttpProduct.readyState=="complete")
	{ 
		if (xmlHttp.responseText.length > 0)
		{
			//document.getElementById("address_container").innerHTML = xmlHttp.responseText
			window.location.href=CART_URL+"korak2/"
		}
	} 
}

function removeProduct(csid) {
	if(confirm("Želite odstraniti izdelek?")) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert (lang_HTTP_request_error)
			return
		} 
		
		var url=CART_URL+"ajax/remove.php"
		url=url+"?csid="+csid
		xmlHttp.onreadystatechange=refreshList
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		
		return false
	}
}

function updateProduct(csid) {
	q = document.getElementById("q_"+csid).value;
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert (lang_HTTP_request_error)
		return
	} 
	if (isNaN(q) || q < 0) {
		alert("Prosimo vnesite veljavno število!")
		return false;
	}
	var url=CART_URL+"ajax/update.php"
	url=url+"?csid="+csid
	url=url+"&q="+q
	xmlHttp.onreadystatechange=refreshList
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
    
	return false
}

function updateRolldown() {
	
    xmlHttpRolldown=GetXmlHttpObject()
	if (xmlHttpRolldown==null)
	{
		alert (lang_HTTP_request_error)
		return
	}
	var url=CART_URL+"ajax/rolldown.php"
    xmlHttpRolldown.open("GET",url,true)
    xmlHttpRolldown.onreadystatechange=refreshRolldown
    xmlHttpRolldown.send(null)
}

function refreshList() 
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		updateRolldown()
		refreshProducts()
	} 
}
function refreshProducts()
{
	var page = "logout"
	if (document.getElementById("customer_id") !== null) {
		page = "login"
	}
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert (lang_HTTP_request_error)
		return
	} 
	
	var url=CART_URL+"ajax/"+page+"Table.php"
	xmlHttp.onreadystatechange=productsStateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function productsStateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText.length > 0)
		{
			document.getElementById("product_listTable").innerHTML = xmlHttp.responseText
		}
	} 
}

function refreshRolldown() 
{ 
	if (xmlHttpRolldown.readyState==4 || xmlHttpRolldown.readyState=="complete")
	{ 
		if (xmlHttpRolldown.responseText.length > 0)
		{
			document.getElementById("cartdd").innerHTML = xmlHttpRolldown.responseText
		}
	} 
}
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 
