// Created by Ovidiu Sopa
// 22.05.2006
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function asc(obj) {
	if(document.getElementById(obj)){
		var element = document.getElementById(obj);
	}
	else if(document.all){ // this is the way old msie versions work
		//alert("In document.all");
		var element = document.all[obj];
	}
	else if (document.layers){ // this is the way nn4 works
		var element = document.layers[obj];
	}
	 
	if(element.className == 'show') 
	{
		document.getElementById(obj).className = 'ascunde';
		//element.className = 'hide';
	}
	else if(element.className == 'ascunde' || element.className == '')
	{
		document.getElementById(obj).className = 'show';
		//element.className = 'hide';
	}
	//alert(obj);
	
	if(obj == 'calstiri') var tab = 'calevenimente';
	else if(obj == 'calevenimente') var tab = 'calstiri';
	
	if(obj == 'calstiri' && element.className == 'show') document.getElementById(tab).className = 'ascunde';
	else if(obj == 'calstiri' && element.className == 'ascunde') document.getElementById(tab).className = 'show';
	if(obj == 'calevenimente' && element.className == 'show') document.getElementById(tab).className = 'ascunde';
	else if(obj == 'calevenimente' && element.className == 'ascunde') document.getElementById(tab).className = 'show';
}

function switch_layers(show_this,hide_this){	
	if(show_this != '') document.getElementById(show_this).className = 'show';
	if(hide_this != '') document.getElementById(hide_this).className = 'ascunde';
	document.getElementById(show_this).focus();
	}

function open_window(link, width, height)
{
     window.open(link,"","toolbar=0,top=0,scrollbars=yes, width="+width+",height="+height+"");
}

function confirmLink(theLink, theSqlQuery)
{
    var is_confirmed = confirm(theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
}

function keyCounter(field, countfield, maxlimit)
{
  //return inputString.split(' ').length;
  var countfield = countfield;
	document.getElementById('keycount').innerHTML = document.add_new.keywords.value.split(' ').length;
	if(document.add_new.keywords.value.split(' ').length > maxlimit)
	{
		document.getElementById('keycount').style.color = 'FF3333';
	}
	else if(document.add_new.keywords.value.split(' ').length < maxlimit)
	{
		document.getElementById('keycount').style.color = '006600';	
	}
}

function textCounter(field, countfield, maxlimit) {
	//alert(document.add_new.description.value.length);
	
	var countF = document.getElementById(countfield);
	document.getElementById('countfield').innerHTML = document.add_new.description.value.length;
	if(document.add_new.description.value.length > maxlimit)
	{
		document.getElementById('countfield').style.color = 'FF3333';
	}
	else if(document.add_new.description.value.length < maxlimit)
	{
		document.getElementById('countfield').style.color = '006600';	
	}
}

function printWindow(){
	navi = parseInt(navigator.appVersion)
   	if (navi >= 4) window.print()
	}

function copy_clip(meintext)
{
 if (window.clipboardData) 
   {
   // the IE-manier
   window.clipboardData.setData("Text", meintext);
   }
   else if (window.netscape) 
   { 
   // you have to sign the code to enable this, or see notes below 
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                 .createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // maak een transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   trans.addDataFlavor('text/unicode');

   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;1"]
                .createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*2);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
   
   }
   //alert("Following info was copied to your clipboard:\n\n" + meintext);
   
   // NEW ADDED 
   document.getElementById('message').innerHTML = "<br>&nbsp;Copied to clipboard : <b>" + meintext + "</b><br>";
   return false;
}

function tolower()
{
	// defineste numele
	var form = 'document.add_new';
	var field = 'title';
	var text = document.add_new.title.value;
	//eval(form+"."+field+".value.toLowerCase()");
	alert ("1."+document.add_new.title.value.toLowerCase());
	alert ("2."+text.toLowerCase());
	document.add_new.title.value = document.add_new.title.value.toLowerCase();
	
}



/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
function openCalendar(params, form, field, type) {
    window.open("./calendar.php?" + params, "calendar", "width=400,height=200,status=yes");
    dateField = eval("document." + form + "." + field);
    dateType = type;
}

function show_description(msg,target,url){
	if(url == '') var full_msg = msg;
	else var full_msg = '<a href="'+url+'" title="'+msg+'" class="aaa">'+msg+'</a>';
	
	document.getElementById(target).innerHTML = full_msg;
	}

function form_reset(form_name,fieldsets){
	var form = eval("document."+form_name);
	//form.reset;
	var fieldset = new Array('field_one','field_two','field_three','field_four');
	
	for(x=0;x<fieldsets;x++){
		document.getElementById(fieldset[x]).className = 'main';
		}

	document.getElementById('reset_button').className = 'ascunde';
	//alert(document.anunturi.length);
	var reset_ = document.anunturi.reset();
	return false;
}

function add_to_favorites(title,url,user_id){
	document.writeln('<div style="border:1px solid #333333; position:absolute;"><IFRAME SRC="recipe.html" TITLE="The Famous Recipe" width="300" height="200"><!-- Alternate content for non-supporting browsers --><H2>The Famous Recipe</H2><H3>Ingredients</H3>...</IFRAME></div>');
	}

//////////////////////
// GALERIE
////////////////

	
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

/* ---------------------------------------
   author: Vincent Puglia, GrassBlade Software
   site:   http://members.aol.com/grassblad
------------------------------------------- */

function selectAll(formObj, isInverse) 
{
   for (var i=0;i < formObj.length;i++) 
   {
      fldObj = formObj.elements[i];
      if (fldObj.type == 'checkbox')
      { 
         if(isInverse)
            fldObj.checked = (fldObj.checked) ? false : true;
         else fldObj.checked = true; 
       }
   }
}

function close_tooltip(tooltip,user_id,update_db)
{
	asc(tooltip);
	if(update_db == 1) update_tooltip_field(tooltip,user_id);
}

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

function scrollToDivID(divID){
	var divID = divID;
	eval("document.all." + divID + ".scrollIntoView(true)");
}
function scrollToTop(){
	window.scrollTo(0,0);
}

// AJAX pentru voturi/////////////////////////////////////////////////////////////////////////////
// Declare our request variable.
var request = false;

// Define a function that will make our request for us:
function ajax_vote(table,id,vote) {
    // Clear the curent request
    request = false;
	var table = table;
	var id = id;
	var vote = vote;
	var loading_dv = "loading_div_" + id;
	if(document.getElementById(loading_dv))
	{
		//alert("cu div");
		document.getElementById(loading_dv).className = '';
	}
	else
	{
		//alert("fara div");
		document.getElementById('loading_div').className = '';
	}
	
	// Generate the request object and handle different browsers:
    if (window.XMLHttpRequest) { // Mozilla & other compliant browsers
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // Internet Explorer
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // If we don't have a request object, then error out.
    if (!request) {
        alert('Browser does not support AJAX!');
        return false;
    }

    // Ok, now we are ready.  Make the request, and tell it to run the
    // function 'updateDate' when it gets data back.
    request.onreadystatechange = updateVotes;

    // Open the connection, sending the current value of the form element:
    request.open('GET',
        'http://www.sibiul.net/ajax-vote.php?table=' + table + '&id=' + id + '&vote=' +vote)
    request.send(null);
}

// The function that will accept the data, and update the page:
function updateVotes() {
    // Make sure that the state is '4', which means finished:
    if (request.readyState == 4) {
        // Make sure that the status is 200, or 'ok'
        if (request.status == 200) {
			
			
			
			var response = request.responseText;
			var update = new Array();
	
			if(response.indexOf('^') != -1) {
				update = response.split('^');
				var test_id = 'vote_results_' + update[0];
				if(document.getElementById(test_id))
				{
					//alert('exista div cu id');	
       			    var text = document.getElementById(test_id);
					if(update[1] != '')
					{
						text.innerHTML = update[1];
					}
				}
				else
				{
					//alert(' NU exista div cu id');	
       			    var text = document.getElementById('vote_results');
					if(update[1] != '')
					{
						text.innerHTML = update[1];
					}
				}
				
				
			}
	
			
			
			
           /* // And now, update the text on the page:
            var text = document.getElementById('vote_results');
            //text.innerHTML = result.firstChild.data;
			if(request.responseText != '')
			{
				text.innerHTML = request.responseText;
			}*/
        } else {
            alert('Error performing request!' + request.status);
        }
    }
}


// AJAX pentru CALENDAR/////////////////////////////////////////////////////////////////////////////
// Declare our request variable.

// Define a function that will make our request for us:
function changeCalendarMonth(c_year,c_month,direction) {
    // Clear the curent request
    request = false;
	var c_year = c_year;
	var c_month = c_month;
	var direction = direction;

	document.getElementById('cal_loading_div').className = '';
	
	// Generate the request object and handle different browsers:
    if (window.XMLHttpRequest) { // Mozilla & other compliant browsers
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // Internet Explorer
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // If we don't have a request object, then error out.
    if (!request) {
        alert('Browser does not support AJAX!');
        return false;
    }

    // Ok, now we are ready.  Make the request, and tell it to run the
    // function 'updateDate' when it gets data back.
    request.onreadystatechange = updateCalendar;

    // Open the connection, sending the current value of the form element:
    request.open('GET',
        'http://www.sibiul.net/ajax-calendar.php?year=' + c_year + '&month=' + c_month + '&direction=' +direction)
    request.send(null);
}

// The function that will accept the data, and update the page:
function updateCalendar() {
    // Make sure that the state is '4', which means finished:
    if (request.readyState == 4) {
        // Make sure that the status is 200, or 'ok'
        if (request.status == 200) {
            // And now, update the text on the page:
            var text = document.getElementById('calendar_stiatica');
            //text.innerHTML = result.firstChild.data;
			if(request.responseText != '')
			{
				text.innerHTML = request.responseText;
			}
        } else {
            alert('Error performing request!' + request.status);
        }
    }
}

function xwd_show_title(element,title)
{
	var elem = document.getElementById(element);
	elem.innerHTML = title;
}

function search_link(keyword)
{
	var keyword = keyword;
	window.location = "http://www.sibiul.net/cauta/1/"+keyword;
}
