var oldLink = null;
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}
// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function showFlatCalendar(id, holder, format, selectedHandler) {
  var prnt = document.getElementById(holder);
  var element = document.getElementById(id);
  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, selectedHandler);
  // hide week numbers
  cal.setDateFormat(format);
  cal.weekNumbers = false;
  cal.create(prnt);
  cal.parseDate(element.value);
  cal.show();
}

function hide(divtohide, idr, ids)
{
var divh=document.getElementById(divtohide);
var rcheck=document.getElementById(idr);
var myids = ids.split(';');
if(rcheck.checked===true)
{
	divh.style.display="none";
	//for(var i=0; i<myids.length; i++)
	//{
		//document.getElementById(myids[i]).value=0;
	//}
}
else {divh.style.display="";}
}

function totul(div, divs, nr)
{
var zice=document.getElementById(div);
var id='';
if(zice.checked===true)
{
for(var i=1; i<=nr; i++)
{
id= divs+i;
document.getElementById(id).checked=true;
}
}
if(zice.checked===false)
{
for(var i=1; i<=nr; i++)
{
id= divs+i;
document.getElementById(id).checked=false;
}
}
}
function showhide (divtoshow, divtohide)
{
var divs=document.getElementById(divtoshow);
var divh=document.getElementById(divtohide);
divs.style.display="";
divh.style.display="none";
}
function suta(ids)
{
var myids = ids.split(';');
var suma=0;
for(var i=0; i<myids.length; i++)
{
suma = suma + parseInt(document.getElementById(myids[i]).value);
}
if(suma>100)
{
window.alert('Procentele nu pot depasi 100%!');
}
if(suma<100 && suma!=0)
{
window.alert('Procentele trebuie sa insumeze 100%!');
}
}

function updateValue(aid,m_name,m_value)
{
	var what = document.getElementById(aid);
	if(what!=null)
	{
		whatValue = what.value+","+m_value;
		what.value = whatValue;
	} else
		{
			document.write('<input type="hidden" name="'+m_name+'" id="'+aid+'" value="'+m_value+'">');
		}
}

function verifbuget(bmare, bmici)
{
bugetmare=parseInt(document.getElementById(bmare).value);
var mybm = bmici.split(';');
bugetmic=0;
pret=0;
for(i=0; i<mybm.length; i++)
{
divb=mybm[i]+'CampanieurlBuget';
diva=mybm[i]+'CampanieurlPretafisari';
divp=mybm[i]+'CampanieurlPretplay';
divc=mybm[i]+'CampanieurlPretclick';
bm=parseInt(document.getElementById(divb).value);
bugetmic=bugetmic+bm;
a=parseInt(document.getElementById(diva).value);
p=parseInt(document.getElementById(divp).value);
c=parseInt(document.getElementById(divc).value);
//if(bm<(a+p+c))
//{
//pret=1;
//}
}
if(bugetmare<bugetmic)
{
window.alert('Bugetul total al campaniei nu poate fi mai mic decat suma bugetelor pentru fiecare url!');
return false;
}
else
{
if(pret==1)
{
window.alert('Preturile insumate nu pot fi mai mari decat bugetul fiecarui url!');
return false;
}
return true;
}
}

function readd(cid,eid,link)
{
var idc = document.getElementById(cid).value;
var ide = document.getElementById(eid).value;
var locatie=link+idc+'/'+ide+'/';
window.location=locatie;
}

function redelete(cid,eid,link)
{
var idc = document.getElementById(cid).value;
var ide = document.getElementById(eid).value;
var cr = document.getElementById(eid).checked;

var locatie=link+idc+'/'+ide+'/';
self.location = locatie;

}

function redel(cuid, cid,link)
{
var idcu = document.getElementById(cuid).value;
var idc = document.getElementById(cid).value;
var cr = document.getElementById(cuid).checked;
//window.alert(cr);
if(cr==false)
{
var locatie=link+idcu+'/'+idc+'/';
self.location = locatie;
}
}

function vsuta(model)
{
var id =model+'Sfeminin;'+model+'Smasculin';
var mys = id.split(';');
var sumas=0;
for(var i=0; i<mys.length; i++)
{
sumas = sumas + parseInt(document.getElementById(mys[i]).value);
}

id=model+'Vtineri;'+model+'Vmaturi;'+model+'Vbatrani';
var myv = id.split(';');
var sumav=0;
for(var i=0; i<myv.length; i++)
{
sumav = sumav + parseInt(document.getElementById(myv[i]).value);
}

id=model+'Escoala;'+model+'Eliceu;'+model+'Efacultate;'+model+'Epostfac';
var mye = id.split(';');
var sumae=0;
for(var i=0; i<mye.length; i++)
{
sumae = sumae + parseInt(document.getElementById(mye[i]).value);
}

id=model+'Lconserv;'+model+'Lexplore;'+model+'Lmainstr;'+model+'Laspirer;'+model+'Lstruggler';
var myl =id.split(';');
var sumal=0;
for(var i=0; i<myl.length; i++)
{
sumal = sumal + parseInt(document.getElementById(myl[i]).value);
}
//window.alert('Cica merge vsuta');

if(sumas>100)
{
window.alert('Procentele nu pot depasi insumat 100% pentru targetul sex!');
ms=0;
}
else
{
	if(sumas<100 && sumas!=0)
	{
	window.alert('Procentele trebuie sa fie exact 100% pentru targetul sex!');
	ms=0;
	}
	else
	{ms=1;}
}
if(sumav>100)
{
window.alert('Procentele nu pot depasi insumat 100% pentru targetul varsta!');
mv=0;
}
else
{
	if(sumav<100 && sumav!=0)
	{
	window.alert('Procentele trebuie sa fie exact 100% pentru targetul varsta!');
	mv=0;
	}
	else
	{mv=1;}
}

if(sumae>100)
{
window.alert('Procentele nu pot depasi insumat 100% pentru targetul educatie!');
me=0;
}
else
{
	if(sumae<100 && sumae!=0)
	{
	window.alert('Procentele trebuie sa fie exact 100% pentru targetul educatie!');
	me=0;
	}
	else
	{me=1;}
}

if(sumal>100)
{
window.alert('Procentele nu pot depasi insumat 100% pentru targetul lifistyle!');
ml=0;
}
else
{
	if(sumal<100 && sumal!=0)
	{
	window.alert('Procentele trebuie sa fie exact 100% pentru targetul lifistyle!');
	ml=0;
	}
	else
	{ml=1;}
}
if(ms==1 && mv==1 && me==1 && ml==1)
{return true;}
else
{return false;}
}

function verifdata(model)
{
var sdata= new Date();
var edata= new Date();
var merge=0;
if (model=='Campanie')
{
startd=model+'Startdate';
endd=model+'Enddate';
}
else
{
startd=model+'Startval';
endd=model+'Endval';
}
sdata=document.getElementById(startd).value;
edata=document.getElementById(endd).value;
//window.alert(sdata);
//window.alert(edata);
//window.alert('Cica merge verifdata');
if(edata<sdata)
{
window.alert('Data de final trebuie sa fie mai mare ca data de inceput!');
merge=0;
}
else
{merge=1;}
if (model=='Campanie')
{model='Target';}
var merges=vsuta(model);
if(merge==1 && merges)
{
return true;
}
else
{return false;}
}

function maxmedmin()
{
var sdata= new Date();
var edata= new Date();
var merge1=0;
var merge2=0;
startd='UrldataStartval';
endd='UrldataEndval';
sdata=document.getElementById(startd).value;
edata=document.getElementById(endd).value;
//window.alert(sdata);
//window.alert(edata);
//window.alert('Cica merge verifdata');
if(edata<sdata)
{
window.alert('Data de final trebuie sa fie mai mare ca data de inceput!');
merge1=0;
}
else
{merge1=1;}
var max=  parseInt(document.getElementById('UrldataDmaxafisari').value);
var med=  parseInt(document.getElementById('UrldataDmedieafisari').value);
var min=  parseInt(document.getElementById('UrldataDminimafisari').value);
if (max<med)
{
window.alert('Numarul maxim de afisari trebuie sa fie cel mai mare!');
merge2=0;
}
else
{
if(med<min || max<min)
{
window.alert('Numarul minim de afisari trebuie sa fie cel mai mic!');
merge2=0;
}
else
{merge2=1;}
}
if(merge1==1 && merge2==1)
{
return true;
}
else
{return false;}
}
function moveThem(whom,cpos)
{
document.getElementById('TargetWhom').value = whom;
document.getElementById('TargetCpos').value = cpos;
document.Campanie.submit();
}
function limitpg(div,link)
{
var limit=document.getElementById(div).value;
var locatie=link+'/limita:'+limit;
self.location = locatie;
}
function inflash(titlu)
{
if(document.getElementById('ArticoleInflash').checked===true)
{
var msg='In aceasta sectiune articolul "'+titlu+'" este bifat pentru a fi afisat in flash! Daca pastrezi bifat articolul actual el va fi cel nou afisat in flash.';
window.alert(msg);
}
}
function infl()
{
var idsect='ArticoleSection'+document.getElementById('ArticoleSectionAjuta').value;
window.alert(idsect);
var titlu=document.getElementById(idsect).value;
window.alert(titlu);
if(document.getElementById('ArticoleInflash').checked===true)
{
var msg='In sectiunea aleasa articolul "'+titlu+'" este bifat pentru a fi afisat in flash! Daca pastrezi bifat articolul actual el va fi cel nou afisat in flash.';
window.alert(msg);
}
}
function ajutor()
{
var sect='ArticoleSection'+document.getElementById('ArticoleSectiuneId').value;
window.alert(sect);
var parinte=document.getElementById(sect).value;
document.getElementById('ArticoleSectionAjuta').value=parinte;
window.alert(parinte);
}
function filtrusect(linc, idsect)
{
var sect=document.getElementById(idsect).value;
var locatie=linc+sect;
self.location = locatie;
}

var tt=0;

function appearc(where,what){
	$("#"+where).load(what);
}

function apppsear(where,what)
{
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(where);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			return true;
		}
	}
//	var reg_tel =  eval('document.reg_toSite.reg_tel' + '.value');
		var queryString = what;
		ajaxRequest.open("GET", queryString, true);
		ajaxRequest.send(null);
}

function appearp(where){
	if(where=='haf'){
		document.getElementById('gre').src = '/images/gc_nesel.png';
		document.getElementById('sis').src = '/images/sis_nesel.png';
		document.getElementById('haf').src = '/images/hf_sel.png';
	}

	if(where=='gre'){
		document.getElementById('gre').src = '/images/gc_sel.png';
		document.getElementById('sis').src = '/images/sis_nesel.png';
		document.getElementById('haf').src = '/images/hf_nesel.png';
	}

	if(where=='sis'){
		document.getElementById('gre').src = '/images/gc_nesel.png';
		document.getElementById('sis').src = '/images/sis_sel.png';
		document.getElementById('haf').src = '/images/hf_nesel.png';
	}
}