Array.prototype.in_array = function(search_term) {
	var i = this.length;
	if (i > 0) {
		do {
			if (this[i] === search_term) {
				return true;
			}
		}
		while (i--);
	}
	return false;
}

Array.prototype.indexOf = function(obj){
	for(var i=0; i<this.length; i++){
		if(this[i]==obj){
			return i;
		}
	}
	return -1;
}

function image_zoom(img_big, description)
{
	var temp = new Image();
	temp.src=img_big;
	var page = '<html>'+
					'<head></head>'+
						'<body bgcolor="#ffffff" rightmargin="5" leftmargin="5" bottommargin="5" topmargin="5">'+
						'<table cellspacing="0" cellpadding="0" border="0" width="100%" height="100%">'+
						'<tr><td height="100%" align="center"><img align="middle" src="'+temp.src+'" border=0 onload="self.window.resizeTo((this.width+25),(this.height+110)); if(screen) self.window.moveTo( ((screen.width-this.width)/2), ((screen.height-this.height)/2));"></td></tr>'+
						'<tr><td align="center">'+description+'</td></tr>'+
					'</table></body>'+
				'</html>';
	var attribs = ',' + "menubar=0,location=0,toolbar=0,status=0,scrollbars=0,resizable=0, width=100,height=100";
	var wind = window.open('','_blank',attribs);
	wind.document.open();
	wind.document.write(page);
	wind.document.close();
}

function validate_submit(f, fields, names)
{
	var errors = 0;
	var errors_fields = "";
	nodes = f.getElementsByTagName("input");
	for (var i = 0; i < nodes.length; i++)
		if ((fields.in_array(nodes[i].name)) && (nodes[i].value.length == 0))
		{
			errors++;
			errors_fields += names[fields.indexOf(nodes[i].name)] + ", ";
		}
	if (errors == 0)
		f.submit();
	else
	{
		var show = "Nu ati completat campu";
		if (errors == 1)
			show += "l: " + errors_fields.substr(0, errors_fields.length - 2) + ".";
		else
			show += "rile:\n" + errors_fields.substr(0, errors_fields.length - 2) + ".";
		alert(show);
	}
}

function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function delete_address_id(id)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			if (xmlHttp.responseText == "Succes")
				document.getElementById("addr_tab_" + id).style.display = "none";
			else
				alert("A aparut o eroare in procesarea informatiei dvs.:\n\n" + xmlHttp.responseText + "\n\nVa rugam reincercati.");
		}
	}
	xmlHttp.open ("GET", "delete_addr_book.php?id=" + id, true);
	xmlHttp.send (null);
}

function show_notif(cust_id, prod_id, el_id, bh)
{
	if (document.getElementById(el_id).innerHTML.substr(0, 3) == "Nu ")
		action = 'remove';
	else
		action = 'add';
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			if (xmlHttp.responseText == "Succes")
			{
				var text = "";
				if (action == 'add')
					text = "Produsul a fost adaugat in lista de notificari.";
				else
					text = "Produsul a fost scos din lista de notificari.";
				alert(text);
				document.getElementById(el_id).innerHTML = ((action == 'add') ? "Nu a" : "A") + "rata<br />noutati";
			}
			else
				alert("A aparut o eroare in procesarea informatiei dvs.:\n\n" + xmlHttp.responseText + "\n\nVa rugam reincercati.");
		}
	}
	xmlHttp.open ("GET", "add_rem_notif.php?cust_id=" + cust_id + "&prod_id=" + prod_id + "&action=" + action, true);
	xmlHttp.send (null);
}

/*#############################################################
Name: Niceforms
Version: 0.9
Author: Lucian Slatineanu
URL: http://www.badboy.ro/

Feel free to use and modify but please provide credits.
#############################################################*/

//global variables that can be used by all the functions on this page.
var selects;
var inputs;
var radios = new Array();
var checkboxes = new Array();
var hovers = new Array();
var buttons = new Array();
var selectText = "Va rugam alegeti";

//this function runs when the page is loaded so put all your other onload stuff in here too.
function init() {
	
	//check if styles are enabled and only then start replacing elements
	//if(findPosX(document.getElementById('stylesheetTest')) == -999) {
	//	replaceSelects();
	//}
}

function replaceSelects() {
    selects = document.getElementsByTagName('select');
    for(var i=0; i < selects.length; i++)
    	if (selects[i].id.substr(0, 8) == "mySelect")	
    	{
    	var selectArea = document.createElement('div');
		var left = document.createElement('div');
		var right = document.createElement('div');
		var center = document.createElement('div');
		var button = document.createElement('a');
		var text = document.createTextNode(selectText);
		center.id = "mySelectText"+i;
		button.href="javascript:showOptions("+i+")";
		selectArea.className = "selectArea";
		left.className = "left";
		right.className = "right";
		center.className = "center";
		right.appendChild(button);
		center.appendChild(text);
		selectArea.appendChild(left);
		selectArea.appendChild(right);
		selectArea.appendChild(center);
        selects[i].style.display='none'; 
		selects[i].parentNode.insertBefore(selectArea, selects[i]);
		var optionsDiv = document.createElement('div');
		optionsDiv.className = "optionsDivInvisible";
		optionsDiv.id = "optionsDiv"+i;
		optionsDiv.style.left = findPosX(selectArea) + 'px';
		optionsDiv.style.top = findPosY(selectArea) + 35 + 'px';
		for(var j=0; j < selects[i].options.length; j++) {
			var optionHolder = document.createElement('p');
			var optionLink = document.createElement('a');
			var optionTxt = document.createTextNode(selects[i].options[j].text);
			optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+j+","+i+");";
			optionLink.appendChild(optionTxt);
			optionHolder.appendChild(optionLink);
			optionsDiv.appendChild(optionHolder);
		}
		document.getElementsByTagName("body")[0].appendChild(optionsDiv);
	}
}

function showOptions(g) {
		elem = document.getElementById("optionsDiv"+g);
		if(elem.className=="optionsDivInvisible") {elem.className = "optionsDivVisible";}
		else if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}
}

function selectMe(selectFieldId,linkNo,selectNo) {
	selectField = document.getElementById(selectFieldId);
	for(var k = 0; k < selectField.options.length; k++) {
		if(k==linkNo) {
			selectField.options[k].selected = "selected";
		}
		else {
			selectField.options[k].selected = "";
		}
	}
	textVar = document.getElementById("mySelectText"+selectNo);
	var newText = document.createTextNode(selectField.options[linkNo].text);
	textVar.replaceChild(newText, textVar.childNodes[0]);
	if (typeof(selectField.onchange) == "function")
		selectField.onchange();
}

function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {
		posTop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {
		posLeft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return posLeft;
}

window.onload = init;