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");
		}
	}
	return xmlHttp;
}

function expandStatuses(id, status, url){
     
	var out = "<select name='status' onchange=\"sendStatus('"+id+"', '"+url+"')\">";
	out+="<option value='' SELECTED DISABLED>Избери...</option>";
	out+="<option value='Y'>Да</option>";
	out+="<option value='N'>Не</option></select>";
	document.getElementById('expand_'+id).innerHTML = out;
}

function expandActive(id, status, url){
     
	var out = "<select name='status' onchange=\"sendActive('"+id+"', '"+url+"')\">";
	out+="<option value='' SELECTED DISABLED>Избери...</option>";
	out+="<option value='1'>Да</option>";
	out+="<option value='0'>Не</option></select>";
	document.getElementById('expand_'+id).innerHTML = out;
}

function sendActive(id, url){

	var xmlHttp = GetXmlHttpObject();
    var params = "id=" + id + "&status=" + document.forms["site_form"]["status"].value;
    xmlHttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			var status = (response == 'Да') ? '0' : '1';
			document.getElementById("expand_"+id).innerHTML = "<a href='javascript:;' onclick=\"expandActive('"+id+"', '"+status+"', '"+url+"');\">"+response+"</a>";
        }
	}
	xmlHttp.send(params);
}


function sendStatus(id, url){

	var xmlHttp = GetXmlHttpObject();
    var params = "id=" + id + "&status=" + document.forms["site_form"]["status"].value;
    xmlHttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			var status = (response == 'Да') ? 'N' : 'Y';
			document.getElementById("expand_"+id).innerHTML = "<a href='javascript:;' onclick=\"expandStatuses('"+id+"', '"+status+"', '"+url+"');\">"+response+"</a>";
        }
	}
	xmlHttp.send(params);
}

function updateOrders(idmenu){

	var xmlHttp = GetXmlHttpObject();
    var params = "idmenu=" + idmenu;
    xmlHttp.open("POST", site_url +  'dealer/updateOrders', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById("updateOrders").innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

function getPoll(id, url){

	var xmlHttp = GetXmlHttpObject();
    var params = "idpoll=" + id;
    xmlHttp.open("POST", site_url +  'index/getpoll/', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById('poll_panel').innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

function votePoll(idpoll, idoption, url){

	var xmlHttp = GetXmlHttpObject();
    var params = "idpoll=" + idpoll + "&option=" + idoption + "&submit_poll=true";
    xmlHttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			getPoll(idpoll, url);
        }
	}
	xmlHttp.send(params);
}

// Гласуване

function vote() {
	var val = 0;
	
	for( i = 0; i < document.poll_form.option.length; i++ ) {
		if( document.poll_form.option[i].checked == true ) {
			val = document.poll_form.option[i].value;
		}
	}
	
	votePoll($('idpoll').value, val, site_url+'index/getpoll/');
}

// Изброяване на градовете

function listTowns(id){

	var xmlHttp = GetXmlHttpObject();
    var params = "id=" + id;
    xmlHttp.open("POST", site_url +  'help/listTowns', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById("listTowns").innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

// Активни промоции за клиенти

function getMemberPromotion(id){

	var xmlHttp = GetXmlHttpObject();
    var params = "id=" + id;
    xmlHttp.open("POST", site_url +  'member/getPromotion', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById("member_promotions").innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

// Активни промоции за търговци

function getDealerPromotion(id){

	var xmlHttp = GetXmlHttpObject();
    var params = "id=" + id;
    xmlHttp.open("POST", site_url +  'dealer/getPromotion', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById("dealer_promotions").innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

function chooseAddress(str){
	
	var info = str.split(",");
	idposition = info[0];
	idaddress = info[1];

	var xmlHttp = GetXmlHttpObject();
    var params = "idposition=" + idposition + "&idaddress=" + idaddress;
    xmlHttp.open("POST", site_url + 'member/chooseAddress', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			if(document.getElementById("chooseAddress")) {
				document.getElementById("chooseAddress").innerHTML = '';
			}
        }
	}
	xmlHttp.send(params);
}

function activateOrder(id){

	var xmlHttp = GetXmlHttpObject();
    var params = "id=" + id;
    xmlHttp.open("POST", '/dealer/activateOrder', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById("orderInfo_"+id).style.display = 'none';
        }
	}
	xmlHttp.send(params);
}

function getOrderInfo(id){
	
	var url = site_url +  'dealer/getOrders?idorder=' + id;
	
	new Ajax.Request(url, 
		{ 	method:'get',
    		onSuccess: function(transport){
      			var response = transport.responseText;
      			box(850,500, 'Данни за поръчка', response);
    		},
    		onFailure: function(){ alert('{/literal}{$lang.error_with_connection}{literal}') }
  		}
	);
}

function getNews(id, url){

	var xmlHttp = GetXmlHttpObject();
    var params = "idnews=" + id;
    xmlHttp.open("POST", site_url +  'index/getnews/', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById('news_panel').innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

function deleteOrder(id){

	var xmlHttp = GetXmlHttpObject();
    var params = "idorder=" + id;
    xmlHttp.open("POST", site_url +  'dealer/deleteOrder', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			//var response = new String();
			$('orderInfo_'+id).style.display = 'none';
        }
	}
	xmlHttp.send(params);
}

function registerWakeup(idmember, hour, minutes, when) {
	
	var xmlHttp = GetXmlHttpObject();
    var params = "idmember=" + idmember + '&hour=' + hour + '&minutes=' + minutes + '&when=' + when;
    xmlHttp.open("POST", site_url +  'help/wakeup', true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			alert('Събуждането беше успешно записано в системата ни! Благодарим, че използвахте нашите услуги!');
        }
	}
	xmlHttp.send(params);
}
