var main_url = "http://aplicatii.contzilla.ro/";

function openPop(url, width, height, scroll) {
	var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	var x = parseInt(screen.width / 2.0) - (width / 2.0);
	var y = parseInt(screen.height / 2.0) - (height / 2.0);
	scroll = "yes";
	if (isMSIE) {
		// Pesky MSIE + XP SP2
		width += 35;
		height += 55;
	}

	var win = window.open(url, "ImageManager", "top=" + y + ",left=" + x + ",scrollbars="+ scroll +",modal=yes,width=" + width + ",height=" + height + ",resizable=no");
	win.focus();
}

function confirmSetLocation(message, url){
    if( confirm(message) ) {
         window.location.href = url;
    }
    return false;
}

function goTo(url){
    window.location.href = url;
    return false;
}


function gupa(param){  
         var param = param.replace(/[[]/,"[").replace(/[]]/,"]");   
         var regexS = "[?&]"+param+"[?&]";    
         var regex = new RegExp(regexS);    
         var results = regex.exec(window.location.href);
         if(results) {   
             return results;  
         }
}


$(document).ready(function () {
	
	//over tr table
	if($("#adminsGrid_table tr")){
		$("#adminsGrid_table tr").mouseover(function(){
			$(this).addClass("tr_over");
		}).mouseout(function(){
			$(this).removeClass("tr_over");
		});
	}
	
	
	if($("#button_up")){
		$("#button_up").click(function(){
			//centering with css
			centerPopup();
			loadPopup();
		});		
	}
	if(gupa("edit")){
		centerPopup();
		loadPopup();
	}
	if(gupa("edit-produs")){
		centerPopup();
		loadPopup();
	}
	if(gupa("edit-clienti")){
		centerPopup();
		loadPopup();
	}
	if(gupa("add")){
		centerPopup();
		loadPopup();
	}
	if(gupa("edit-cf")){
		centerPopup();
		loadPopup();
	}
	
	
	if($("#add_line")){
		$("#add_line").click(function(){
			var html = "";
			var elm = document.createElement("tr");
			var var_list;
			var numRows = $("#nr").val();
			numRows++;
			
			$.post("../include/list_prod.php",{ ch:$("#ch").val() } ,function(data)
			{
				var_list=data;
				
				html = '<tr class="table_list" id="line_'+numRows+'">';
					html += '<td class="td_border td_border_bottom center"><a href="#" onclick="delRow(\'line_'+numRows+'\')">x</a></td>';
					html += '<td class="td_border td_border_bottom">';
						html += '<select name="produs['+numRows+']" id="produs_'+numRows+'" style="width:220px;" onchange="prod_select(\'produs_'+numRows+'\', '+numRows+')">';
						html += '<option value="">Selectati un produs</option>';
						html += var_list;
					  html += '</select>';
					html += '</td>';
					html += '<td class="td_border td_border_bottom"><input type="text" name="cantitate['+numRows+']" id="cantitate_'+numRows+'" style="width:60px;text-align:right;" value="1"></td>';
					html += '<td class="td_border td_border_bottom"><input type="text" name="um['+numRows+']" id="um_'+numRows+'" style="width:33px;text-align:right;" value=""></td>';
					html += '<td class="td_border td_border_bottom"><input type="text" name="tva['+numRows+']" id="tva_'+numRows+'" style="width:50px;text-align:right;" value="19"></td>';
					html += '<td class="td_border td_border_bottom"><input type="text" name="pret_unitar['+numRows+']" id="pret_unitar_'+numRows+'" style="width:140px;text-align:right;"></td>';
					html += '<td class="td_border td_border_bottom">';
						html += '<select name="moneda['+numRows+']" id="moneda_'+numRows+'" style="width:90px;" onchange="moneda_select(\'moneda_'+numRows+'\', '+numRows+')">';
							html += '<option value="1">LEI</option>';
							html += '<option value="2">EUR</option>';
							html += '<option value="3">USD</option>';
							html += '<option value="4">CHF</option>';
							html += '<option value="5">GBP</option>';
					  html += '</select>';
					html += '</td>';
					html += '<td class="td_border td_border_bottom"><input type="text" name="rata_ech['+numRows+']" id="rata_ech_'+numRows+'" style="width:75px;text-align:right;" value="1.0000" disabled="disabled"></td>';
				  html += '</tr>';
				
				$('#tab_prod').append(html);
				$("#nr").val(numRows);
			});
					
		});	
	}
	
	if($("#popupSeriiClose")){
		$("#popupSeriiClose").click(function(){
			if(disablePopup()){disablePopup();}
		});
	}
	//Click out event!
	if($("#backgroundPopup")){
		$("#backgroundPopup").click(function(){
			if(disablePopup()){disablePopup();}
		});
	}
	
});


function delRow(id) {
	var elm = document.getElementById(id);
	var numRows = $("#nr").val();
	if (elm) {
		elm.parentNode.removeChild(elm);
		numRows--;
		//$("#nr").val(numRows);
	}
}

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupSerii").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupSerii").fadeOut("slow");
		popupStatus = 0;
		if(gupa("edit")){
			window.location.href = main_url+'contul-meu/?administrare-cont&personal';
		}
		if(gupa("edit-produs")){
			window.location.href = main_url+'contul-meu/?produse';
		}
		if(gupa("edit-clienti")){
			window.location.href = main_url+'contul-meu/?clienti';
		}
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSerii").height();
	var popupWidth = $("#popupSerii").width();
	//centering
	$("#popupSerii").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}


function prod_select(id_selector, line_id){
	$.post("../include/prod_select.php",{ id:$("select#"+id_selector).val(),line_id:line_id } ,function(data)
	{
	$("#line_"+line_id).html(data);
	});
	return false;
}

function moneda_select(id_selector, line_id){
	$.post("../include/moneda_select.php",{ id:$("select#"+id_selector).val(),line_id:line_id } ,function(data)
	{
		$("#rata_ech_"+line_id).val(data);
		if($("select#"+id_selector).val()==1){
			$("#rata_ech_"+line_id).attr("disabled","disabled");
		}else{
			$("#rata_ech_"+line_id).attr("disabled","");
		}
	});
	return false;
}

function getE(name)
{
	if (document.getElementById)
		var elem = document.getElementById(name);
	else if (document.all)
		var elem = document.all[name];
	else if (document.layers)
		var elem = document.layers[name];
	return elem;
}


function display_ch(){
	var elem = getE('incas_ch');
	var current = (elem.style.display == 'none') ? 'block' : 'none';
	elem.style.display = current;
}

function display_repet(){
	var elem = getE('repet_table');
	var current = (elem.style.display == 'none') ? 'block' : 'none';
	elem.style.display = current;
}
