var preloadFlag = false;
var WinTop=screen.height/2;
var WinLeft=screen.width/2;
var popup_window = '';
var light_window = '';
var image_window = '';
var confirmMsg  = 'Sunteti sigur ca doriti sa stergeti ';

function open_popup_window(url,w,h) {
	if (!w || !h) {
		w = 600;
		h = 500;
	 }
    if (!popup_window.closed && popup_window.location) {
        popup_window.focus();
		popup_window.location = url;
     }
	else {
		features = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+w+',height='+h+',top='+(WinTop-h/2)+",left="+(WinLeft-w/2);
        popup_window = window.open(url, 'popup', features);
		popup_window.opener = self;
	 }
    if (window.focus) popup_window.focus();
    return false;
 }

function open_light_window(url,w,h) {
	if (!w || !h) {
		w = 600;
		h = 500;
	 }
    if (!light_window.closed && light_window.location) {
        light_window.focus();
		light_window.location = url;
     }
	else {
		features = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+w+',height='+h+',top='+(WinTop-h/2)+",left="+(WinLeft-w/2);
        light_window = window.open(url, 'light', features);
		light_window.opener = self;
	 }
    if (window.focus) light_window.focus();
    return false;
 }

function open_image_window(url,w,h) {
	if (!w || !h) {
		w = 500;
		h = 400;
	 }
    if (!image_window.closed && image_window.location) image_window.close();
	features = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+w+',height='+h+',top='+(WinTop-h/2)+",left="+(WinLeft-w/2);
	image_window = window.open(url, 'image', features);
	image_window.opener = self;
    if (!image_window.opener) image_window.opener = self;
    if (window.focus) image_window.focus();
    return false;
 }

function getObj(name)
 {
	if (document.getElementById)
	 {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	 }
	else if (document.all)
	 {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	 }
	else if (document.layers)
	 {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	 }
 }


function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
 }

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
 }

function preloadImages() {
	if(document.images) {
	//	btn_home_h = newImage("{$assets_path}images/btn_home_h.png");
	//	btn_contact_h = newImage("{$assets_path}images/btn_contact_h.png");
		preloadFlag = true;
	}
 }

function toggle_visibility(id, show, type) {
	var e = document.getElementById(id);
	if(show == "1") e.style.display = type;
	else e.style.display = 'none';
 }

function addBookmark() {
	if (window.sidebar) window.sidebar.addPanel(title, url, ""); 
	else if(document.all) window.external.AddFavorite(url, title); 
	else if( window.opera && window.print ) alert("Sorry! Your browser doesn't support this function."); 
 } 

function ChangeinnerHTML(layerid, htmlcode) {
    var statement = '';
    if(document.all) statement += "document.all['" + layerid + "'].innerHTML = '" + htmlcode + "';";	     
    else if(document.getElementById) statement += "document.getElementById('" + layerid + "').innerHTML = '" + htmlcode + "';"; 
	eval(statement);
 }

function countChar(str, frm)
 {
	frm.chars_count.value = eval(255 - str.length);
 }

function pause(ms) {
	var now = new Date();
	var exitTime = now.getTime() + ms;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime) return;
	 }
 }

function confirmLink(theLink, theMessage) {
	/*
    if (confirmMsg == '' || typeof(window.opera) != 'undefined')
	 {
		theLink.href += '&confirmed=1';
		return true;
	 }
	*/
    var is_confirmed = confirm(confirmMsg + theMessage);
    if (is_confirmed) theLink.href += '&confirmed=1';
    return is_confirmed;
 }

// start - date functions
function ValidDate(y, m, d) {
    with (new Date(y, m, d)) {
        return (getMonth() == m && getDate() == d);
    }
 }

function ValidUTCDate(y, m, d) {
    with (new Date(Date.UTC(y, m, d))) {
        return (getUTCMonth() == m && getUTCDate() == d);
    }
 }

function DateGood(y, m, d) {
	var D;
	with (D = new Date(y, --m, d)) {
		return (getMonth() == m && getDate() == d) ? D : NaN;
	}
 }

function ReadISO8601date(Q) {
    var T;
    if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(Q)) == null) {
        return -2;
    }
    for (var j = 1; j <= 5; j += 2) {
        T[j] = +T[j];
    }
    if (!ValidDate(T[1], T[3] - 1, T[5])) {
        return -1;
    }
    return [T[1], T[3], T[5]];
 }

function DateFieldOK(D) {
    var X = ReadISO8601date(D);
    return DateGood(+X[0], +X[1], +X[2]);
 }

function roValue(number)
 {
	number += '';
	x = number.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
 }

function validateNumber(checkStr, decimal, negative, notempty)
 {
	// check warranty months
	var checkOK = "0123456789";
	if(decimal) checkOK = "." + checkOK;
	if(negative) checkOK = "-" + checkOK;

	var allValid = true;
	var allNum = "";
	for(i=0; i<checkStr.length; i++)
	 {
		ch = checkStr.charAt(i);
		for(j=0; j<checkOK.length; j++)
			if(ch == checkOK.charAt(j)) break;
		if(j == checkOK.length) 
		 {
			allValid = false;
			break;
		 }
		if(ch != ".")
		allNum += ch;
	 }

	if(checkStr == "" || !allValid) return false;
	else
	 {
		var checkNum = Number(checkStr);
		if(notempty && checkNum == 0) return false;
		else return true;
	 }
 }	

function validateEmail(email) {
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null) {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
     }
    if(splitted[2] != null) {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
       }// if
      return true;
     }
	return false;
 }

function search_form_verif()
 {
	if(document.search.query.value == "" || eval(document.search.query.value.length) < 4)
	 {
		alert("Introduceti un cuvant de cautare de minim 4 caractere!");
		document.search.query.focus();
		return false;
	 }

	//document.search.submit();
 }

function login_form_verif()
 {
	if(!validateEmail(document.login.uname.value))
	 {
		alert('Introduceti numele de utilizator (adresa email valida)!');
		document.login.uname.focus();
		return false;
	 }	
	if(document.login.passwd.value == "")
	 {
		alert('Nu ati introdus parola!');
		document.login.passwd.focus();
		return false;
	 }

	document.login.valid_submit.value = "1";
	//document.login.submit();
 }

function passgen_form_verif()
 {
	if(!validateEmail(document.passgen.uname.value))
	 {
		alert('Introduceti numele de utilizator (adresa email valida)!');
		document.passgen.uname.focus();
		return false;
	 }	

	document.passgen.valid_submit.value = "1";
	//document.passgen.submit();
 }

