/*
    CMS Backend - Interfata de administrare a CMS-urilor dezvoltate de Felix Manea
    Copyright (C) 2007-2008 Felix Manea (felix.manea@gmail.com)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
function preloadImages(imgArray){
	var images = new Array();
	for(i=0;i<imgArray.length;i++)
	{
		t = new Image();
		t.src = '<?=base_url?>'+imgArray[i];
		images.push(t);
	}
}

function getElById(elName)
{
	el = document.getElementById(elName);
	if(typeof(el) == 'undefined' || el == null) return false;
	return el;
}

function returnObjById( id )
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

function getElementHeight(Elem) {
	if (elem && elem.clip && elem.clip.height) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else
	{
		if(document.getElementById)
		{
			var elem = document.getElementById(Elem);
		}
		else if (document.all)
		{
			var elem = document.all[Elem];
		}
		if (elem)
		{
			if(elem.style && elem.style.pixelHeight)
			{
				yPos = elem.style.pixelHeight;
			}
			else
			{
				yPos = elem.offsetHeight;
			}
		}
		else return false;
		return yPos;
	}
}

function getElementWidth(Elem) {
	if (elem && elem.clip && elem.clip.width) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else
	{
		if(document.getElementById)
		{
			var elem = document.getElementById(Elem);
		}
		else if (document.all)
		{
			var elem = document.all[Elem];
		}
		if (elem)
		{
			if(elem.style && elem.style.pixelWidth)
			{
				xPos = elem.style.pixelWidth;
			}
			else
			{
				xPos = elem.offsetWidth;
			}
		}
		else return false;
		return xPos;
	}
}

function bigPicture(image, base_url)
{
	if(typeof(image) == 'undefined')
	{
		img = document.getElementById('imagine_produs');
		if(typeof(img) == 'undefined') return;
		image = img.src;
	}
	if(typeof(base_url) == 'undefined') base_url = '';
	window.open(base_url + 'imgpreview.php?src='+escape(image),'_bigPicture','top=0, left=0, scrollbars=1, status=0, width=100, height=100, resizable=0');
}

function getScreenResolution()
{
	var screenW = 640, screenH = 480;
	if (parseInt(navigator.appVersion)>3) {
	 screenW = screen.width;
	 screenH = screen.height;
	}
	else if (navigator.appName == "Netscape"
			&& parseInt(navigator.appVersion)==3
			&& navigator.javaEnabled()
		 )
	{
	 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
	 var jScreenSize = jToolkit.getScreenSize();
	 screenW = jScreenSize.width;
	 screenH = jScreenSize.height;
	}
	return new Array(screenW, screenH);
}


function resizeWindow(w,h) {
 //trebuie recalculat in fct de marime sa faca sau nu bara orizontala
 w+=16;
 h+=3;

 resolution = getScreenResolution();
 if(w > resolution[0]) w = resolution[0];
 if(h > resolution[1]) h = resolution[1] - 50;

 if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
		top.outerWidth=w;
		top.outerHeight=h;
	 }
	 else top.resizeTo(w,h);
 }
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {width: myWidth, height: myHeight}
}

function resizeElHeightByWindowSize(element, difference){
	w = getWindowSize();
	height = (w.height - difference);
	//if(getElementHeight(element) <= height)
	document.getElementById(element).style.height = height+'px';
}

function resizeAll(){
	resizeElHeightByWindowSize('pageContent', 286);
	//alert(getElementHeight('pageContent'));
	//document.getElementById('pagePlanta').style.height = (getElementHeight('pageContent')-304)+'px';
}

function getImageFromSrc(src){
	var regexp = /^(.*\/)?([^\/]+)$/;
	if(!regexp.exec(src)) return alert("Nume fisier trimis invalid: "+src);
	return RegExp.$2;
}

// special function

var marimeFontDefault = 10;
var marimeFontMinim = 8;
var marimeFontMaxim = 18;

var marimeFontDefault1 = 0;
var marimeFontMinim1 = -2;
var marimeFontMaxim1 = 8;

xx=getCookie('marimeFontDefault1');
if(xx!=null) marimeFontDefault1=parseInt(xx);

//var stiluri=new Array("p","h1", "h2", "h3", "h4", "h5", "h6", "Heading7", "Heading8", "Heading9", "Heading10", "a");
var stiluri=new Array("p", "h2", "h3", "h4", "h5", "h6", "Heading7", "Heading8", "Heading9", "Heading10", "a", "ul", "li","td", "th");
var siz_stiluri=new Array(12,16,12,12,11,11,11,12,16,12,12,12,12,12,12);

function init_stiles_sizes(){
	var theDiv=document.getElementById('body_content');
	for(i=0; i<stiluri.length; i++){
		var p = theDiv.getElementsByTagName(stiluri[i]);
		s=10;
		if(p.length>0){
			if(p[0].style.fontSize) {
				s = parseInt(p[0].style.fontSize.replace("px",""));
			}
		}
		siz_stiluri[i]=s;
	}
}
		
function maresteFont1()
{
	theDiv=document.getElementById('body_content');
	marimeFontDefault = marimeFontDefault+1;
	if (marimeFontDefault > marimeFontMaxim) {marimeFontDefault = marimeFontMaxim;}
	theDiv.style.fontSize=marimeFontDefault+"px";
}

function micsoreazaFont1()
{
	theDiv=document.getElementById('body_content');
	marimeFontDefault = marimeFontDefault-1;
	if (marimeFontDefault < marimeFontMinim) {marimeFontDefault = marimeFontMinim;}
	theDiv.style.fontSize=marimeFontDefault+"px";
}

function increaseFont()
{
	theDiv=document.getElementById('body_content');
	marimeFontDefault1 = marimeFontDefault1+1;
	if (marimeFontDefault1 > marimeFontMaxim1) {marimeFontDefault1 = marimeFontMaxim1;}
	for(i=0; i<stiluri.length; i++){
		p = theDiv.getElementsByTagName(stiluri[i]);
		for(j=0;j<p.length;j++) {
    	p[j].style.fontSize=(siz_stiluri[i]+marimeFontDefault1)+"px";
		}
	}
	setCookie('marimeFontDefault1',marimeFontDefault1);
}

function decreaseFont()
{
	theDiv=document.getElementById('body_content');
	marimeFontDefault1 = marimeFontDefault1-1;
	if (marimeFontDefault1 < marimeFontMinim1) {marimeFontDefault1 = marimeFontMinim1;}
	for(i=0; i<stiluri.length; i++){
		p = theDiv.getElementsByTagName(stiluri[i]);
		for(j=0;j<p.length;j++) {
    	p[j].style.fontSize=(siz_stiluri[i]+marimeFontDefault1)+"px";
		}
	}
	setCookie('marimeFontDefault1',marimeFontDefault1);
}

function AplicaFont()
{
	theDiv=document.getElementById('body_content');
	for(i=0; i<stiluri.length; i++){
		p = theDiv.getElementsByTagName(stiluri[i]);
		for(j=0;j<p.length;j++) {
    	p[j].style.fontSize=(parseInt(siz_stiluri[i])+marimeFontDefault1)+"px";
		}
	}
}

function increaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}

var gAutoPrint = true;
function printFriendly()
{
	if (document.getElementById!= null)
	{
		var html = '<HTML>\n<HEAD>\n';
		if (document.getElementsByTagName!= null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}
		html += '\n</HE' + 'AD>\n<BODY>\n';
		var printPageElem = document.getElementById("printReady");

		if (printPageElem!= null)
		{
			html +='\n<div id="body_content">\n';
			html += printPageElem.innerHTML;
//			html.replace("\<h1\>", "\<div class='hh1'\>");
//			html.replace("\</h1\>", "\</div\>");
			html +='\n</div>\n';
		}
		else
		{
			alert("We cannot find the printable section.");
			return;
		}

		html += '\n</BO' + 'DY>\n</HT' + 'ML>';

		var printWin = window.open("","printFriendly");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
			printWin.print();
	}
	else
	{
		alert("This function can be used only \n by browsers supporting JavaScript (active).");
	}
}

//ascunde / afiseaza un element
function showHideEl(elName, imgName, img1, img2)
{
	el = document.getElementById(elName);
	if(typeof(el) == 'undefined') return false;
	if(el.style.display == 'none')
	{
		el.style.display = 'block';
		chImg(imgName, img2);
	}
	else
	{
		el.style.display = 'none';
		chImg(imgName, img1);
	}
}


function showHideEl_TELLFRIEND(elName1, elName2)
{
	el1 = document.getElementById(elName1);
	el2 = document.getElementById(elName2);
	if(el1.style.display == 'none')
	{

		el1.style.display = 'block';
		el2.style.display = 'none';

	}
	else
	{
		el1.style.display = 'none';
		el2.style.display = 'block';

	}

}




//schimba imaginea
function chImg(elName, img)
{
	el = document.getElementById(elName);
	if(typeof(el) == 'undefined') return false;
	el.src = img;
}

// This function sets a client-side cookie as above.  Only first 2 parameters are required
// Rest of the parameters are optional. If no szExpires value is set, cookie is a session cookie.
//
// Prototype : setCookie(szName, szValue [,szExpires] [,szPath] [,szDomain] [,bSecure])
//******************************************************************************************


function setCookie(szName, szValue, szExpires, szPath, szDomain, bSecure)
{
 	var szCookieText = 	   escape(szName) + '=' + escape(szValue);
	szCookieText +=	 	   (szExpires ? '; EXPIRES=' + szExpires.toGMTString() : '');
	szCookieText += 	   (szPath ? '; PATH=' + szPath : '');
	szCookieText += 	   (szDomain ? '; DOMAIN=' + szDomain : '');
	szCookieText += 	   (bSecure ? '; SECURE' : '');
	
	document.cookie = szCookieText;
}

//******************************************************************************************
// This functions reads & returns the cookie value of the specified cookie (by cookie name) 
//
// Prototype : getCookie(szName)
//******************************************************************************************

function getCookie(szName)
{
 	var szValue =	  null;
	if(document.cookie)	   //only if exists
	{
       	var arr = 		  document.cookie.split((escape(szName) + '=')); 
       	if(2 <= arr.length)
       	{
           	var arr2 = 	   arr[1].split(';');
       		szValue  = 	   unescape(arr2[0]);
       	}
	}
	return szValue;
}

//******************************************************************************************
// To delete a cookie, pass name of the cookie to be deleted
//
// Prototype : deleteCookie(szName)
//******************************************************************************************

function deleteCookie(szName)
{
 	var tmp = 	  			 	 getCookie(szName);
	if(tmp) 
	{ setCookie(szName,tmp,(new Date(1))); }
}

