/* browser */

function bwcheck()
{
	this.ver    = navigator.appVersion;
	this.agent  = navigator.userAgent;

	this.dom    = document.getElementById ? 1 : 0;

	this.opera5 = (navigator.userAgent.indexOf("Opera") > -1 && document.getElementById) ? 1 : 0;

	this.ie5    = (this.ver.indexOf("MSIE 5") > -1 && this.dom && !this.opera5) ? 1 : 0;
	this.ie6    = (this.ver.indexOf("MSIE 6") > -1 && this.dom && !this.opera5) ? 1 : 0;
	this.ie4    = (document.all && !this.dom && !this.opera5) ? 1 : 0;
	this.ie     = this.ie4 || this.ie5 || this.ie6;

	this.mac    = this.agent.indexOf("Mac") > -1;

	this.ns6    = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0;
	this.ns4    = (document.layers && !this.dom) ? 1 : 0;

	return this;
}



/* submit once

<form .... onSubmit="submit_once();">

*/

var submit_count = 0;

function submit_once()
{
	if (submit_count == 0)
	{
		for (var j = 0; j < document.forms.length; j++)
		{
			for (var i = 0; i < document.forms[j].elements.length; i++)
			{
				var e = document.forms[j].elements[i];
				if ((e.type == "submit") || (e.type == "image") || (e.type == "button"))
				{
					e.style.visibility = 'hidden';
				}
			}
		}

		submit_count++;
		return true;
	}
	else
	{
		alert("Acest formular a fost deja trimis.\nVa rugam asteptati pana cand serverul proceseaza datele.\nVa multumim!");
		return false;
	}
}



/* image over

<a href="url.php" onMouseOver="swapImage('img_name', '', './images/img_src_over.gif', 1);" onMouseOut="swapImgRestore();"><img name="img_name" src="./images/img_src.gif" width="15" height="9" border="0" /></a>

*/

function swapImage()
{
	var i,
	    j = 0,
	    x,
	    a = swapImage.arguments;

	document.sr = new Array;

	for (i = 0; i < (a.length - 2); i += 3)
	{
		if ((x = findObj(a[i])) != null)
		{
			document.sr[j++] = x;
		}

		if(!x.oSrc)
		{
			x.oSrc = x.src;
		}

		x.src = a[i + 2];
	}
}

function swapImgRestore()
{
	var i,
	    x,
	    a = document.sr;

	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
	{
		x.src = x.oSrc;
	}
}

function preloadImages()
{
	var d = document;

	if (d.images)
	{
		if (!d.p)
		{
			d.p = new Array();
		}

		var i,
		    j = d.p.length,
		    a = preloadImages.arguments;

		for (i = 0; i < a.length; i++)
		{
			if (a[i].indexOf("#") != 0)
			{
				d.p[j] = new Image;
				d.p[j++].src = a[i];
			}
		}
	}
}

function findObj(n, d)
{
	var p,
	    i,
	    x;

	if (!d)
	{
		d=document;
	}

	if ((p = n.indexOf("?")) > 0 && parent.frames.length)
	{
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}

	if (!(x = d[n]) && d.all)
	{
		x = d.all[n];
	}

	for (i = 0; !x && i < d.forms.length; i++)
	{
		x = d.forms[i][n];
	}

	for (i = 0; !x && d.layers && i < d.layers.length; i++)
	{
		x = findObj(n, d.layers[i].document);
	}

	if (!x && d.getElementById)
	{
		x = d.getElementById(n);
	}

	return x;
}



/* open new window */

function open_new(win_url, win_name, w, h)
{
	var newWindow = null;

	LeftPosition = (screen.width)  ? ((screen.width  - w) / 2) : 100;
	TopPosition  = (screen.height) ? ((screen.height - h) / 2) : 100;

	settings = 'width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no,dependent=no';
	newWindow = window.open('', win_name, settings);

	newWindow.location = win_url;
	newWindow.title = win_name;
	newWindow.focus();
}



/* link over */

function mouse_out(tr, color)
{
	tr.style.backgroundColor = color;
	return;
}

function mouse_over(tr, color)
{
	tr.style.backgroundColor = color;
	return;
}



/* checkboxes */

function check_showed(check, input)
{
	if (check.checked == false)
	{
		input.disabled = true;
		input.value    = '';
	}
	else
	{
		input.disabled = false;
	}
}



/*  */

function to_number(input)
{
	count = 0;
	for (i = 0; i <= (input.value.length - 1); i++)
	{
		if (input.value.charAt(i) == '.')
		{
			count++;
		}
	}

	if (((count == 1) && (input.value.charAt(input.value.length - 1) == '.')) || (input.value.length == 0))
	{
		return false;
	}

	number = parseFloat(input.value);
	input.value = number;

	return number;
}

function getAjaxObject()
{
		var aj = false;
		try
		{
				aj = new XMLHttpRequest();
		}
		catch(err1)
		{
				try
				{
						aj = new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch(err2)
				{
						try
						{
								aj = new ActiveXObject('Microsoft.XMLHTTP');
						}
						catch(err3)
						{
								aj = false;
						}
				}
		}
		return aj;
}	

function new_preview()
{
		prv = false;
		status = document.getElementById('statusPreview');
		if(ajObject.readyState == 4)
		{
				prv = window.open('/admin/newsletter.template.preview.php?g_showPreview=1', '_blank');
				prv.focus();
				opened = true;
		}
};
						
function ajaxProc(global)
{
		var randomnumber=Math.floor(Math.random()*11)
		ajObject = getAjaxObject();
		ajObject.onreadystatechange = new_preview;
		if(global != 1)
		{
			content = escape(document.template_edit.p_content.value);
			id = escape(document.template_edit.p_id.value);
			params = 'p_content='+content+'&p_id='+id+'&p_preview=1';
			ajObject.open('POST', '/admin/newsletter.template.preview.php?p_rand'+randomnumber);
			ajObject.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			ajObject.setRequestHeader('Accept-Charset', 'utf-8');
			ajObject.setRequestHeader('Content-length', params.length);
			ajObject.setRequestHeader('Connection', 'close');
			ajObject.send(params);
		}
		else
		{
			ajObject.open('GET', '/admin/newsletter.template.preview.php?g_rand'+randomnumber+'&g_preview=1');
			ajObject.send(null);
		}
}

