/*********************************************************/
/* General functions                                     */
/*********************************************************/

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function openWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
  return false;
}

function setTextOfLayer(objName,newText) {
	while (obj = document.getElementById(objName)) {
		with (obj)
			if (document.layers) {document.write(unescape(newText)); document.close();}
			else innerHTML = unescape(newText);
		obj.id = '';
	}
}

function showLayer(obj) {
	if (obj.style)
		obj=obj.style;

	obj.display = 'block'
}

function hideLayer(obj) {
	if (obj.style)
		obj=obj.style;

	obj.display = 'none'
}

function onblurInput(theEl, value){
	if(theEl.value == '')
		theEl.value = value;
}
function onfocusInput(theEl, value){
	if(theEl.value == value)
		theEl.value = '';
}


/*********************************************************/
/* Confirm form submit                                   */
/*********************************************************/

function confirm_submit(o, str)
{
	f = findObj(o);
	if(confirm(str)) f.submit();
}

function confirm_redirect(o, str)
{
	if(confirm(str)) window.location=o;
}


/*********************************************************/
/* Check form                                            */
/*********************************************************/

function formSetRequirements(obj, descr, req, check)
{
	obj = findObj(obj);

	// set properties
	if (obj)
	{
		obj.validateReq = req;
		obj.validateCheck = check;
		obj.validateDescr = descr;
	}
}

function formSetUnique(obj, unique)
{
	obj = findObj(obj);

	// set properties
	if (obj)
		obj.validateUnique = unique;
}

function formUpdate(obj)
{
	if (obj.validateCheck || obj.validateReq)
	{
		err = false;
		val = obj.value;

		if ((val == '' || val == '-' || val == 'http://') && obj.validateReq == true)
			err = true;

		if (obj.validateCheck && err == false && val != '')
		{
			if (obj.validateCheck == 'url' &&
				val.substr(0,7) != 'http://' &&
				val.substr(0,8) != 'https://')
					err = true;

			if (obj.validateCheck == 'email' &&
				(val.indexOf('@') < 1 || val.indexOf('@') == (val.length - 1)))
					err = true;


			if (obj.validateCheck == 'number*' &&
				(isNaN(val) && val != '*' || parseInt(val) < 0))
					err = true;

			if (obj.validateCheck.substr(0,7) == 'number+')
			{
				if (obj.validateCheck.length > 7)
					min = obj.validateCheck.substr(7,obj.validateCheck.length - 7);
				else
					min = 0;

				if (min == 0 && val == '-') val = 0;

				if (isNaN(val) || parseInt(val) < parseInt(min))
					err = true;
			}

			if (obj.validateCheck.substr(0,8) == 'compare:')
			{
				compare = obj.validateCheck.substr(8,obj.validateCheck.length - 8);
				compareobj = findObj(compare);

				if (val != compareobj.value)
					err = true;
			}

			if (obj.validateCheck == 'unique')
			{
				needle = obj.value.toLowerCase();
				haystack = obj.validateUnique.toLowerCase();

				if (haystack.indexOf('|'+needle+'|') > -1)
					err = true;
			}
		}

		// Change class
		//if (err)
			//obj.className='error';
		//else
			//obj.className='flat';

		return (err);
	}
}

selectFirstForm = function() {
	// Select first field
	f.elements[1].select();
	f.elements[1].focus();
}


function formCheck(f)
{
	var noerrors = true;
	var first	 = false;
	var fields   = new Array();

	// Check for errors
	for (var i = 0; i < f.elements.length; i++)
	{
		if (f.elements[i].validateCheck ||
			f.elements[i].validateReq)
		{
			err = formUpdate (obj = f.elements[i]);

			if (err)
			{
				if (first == false) first = i;

				fields.push(f.elements[i].validateDescr);
				noerrors = false;
			}
		}
	}

	if (noerrors == false)
	{
		alert ('The following fields contain errors:' +
			   '\n\n- ' +
			   fields.join('\n- ') +
			   '\n\n' +
			   'Before you can continue you need' +
			   '\n' +
			   'to correct these errors.' +
			   '\n');

		// Select field with first error
		f.elements[first].select();
		f.elements[first].focus();
	}

	return (noerrors);
}