function $(id) {return document.getElementById(id);}

 if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
 } else if (window.ActiveXObject) {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
 }

function ajax_properties() {
 http_request.open('GET', base + "pl/ajax_properties/left/" + margin_left +"/top/" + margin_top, true);
 http_request.onreadystatechange = function() {
 if (http_request.readyState == 4) {
	if (http_request.status == 200) {
	} else {
		alert('Trwa ładowanie...Proszę czekać.');
	}
 } 
 };
 http_request.send(null);
}

function mypopup(msg) {
	var lewy = margin_left-150;
	var gora = margin_top-75;
	config = "toolbar=no,location=no,directories=no,status=no,menubar=no,width=300,height=120";
	config += "scrollbars=no,resizable=no,top=" + gora + ",left=" + lewy;
	if (msg) {
	 pop = window.open ("","pop",config)
	 pop.document.open();
	 pop.document.write('<body style="background-color:#FFF">');
	 pop.document.write(msg);
	 pop.document.write('</body>');
	 pop.document.close();
	} 
}

function RandItem() {
 	advAJAX.get({
    	url: base + "pl/ajax_rand_item/rand/1",
		onLoading : function(obj) { 
			$('rand_item').innerHTML = "<br/>Ładuję...";
  		},
    	onSuccess : function(obj) {
    		$('rand_item').innerHTML = obj.responseText;
			opacity('rand_item',0,100, 5000);
    	},
		onError : function(obj) { 
			$('rand_item').innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
  		}
	});	
	setTimeout("RandItem();", 5000);
}

function addart(art_id) {
 changeOpac(93,'theLayer');
 http_request.open('GET', base + "pl/ajax_addbasket/art_id/" + art_id, true);
 http_request.onreadystatechange = function() {
 if (http_request.readyState == 4) {
	if (http_request.status == 200) {
		$('layer_content').innerHTML = http_request.responseText;
		showMe();
		setTimeout("hideMe();",3500);
	} else {
		alert('Trwa ładowanie...Proszę czekać.');
	}
 }
 };
 http_request.send(null);
}

function RemoveArt(art_id) {
 http_request.open('GET', base + "pl/ajax_koszyk/usun/" + art_id, true);
 http_request.onreadystatechange = function() {
 if (http_request.readyState == 4) {
	if (http_request.status == 200) {
		$('layer_content').innerHTML = http_request.responseText;
		showMe();
	} else {
		alert('Trwa ładowanie...Proszę czekać.');
	}
 }
 };
 http_request.send(null);
}

function Basket(){
 changeOpac(93,'theLayer');
 http_request.open('GET', base + "pl/ajax_koszyk", true);
 http_request.onreadystatechange = function() {
 if (http_request.readyState == 4) {
	if (http_request.status == 200) {
		$('layer_content').innerHTML = http_request.responseText;
		showMe();
	} else {
		alert('Trwa ładowanie...Proszę czekać.');
	}
 }
 };
 http_request.send(null);
}

function RecountArt() {
 advAJAX.submit(document.getElementById("form_koszyk"), {
  onSuccess : function(obj) {
    $('layer_content').innerHTML = obj.responseText;
    showMe();
  }
 });
}

function Login(){
  changeOpac(93,'theLayer');
  advAJAX.submit(document.getElementById("klient_logowanie"), {
  onLoading : function(obj) { 
	$('layer_content').innerHTML = "<center><div style=\"margin-top:10px;\"></div><br/>Loading...</center>";
  },
  onSuccess : function(obj) {
	if(obj.responseText == 1) {
		window.location= http_self;
	} else {
    	$('layer_content').innerHTML = obj.responseText;
		showMe();
	}
  },
  onError : function(obj) { 
	$('layer_content').innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
  }
 });
}
function CheckLogin() {
	document.logowanie.login.value = 1;
	Login();
	document.logowanie.login.value = 0;
}
function SendMessage(){
  changeOpac(93,'theLayer');
  showMe();
  advAJAX.submit(document.getElementById("kontakt"), {
  onLoading : function(obj) { 
	$('layer_content').innerHTML = "<center><div style=\"margin-top:10px;\"></div><br/>Loading...</center>";
  },
  onSuccess : function(obj) {
    $('layer_content').innerHTML = obj.responseText;
  },
  onError : function(obj) { 
	$('layer_content').innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
  }
  });
 setTimeout("hideMe();",4000);
}

function SubscribeNews(email) {
 advAJAX.get({
	url: base + "pl/ajax_subscribe_email/email/" + email,
	onLoading : function(obj) { 
	 $('subscribe_content').innerHTML = "Loading...";
    },
    onSuccess : function(obj) {
     $('subscribe_content').innerHTML = obj.responseText;
    },
    onError : function(obj) { 
	 $('subscribe_content').innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
    }
  });
}

function RecallPassword(email) {
 advAJAX.get({
	url: base + "pl/ajax_recall_password/email/" + email,
	onLoading : function(obj) { 
	 $('recall_content').innerHTML = "Loading...";
    },
    onSuccess : function(obj) {
     $('recall_content').innerHTML = obj.responseText;
    },
    onError : function(obj) { 
	 $('recall_content').innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
    }
  });
}


function doSomething(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop+ document.documentElement.scrollTop;
	}
	showStatusTable((posy-60));
}

function showStatusTable(posy) {
	showElement('status_table');
	$('status_table').style.marginTop = posy+'px';
	$('status_table').style.marginRight = '20px;';
	$('order').value = $('order_id').value;
}

function changeOrderStatus(status_id) {
	hideElement('status_table');
	var order_id = $('order_id').value; 
	advAJAX.get({
		url: base + "admin/ajax_change_status/order_id/" + order_id + "/status_id/" + status_id,
		onLoading : function(obj) { 
			$('amount_content_' + order_id).innerHTML = "Proszę czekać...";
			$('date_content_' + order_id).innerHTML = "Proszę czekać...";
			$('send_content_' + order_id).innerHTML = "Proszę czekać...";
			$('status_content_' + order_id).innerHTML = "Proszę czekać...";
		},
		onSuccess : function(obj) {
			var result = ToArray.xml2array(obj.responseXML);
			result = result['order_info'];
			$('amount_content_' + order_id).innerHTML = "<span style='color:"+ result['status_color'] + "'>" + result['amount'] + " zł</span>";
			$('date_content_' + order_id).innerHTML = "<span style='color:"+ result['status_color'] + "'>" + result['date'] + "</span>";;
			if(!result['send_date'])
				result['send_date'] = '';
			$('send_content_' + order_id).innerHTML = "<span style='color:"+ result['status_color'] + "'>" + result['send_date'] + "</span>";
			$('status_content_' + order_id).innerHTML = "<span style='color:"+ result['status_color'] + "' onclick=\"$('order_id').value=\'"+order_id +"\'>" + result['status_desc'] + "</span>";
			$('transport_content_' + order_id).innerHTML = "<span style='color:"+ result['status_color'] + "'>" + result['transport'] + "</span>";
			$('weight_content_' + order_id).innerHTML = "<span style='color:"+ result['status_color'] + "'>" + result['weight'] + "</span>";
			$('others_content_' + order_id).innerHTML = "<a href=" + base + "admin/klienci_lista/klient_id/" + result['others'] + " style='color:"+ result['status_color'] + "'>zobacz</a>";
		},
		onError : function(obj) { 
			$('status_content_' + order_id).innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
		}
  	});
}

function showElement(element_id) {
	if($(element_id)) {
		$(element_id).style.display = 'block'; 
	}
}
function hideElement(element_id) {
	if($(element_id)) {
		$(element_id).style.display = 'none'; 
	}
}
/*
function doSomething(e) {
	if(!e) var e = window.event;
	alert(e.type);
}*/

 
var ToArray = {
	version: '1.0',
	
	str2array: function (string, separator) {/*...*/},
	
	isOnlyWhiteString: function (value) {/*...*/},
	
	xml2array: function (xml, byId) {
		if (xml.hasChildNodes() == true) {
		var nrChildren = xml.childNodes.length;
		var result = new Array();
		for(var i=0; i<nrChildren; i++) {
			var node = xml.childNodes[i];
			if(node.nodeName != '#text') {
			var key = node.nodeName;
			if(key!='xml') { // in view of IE
				if (byId == true && node.getAttribute('id')) {
				key = node.getAttribute('id');
				}
				result[key] = this.xml2array(node, byId);
			}
			} else {
			if (nrChildren > 1) {
				if(this.isOnlyWhiteString(node.nodeValue) == false) {
				result.push(node.nodeValue);
				}
			} else {
				var result = node.nodeValue;
			}
			}
		}
		}
		return result;
	}
}


function showInvoiceForm(e) {
	var posx = 0;
	var posy = 0;
	//$('order').value = $('order_id').value;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop+ document.documentElement.scrollTop;
	}
	showElement('invoiceForm');
	$('invoiceForm').style.marginTop = (posy-20)+'px';
	$('invoiceForm').style.marginLeft = (posx-100) + 'px';
	advAJAX.get({
		url: base + "admin/invoiceForm/id/" + $('order_id').value,
		onLoading : function(obj) { 
			//$('invoiceForm').innerHTML = "Prosz czekać...";
		},
		onSuccess : function(obj) {
			var result = ToArray.xml2array(obj.responseXML);
			result = result['invoiceForm'];
			$('invoice_number').value = result['invoice_number'];
			$('invoice_time').value = result['invoice_time'];
			$('sale_date').value = result['sale_date'];
			$('create_date').value = result['create_date'];
			$('orderID').value = result['orderID'];
		},
		onError : function(obj) { 
			$('invoiceForm').innerHTML = "Wystąpiły problemy.<br/>Spróbuj później.";
		}
  	});
}


function createArtMailingGroup(artId) {
	//alert(artId);
	advAJAX.get({
		url: base + "admin/mailing_grupy/createArtGroup/" + artId,
		onLoading : function(obj) { 
			$('artMailingGroup').innerHTML = "Prosz czekać...";
		},
		onSuccess : function(obj) {
			/*var result = ToArray.xml2array(obj.responseXML);
			result = result['invoiceForm'];
			$('invoice_number').value = result['invoice_number'];
			$('invoice_time').value = result['invoice_time'];
			$('sale_date').value = result['sale_date'];
			$('create_date').value = result['create_date'];
			$('orderID').value = result['orderID'];*/
			$('artMailingGroup').innerHTML = obj.responseText;
		},
		onError : function(obj) { 
			$('artMailingGroup').innerHTML = "Wystapiły problemy.<br/>Spróbuj później.";
		}
	});
}


/*
* Zglaszanie bledow poprzez SOAP dla CRM
*/
function reportProblem() {
	advAJAX.submit(document.getElementById("error-form"), {
		onLoading : function(obj) { 
			$('content-form-msg').innerHTML = "Trwa wysylanie...";
		},
		onSuccess : function(obj) {
			$('error-subject').value = '';
			$('error-description').value = '';
			$('content-form-msg').innerHTML = obj.responseText;
		}
	}
	);
}
