function edit_categorie(id, name, parent, description, active)
{
	name = document.getElementById('ed_name_'+id).value;
	parent = document.getElementById('ed_parent_'+id).value;
	description = document.getElementById('ed_description_'+id).value;
	active = document.getElementById('ed_active_'+id).value;
	
	document.getElementById('name').value = name;
    document.getElementById('parent_' + parent).selected = 'selected';
    document.getElementById('description').value = description; 
	if (active == '1') 
	{
		document.getElementById('active').checked = 'checked';
	}
	else
	{
			document.getElementById('active').checked = '';
	}
	document.getElementById('categorie_action').value = 'edit';
	document.getElementById('edit_id').value = id;
}

function checkAll(form, checkboxName) {
    var theForm, z = 0;
    theForm = document.getElementById(form).getElementsByTagName('input');
     for(z=0; z<theForm.length;z++){
      if ((theForm[z].type == 'checkbox') && (theForm[z].name == checkboxName))
	  {
      theForm[z].checked = true;
      }
    }
}
 
function uncheckAll(form, checkboxName) {
    var theForm, z = 0;
    theForm = document.getElementById(form).getElementsByTagName('input');
     for(z=0; z<theForm.length;z++){
      if ((theForm[z].type == 'checkbox') && (theForm[z].name == checkboxName))
	  {
      theForm[z].checked = false;
      }
    }
}

function fillAll(form, inputClass, value) {
    var theForm, z = 0;
    theForm = form;
     for(z=0; z<theForm.length;z++){
      if ((theForm[z].type == 'text') && (theForm[z].className == inputClass))
	  {
      	  theForm[z].value = value;
      }
    }
}

function count_words(text, output_id, max_words)
{
	words_number = text.split(' ').length;
	if (words_number <= max_words)
	{
		document.getElementById(output_id).style.color = '#666666';
	}
	else
	{
		document.getElementById(output_id).style.color = '#FF0000';
	}
	document.getElementById(output_id).innerHTML = words_number;
}

function create_ajax(path, obj)
{
	var ajaxobj = false;
	if (window.XMLHttpRequest)
	{
		ajaxobj = new XMLHttpRequest();	
	}
	else if(window.ActiveXObject)
	{
		ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");	
	}
	
	ajaxobj.onreadystatechange = function()
	{
		if (ajaxobj.readyState==4 && ajaxobj.status==200)
		{
			if (obj!='') obj.value = ajaxobj.responseText;
		}
	}
	
	ajaxobj.open("GET", path);
	ajaxobj.send(null);
}


function save_magazin(path, id, trid, firmaid, action)
{
	name = document.getElementById('name_'+trid).value;
	localitate = document.getElementById('localitate_'+trid).value;
	address = document.getElementById('address_'+trid).value;
	persoana = document.getElementById('persoana_'+trid).value;
	tel = document.getElementById('tel_'+trid).value;
	fax = document.getElementById('fax_'+trid).value;
	mobil = document.getElementById('mobil_'+trid).value;
	email = document.getElementById('email_'+trid).value;
	web = document.getElementById('web_'+trid).value;
	cord_lat = document.getElementById('cord_lat_'+trid).value;
	cord_long = document.getElementById('cord_long_'+trid).value;
	judet = document.getElementById('judet_'+trid).value;
	
	path = path+'?id='+id
			   +'&firmaid='+firmaid
			   +'&judet='+judet
			   +'&name='+name
			   +'&localitate='+localitate
			   +'&address='+address
			   +'&persoana='+persoana
			   +'&tel='+tel
			   +'&fax='+fax
			   +'&mobil='+mobil
			   +'&email='+email
			   +'&web='+web
			   +'&cord_lat='+cord_lat
			   +'&cord_long='+cord_long;
			   
	create_ajax(path);
	
	if (localitate!= '') td_address = localitate+', '+address; else td_address = address;
	td_contact = 'Contact: '+persoana+'<br>Tel: '+tel+'<br>Fax: '+fax+'<br>Mobil: '+mobil+'<br>E-mail: '+email+'<br>Web: '+web;
	document.getElementById('td_name_'+trid).innerHTML = name;
	document.getElementById('td_address_'+trid).innerHTML = td_address;
	document.getElementById('td_contact_'+trid).innerHTML = td_contact;
	
	//if (id == '') document.location.href='account_magazine.php';
}


function actualize_magazine()
{
	mtr = document.getElementById('magazine_table').getElementsByTagName('tr');
	for (i=0; i<mtr.length; i++)
	{
		mtr[i].style.display = 'none';
	}
	
	judete_inputs = document.getElementById('judete_table').getElementsByTagName('input');
	for (i=0; i<judete_inputs.length; i++)
	{
		if ((judete_inputs[i].type = 'checkbox') && (judete_inputs[i].checked == true))
		{
			judet = judete_inputs[i].value;
			magazine_inputs = document.getElementById('magazine_table').getElementsByTagName('input');
			for (j=0; j<magazine_inputs.length; j++)
			{
				if (magazine_inputs[j].type == 'hidden')
				{
					magjud = magazine_inputs[j].value.split("_");
					if (magjud[0] == judet) document.getElementById('mtr_'+magjud[1]).style.display = '';
				}
			}
		}
	}
	
	mtr = document.getElementById('magazine_table').getElementsByTagName('tr');
	for (i=0; i<mtr.length; i++)
	{
		if (mtr[i].style.display == 'none')
		{
			mtr_id = mtr[i].id.split("_");
			document.getElementById('magazin_'+mtr_id[1]).checked = false;
		}
	}
}

function scroll_thumb(direction, a)
{
	if ((a>2) || (a<-2))
	{
		gal = document.getElementById('thumb_view');
		gal.scrollLeft += a;
		setTimeout('scroll_thumb(\''+direction+'\','+(a/1.5)+')',20);
	}
}

function mark_date(type, position, year, month, day, unit_price, min_days, max_days)
{
	started = document.getElementById('started').value;
	if (started == '') 
	{		
		calendar_td = document.getElementById('ext_cal_'+type+'_'+position).getElementsByTagName('td');
		for (i=0; i<calendar_td.length; i++)
		{
			if ((calendar_td[i].style.color == 'rgb(255, 255, 255)') && (calendar_td[i].style.backgroundColor == 'rgb(241, 88, 34)'))
			{
				calendar_td[i].style.backgroundColor = '#FFFFFF';
				calendar_td[i].style.color = '#D77852';
			}
		}
		
		if ((document.getElementById('type').value != '') && (document.getElementById('position').value != '') && ((document.getElementById('type').value != type) || (document.getElementById('position').value != position)) )
		{
			document.getElementById('ext_text_'+document.getElementById('type').value+'_'+document.getElementById('position').value).style.display = 'none';
		}
		document.getElementById('ext_text_submit_'+type+'_'+position).style.display = 'none';
		document.getElementById('ext_text_error_'+type+'_'+position).innerHTML = '';
		document.getElementById('type').value = type;
		document.getElementById('position').value = position;
		document.getElementById('date_start').value = year+'-'+month+'-'+day;
		document.getElementById('date_end').value = '';
		document.getElementById('ext_text_'+type+'_'+position).style.display = '';
		document.getElementById('ext_text_price_'+type+'_'+position).innerHTML = 'Selecteaza data terminarii';
		document.getElementById('ext_text_date_'+type+'_'+position).innerHTML = 'Data: '+day+'.'+month+'.'+year+' - ';
		document.getElementById('started').value = '1';
		
		if (month-1 == 0) js_month = 12; else js_month = month-1;
		document.getElementById('ext_date_'+type+'_'+position+'_'+year+'-'+month+'-'+day).style.color = '#FFFFFF';
		document.getElementById('ext_date_'+type+'_'+position+'_'+year+'-'+month+'-'+day).style.backgroundColor = '#f15822';
	}
	else
	if ( (type == document.getElementById('type').value) && (position == document.getElementById('position').value) )
	{
		total_count = 1;
		var date_end = new Date(year, month-1, day);
		var date_start_split = document.getElementById('date_start').value.split('-');
		var date_start = new Date(date_start_split[0], date_start_split[1]-1, date_start_split[2]);
		
		force_end = false;
		while ( ((date_end.getTime()-date_start.getTime()) > 0) && (force_end == false) )
		{
			date_start.setDate(date_start.getDate()+1);
			var curr_date = date_start.getDate();
			var curr_month = ((date_start.getMonth()+1)%13); 
			var curr_year = date_start.getFullYear();
			if (curr_month == 0) curr_month = 1;
			if (curr_month<10) curr_month = '0'+curr_month;
			total_count++;
			
			if (!document.getElementById('ext_date_'+type+'_'+position+'_'+curr_year+'-'+curr_month+'-'+curr_date))
			{			
				date_start.setDate(date_start.getDate()-1);
				curr_date = date_start.getDate();
				curr_month = ((date_start.getMonth()+1)%13); 
				curr_year = date_start.getFullYear();
				if (curr_month == 0) curr_month = 1;
			
				document.getElementById('date_end').value = curr_year+'-'+curr_month+'-'+curr_date;
				document.getElementById('ext_text_'+type+'_'+position).style.display = '';
				document.getElementById('ext_text_date_'+type+'_'+position).innerHTML += curr_date+'.'+curr_month+'.'+curr_year;
				document.getElementById('started').value = '';
				force_end = true;
			}
			else
			{
				document.getElementById('ext_date_'+type+'_'+position+'_'+curr_year+'-'+curr_month+'-'+curr_date).style.color = '#FFFFFF';
				document.getElementById('ext_date_'+type+'_'+position+'_'+curr_year+'-'+curr_month+'-'+curr_date).style.backgroundColor = '#f15822';
			}
		}
		
		if (force_end == false)
		{
			document.getElementById('date_end').value = year+'-'+month+'-'+day;
			document.getElementById('ext_text_'+type+'_'+position).style.display = '';
			document.getElementById('ext_text_date_'+type+'_'+position).innerHTML += day+'.'+month+'.'+year;
			document.getElementById('started').value = '';
		}

		total_price = total_count * parseFloat(unit_price);
		document.getElementById('ext_text_price_'+type+'_'+position).innerHTML = 'Pret: '+total_price+' RON';
	
		if (total_count < min_days)
		{
			document.getElementById('ext_text_error_'+type+'_'+position).innerHTML = 'Trebuie selectate minim '+min_days+' zile!';
		}
		else
		if (total_count > max_days)
		{
			document.getElementById('ext_text_error_'+type+'_'+position).innerHTML = 'Trebuie selectate maxim '+max_days+' zile!';
		}
		else
		{
			document.getElementById('ext_text_submit_'+type+'_'+position).style.display = '';  
		}
	}
}

function promotie_save(pret_allow)
{
	error = 0;
	msg = '';
	
	if (document.getElementById('name').value == '')
	{
		error = 1;
		msg += 'Nume necompletat!\n';
	}
	
	if ((document.getElementById('pret').value == '') && (pret_allow != '1'))
	{
		error = 1;
		msg += 'Pret initial necompletat!\n';
	}
	
	if (document.getElementById('categorie').value == '')
	{
		error = 1;
		msg += 'Alege o subcategorie!\n';
	}

	if ((isNaN(document.getElementById('pret').value) || (document.getElementById('pret').value == " ")) && (pret_allow != '1'))
	{
		error = 1;
		msg += 'Pretul initial trebuie sa fie un numar!\n';
	}
	
	if ((document.getElementById('promo_1').value == '') && (document.getElementById('promo_2').value == '') &&
	    (document.getElementById('promo_3').value == '') && (document.getElementById('promo_4').value == ''))
	{
		error = 1;
		msg += 'Promotie necompletata!\n';
	}
	
	if (pret_allow != '1')
	if ((document.getElementById('promo_1').value != '') && (parseFloat(document.getElementById('promo_1').value) >= parseFloat(document.getElementById('pret').value)))
	{
		error = 1;
		msg += 'Pretul promotional este mai mare de cel initial!\n';
	}
	
	mag_error = 1;
	for (i=0; i<tot_mag.length; i++)
	{
		if (document.getElementById('magazin_'+tot_mag[i]).checked == true)
			mag_error = 0;
	}
	
	if (mag_error == 1) 
	{
		error = 1;
		msg += 'Alegeti cel putin un magazin!\n';
	}
	
	if (error == 0)
	{
		document.getElementById('promotie_form').submit();
	}
	else
	{
		alert(msg);
	}
}

function update_firma_clicks(firmaid)
{
	create_ajax('ajax/update_firma_clicks.php?firmaid='+firmaid, '');
}

var constRotate = 'top';

function contentRotator(parentID)
{
	if (constRotate == 'top') 
	{
		document.getElementById(parentID).scrollTop +=1;
	}
	
	while (document.getElementById(parentID).childNodes[0].childNodes[0].rel != 'scrollItem')  document.getElementById(parentID).childNodes[0].removeChild(document.getElementById(parentID).childNodes[0].childNodes[0]);
	
	if ((constRotate == 'top') || (constRotate == 'fasttop'))
	{
		if (document.getElementById(parentID).scrollTop > (document.getElementById(parentID).childNodes[0].childNodes[0].childNodes[0].height +11 ))
		{
			document.getElementById(parentID).childNodes[0].appendChild(document.getElementById(parentID).childNodes[0].firstChild);
			document.getElementById(parentID).scrollTop = 0;
		}
	}
	
	setTimeout('contentRotator("'+parentID+'")',50);
}
function url_function(url_string)
{
	url_array=url_string.split('_#_');
		for(i=0;i<url_array.length;i++)
		{
			window.open(url_array[i],'win'+i,'location=1,status=1,scrollbars=1,width=100,height=100');
		}
}

headerOver = 0;

function headerRotator(addHeight)
{	
	document.getElementById('header_promo_1').style.height = (parseInt(document.getElementById('header_promo_1').style.height) + addHeight) + 'px';
	document.getElementById('header_promo_2').style.height = (parseInt(document.getElementById('header_promo_2').style.height) + addHeight) + 'px';
	document.getElementById('header_promo_3').style.height = (parseInt(document.getElementById('header_promo_3').style.height) + addHeight) + 'px';
	document.getElementById('header_promo_4').style.height = (parseInt(document.getElementById('header_promo_4').style.height) + addHeight) + 'px';
	
	height = parseInt(document.getElementById('header_promo_1').style.height);
	
	if (headerOver == 0)
	{
		if (((height <= 80) && (addHeight > 0)) || ((height <= 20) && (addHeight < 0)))
		{
			setTimeout('headerRotator(1)',120);
		}
		else
		if (((height >= 80) && (addHeight >= 0)) || ((height >= 20) && (addHeight <= 0)))
		{
			setTimeout('headerRotator(-1)',120);
		}
	}
	else
	{
		document.getElementById('header_promo_1').style.height = '80px';
		document.getElementById('header_promo_2').style.height = '80px';
		document.getElementById('header_promo_3').style.height = '80px';
		document.getElementById('header_promo_4').style.height = '80px';
		setTimeout('headerRotator(0)',70);
	}
}
