/*
 * stop javascript errors from popping up
 */
function stopError (  ) { return true; }
window.onerror = stopError;

/*
 * Function to preload images
 */
function preloadIMG ( imgsARR )
{
    path = "img/";
    for ( i=0; i<imgsARR.length; i++ )
    {
    	tmpImgStrJs = "image"+i+" = new Image();\nimage"+i+".src = \""+path+imgsARR[i]+"\";\n";
        eval ( tmpImgStrJs );
    }
}

/*
 * Change an Image
 */
function changeIMG ( imgId, imgSrc )
{
	if ( document.images )
    {
		img = document.getElementById( imgId );
		img.src = imgSrc;
	}
}

/*
 * Submit a form
 */
function submitForm( form_id )
{
	frm = document.getElementById( form_id );
	frm.submit(  );
}

/*
  * Get selected value of dropdown selector
  */
function getSelected( dropdownObject )
{
	sel = dropdownObject.options[dropdownObject.selectedIndex].value;
	return sel;
}

/*
 * Launch Popup Window
 */
function launchPop( wURL, wNAME, wWIDTH, wHEIGHT, wRESIZE, wSCROLL )
{

	var x = null;
	wX = (screen.width!=null)? screen.width/2-wWIDTH/2:0;
	wY = (screen.height!=null)? screen.height/2-wHEIGHT/2:0;
	
	if( wSCROLL == undefined ) wSCROLL = "0";
	if( wRESIZE == undefined ) wRESIZE = "0";
	
	wPARAM  = "left=" + wX + ",top=" + wY;
	wPARAM += ",WIDTH=" + wWIDTH + ",HEIGHT=" + wHEIGHT;
	wPARAM += ",location=0,resizable="+wRESIZE+",titlebar=1,directories=0";
	wPARAM += ",toolbar=0,menubar=0,scrollbars="+wSCROLL+",status=no";

	x = window.open( wURL, wNAME, wPARAM );
	x.focus();
	
}

/*
 * Launch Calendar Popup
 */
function launchCalendar( frm, fld )
{
	wURL = 'index.php?page=pop_calendar&fld=' + frm + '.' + fld;
	launchPop( wURL, 'cal', 200, 200, 1, 0 );
}

/*
 * Focus Field: clear default value
 */
function smartField( fld, mode )
{
	if( mode == 'clear' )
	{
		if( fld.value == fld.defaultValue )
		{
			fld.value = '';
		}
	}
	else if( mode == 'restore' )
	{
		if( fld.value == '' )
		{
			fld.value = fld.defaultValue;
		}
	}
}

/*
 * User Registration Form
 */
function changeUserFormFieldValue( frm )
{
	frm.page.value = "register";
	frm.submit();
}

/*
 * Inscription Form
 */
function changeInscrFormFieldValue( frm )
{
	frm.page.value = "inscription";
	frm.method = 'get';
	frm.submit();
}

/*
 * User Registration Form: change City Dropdown value
 */
var frm_city_str = '';

function selectCity( obj )
{
	sel = getSelected( obj );
	if( sel != 0 )
	{
		frm_city_str = obj.form.city_str.value;
		obj.form.city_str.value = '';
	}
	else
		obj.form.city_str.value = frm_city_str;
}

/*
 * User Registration Form: introduce city string
 */
function introCity( obj )
{
	frm_city_str = obj.value;
	if( frm_city_str != '' )
	{
		obj.form.city.options[ 0 ].selected = true;
	}
}

/*
 * Function to change User Suggestion Form type
 */
function changeSuggType( frm )
{
	frm.page.value = 'suggest';
	frm.submit(  );
}

/*
 * Launch Print Window
 */
function launchPrint( doc_id, doclng )
{
	launchPop( 'index.php?page=doc_print&doc_id=' + doc_id + '&doclng=' + doclng, 'dprint'+doc_id, 600, 500, 1, 1 );
}

/*
 * Launch Image Viewer
 */
function launchImage( img_id, w, h )
{
	launchPop( 'index.php?page=image&rsr_id=' + img_id, 'img'+img_id, w, h );
}

/*
 * Set Form Field value to...
 */
function set_field_value( fld, value )
{
	field = document.getElementById( fld );
	field.value = value;
}

/* Change CSS Class for element
 */
function set_class( elm_id, c )
{
	elm = document.getElementById( elm_id );
	elm.className = c;
}

/* Launch Dictionary
 */
function launchDictionary( key )
{
	launchPop( 'admin/index.php?p=dict_list&txt=' + key, 'dict', 800, 600 );
}

/* Check if valid vote ( Poll )
 */
function votant()
{
   var b;
   var i;
   b=false;
   i=0;
   while ((i <document.votacions.resposta.length)&&(b== false))
   {
      b= document.votacions.resposta[i].checked;
      i++;
   }

  return b;
}

/* Function to preview an image for upload
 */
function previewImg ( box, img, wmax, hmax )
{
	box = box.value.toString(  );
	tmp = box.toLowerCase(  );
	if ( tmp.indexOf( '.jpg' ) != -1 || tmp.indexOf( '.gif' ) != -1 )
	{
		if ( document.images )
		{
			// load img into object
			var pi = new Image(  );
			pi.src = box;
			w = pi.width;
			h = pi.height;
			// if necessary, scale image proportionally
			if ( w > wmax )
			{
				h = ( wmax / w ) * h;
				w = wmax;
			}
			if ( h > hmax )
			{
				w = ( hmax / h ) * w;
				h = hmax;
			}
			// show preview
			document.images[ img ].width = w;
			document.images[ img ].height = h;
			document.images[ img ].src = box;
		}
	}
}

/* Change content of <div>
 */
function change_content(d1,d2,d3,txt)
{
	d=document;
	txt = txt.replace( /\"/gi, "\\\"" );
	if (document.all||document.getElementById)
	{
		js  = "c=d.getElementById? d.getElementById(\""+d3+"\"):d.all."+d3+";\n";
		js += "if(c!=undefined){ c.innerHTML=\""+txt+"\" }";
	}
	else if (document.layers)
	{
		js  = "if( d."+d1+" != undefined ){\n";
		js += "d."+d1+".visibility=\"show\";\n";
		js += "d."+d1+".document."+d2+".document.write(\""+txt+"\");\n";
		js += "d."+d1+".document."+d2+".document.close();\n}";
	}
	eval(js);
}

/* change element's colour
 */
function highlight_id( elm_id, clr )
{
	document.getElementById( elm_id ).style.backgroundColor = clr;
}

/* Trim a string
 */
function trimsc( str )
{
	op = '';
	if( str != '' )
	{
		p1 = str.length-1;
		for( i = 0; i < str.length; i ++ )
		{
			empty = ( str.charAt(i) == ' ' || str.charAt(i) == '\r\n' );
			if( !empty ){ p1 = i; break; }
		}
		
		p2 = str.length-1;
		if( p2 != p1 )
		{
			for( i = p2; i > 0; i -- )
			{
				empty = ( str.charAt(i) == ' ' || str.charAt(i) == '\r\n' );
				if( !empty ){ p2 = i; break; }
			}
			
			op = str.substring( p1, p2 );
		}
	}
	
	return op;
}



function updateFlashTitleCharCount() {
	var lon = 0;
	var max = 40;
	var inp = document.getElementById("dtit");
	var out;
	var demo = document.getElementById("news_flash_title");
	if (inp != null) {
		lon = inp.value.length;
		out = document.getElementById("dtitle_chars");
		out.innerHTML = (max - lon > 0) ? (max - lon) : 0;
		
		demo.innerHTML = inp.value;
	}
}

function updateFlashBodyCharCount() {
	var lon = 0;
	var max = 170;
	var inp = document.getElementById("dlead");
	var out;
	var demo = document.getElementById("news_flash_body");
//	inp.value = inp.value.substring(0, max);
	if (inp != null) {
		lon = inp.value.length;
		out = document.getElementById("dlead_chars");
		out.innerHTML = (max - lon > 0) ? (max - lon) : 0;
		
		demo.innerHTML = inp.value;
	}
}


function updateFlashImage() {
	// Detect browser
	if (navigator.appName.indexOf('Microsoft') != -1) {
		// Browser is IE
		var box = document.getElementById("dfile");
		box = box.value.toString();
		img = 'flash_img';
		wmax = 70;
		hmax = 70;
		
		tmp = box.toLowerCase();
		if ((tmp.indexOf('.jpg') < 0) && (tmp.indexOf('.gif') < 0)) {
			// No valid image. Try original image
			try { 
				tmp = orig_flash_img.toLowerCase();
			}
			catch (e) {
				tmp = "";
			}
		}
		
		if ((tmp.indexOf('.jpg') != -1) || (tmp.indexOf('.gif') != -1)) {
			if (document.images) {
				// Show image area
				var elem = document.getElementById("flash_img_area");
				elem.style.display = "";
				
				// load img into object
				var pi = new Image();
				pi.src = tmp;
				
				w = pi.width;
				h = pi.height;
				if ((w <= 0) && (h <= 0)) {
					document.images[img].width = 1;
					document.images[img].height = 1;
					document.images[img].style.marginLeft = 0;
					document.images[img].style.marginTop = 0;
					document.images[img].src = "img/trans.gif";
				}
				else {
					var ratio1 = w / wmax;
					var ratio2 = h / hmax;
					var ratio = Math.min(ratio1, ratio2);
					
					var destWidth = Math.round(w / ratio);
					var destHeight = Math.round(h / ratio);
					
					var margin_left = -1 * Math.max(Math.round((destWidth - wmax) / 2), 0);
					var margin_top = -1 * Math.max(Math.round((destHeight - hmax) / 2), 0);
					
					document.images[img].width = destWidth;
					document.images[img].height = destHeight;
					document.images[img].style.marginLeft = margin_left;
					document.images[img].style.marginTop = margin_top;
					document.images[img].src = tmp;
				}
			}
			else {
				// Hide image area
				var elem = document.getElementById("flash_img_area");
				elem.style.display = "none";
			}
		}
		else {
			// Hide image area
			var elem = document.getElementById("flash_img_area");
			elem.style.display = "none";
		}
	}
}
