
function validEmail(email)
{
	if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    }
	else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    }
	else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    }
	else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
    }
	else if (email.indexOf(".") == email.length) {  // . must not be the last character
		return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}



function submit_contact(lang)
{
	if (document.f_contact.p_nume.value.length==0 || 
		document.f_contact.p_email.value.length==0)
			alert("Completati toate campurile marcate cu * !");
	else
	if (!validEmail(document.f_contact.p_email.value))
			alert("Introduceti o adresa de email valida!");
	else
		document.f_contact.submit();
}

function show(nID)
{
	document.getElementById(nID).style.display="block";
}
function hide(nID)
{
	document.getElementById(nID).style.display="none";
}



function getElement(aID)
{ 
    return (document.getElementById(aID)) ? document.getElementById(aID)
                                     : document.all[aID];
}


//ediy v2
function flash_fix()
{
n=navigator.userAgent;
w=n.indexOf("MSIE");
o=n.indexOf("Opera");
if( ( (w>0)&&(parseInt(n.charAt(w+5)) >5 ) ) || (o>=0) )
{
	T=["object","embed","applet"];
	for(j=0;j<3;j++)
	{
		E=document.getElementsByTagName(T[j]);
		for(i=0;i<E.length;i++)
		{
			P=E[i].parentNode;
			H=P.innerHTML;
			P.removeChild(E[i]);
			P.innerHTML=H;
		}
	}
}

}

/* browser */

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

	this.dom    = getElement ? 1 : 0;

	this.opera5 = (navigator.userAgent.indexOf("Opera") > -1 && getElement) ? 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 returnFrame(strID) {
    var oIframe = document.getElementById(strID);
    var oDoc = oIframe.contentWindow || oIframe.contentDocument;
    if (oDoc.document) {
        oDoc = oDoc.document;
    }
    return oDoc;
}
function getIFrameHTML(strID) {
    var objChilds   = returnFrame(strID).childNodes;
    var strHTML     = "";
    for(i = 0; i < objChilds.length; i++) {
        if((objChilds[i].nodeType == 1) && (objChilds[i].innerHTML !="")) {
            strHTML     = objChilds[i].innerHTML;
            break;
        }
    }
    return strHTML;
}

function ajaxProc(global)
{
		var randomnumber=Math.floor(Math.random()*11)
		ajObject = getAjaxObject();
		if(ajObject == false)
		{
				alert('Browser-ul dumneavoastra nu suporta Ajax');
		}
		else
		{
				ajObject.onreadystatechange = new_preview;
				if(global != 1)
				{
						
						content = escape(getIFrameHTML('p_content_rEdit'));
						rezumat = escape(getIFrameHTML('p_summary_rEdit'));
						template = escape(document.getElementById('p_template').value);
						headline = escape(document.getElementById('p_headline').value);
						id = escape(document.getElementById('p_id').value);
						params = 'p_content='+content+'&p_rezumat='+rezumat+'&p_template='+template+'&p_headline='+headline+'&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);
				}
		};
}

var headerCount = 1;

function changeHeader()
{
	element = document.getElementById('right_content');
	elementHeader = document.getElementById('header');
	if(headerCount >= 4)
	{
		headerCount = 1;
	}
	headerCount++;
	element.className = 'right_content'+headerCount;
	elementHeader.style.backgroundImage = 'url(/images/new_interface/bkg_header'+headerCount+'.jpg)';
}

function showHideJob(id, lang)
{
	element = document.getElementById('job_'+id);
	if(element.style.display == 'none')
	{
		element.style.display = 'block';
	}
	else
	{
		element.style.display = 'none';
	}
}

function position_cert()
{
	left = findPosX($('content'));
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		left += 10;
	}
	$('certificari').style.left = left + 740 + 'px';
	$('certificari').style.top = '3px';
}

function toggle_sap(id)
{
	div = document.getElementById(id);
	if(div.style.display == 'none')
	{
		div.style.display = 'block';
	}
	else
	{
		div.style.display = 'none';
	}
}

function toggle_man(id)
{
	div = document.getElementById(id);
	if(div.style.display == 'none')
	{
		div.style.display = 'block';
	}
	else
	{
		div.style.display = 'none';
	}
}

function toggle_clienti() {
	div = document.getElementById('__clienti');
	if(div.style.display == 'none') {
		div.style.display = 'block';
	} else {
		div.style.display = 'none';
	}
}

function show_headline_help() {
		document.getElementById('help').style.display = 'block';
	}
	
	function hide_headline_help() {
		document.getElementById('help').style.display = 'none';
	}
	
/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/

var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
