/*
 * 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 );
}

/*
 * Launch User Name Suggestion Popup
 */
function launchLoginSugg( frm, fld )
{
	_form = document.getElementById( frm );
	if( _form != undefined )
	{
		_log = _form['log'].value;
		_fname = _form['fname'].value;
		_sname = _form['sname'].value;
		
		if( _log != undefined && _fname != undefined && _sname != undefined )
		{
			wURL = 'index.php?page=login_availability&fld=' + frm + '.' + fld + '&log=' + _log +  '&fname=' + _fname +  '&sname=' + _sname;
			launchPop( wURL, 'logsugg', 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);
}