// check if a form has been altered ( MAKE SURE RETURNING IF ALTERED CORRECTLY!!!!)
function form_changed(formname)
{
	var altered = false;
	var output = '';
	for (var i = 0, j = formname.elements.length; i < j; i++)
	{
		inputType = formname.elements[i].type;
		if (inputType == 'checkbox' || inputType == 'radio')
		{
			if (formname.elements[i].checked && formname.elements[i].defaultChecked)
			{
				output += formname.elements[i].name + ' is still checked' + '\n';
				altered = false;
			}
		}
		if (inputType == 'hidden' || inputType == 'password' || inputType == 'text' || inputType == 'textarea')
		{
			if (formname.elements[i].value == formname.elements[i].defaultValue)
			{
				output += formname.elements[i].name + ' still equals "' + formname.elements[i].defaultValue + '"' + '\n';
				altered = false;
			}
		}
		if (inputType == 'select-one' || inputType == 'select-multiple')
		{
			for (var k = 0, l = formname.elements[i].options.length; k < l; k++)
			{
				if (formname.elements[i].options[k].selected && formname.elements[i].options[k].defaultSelected)
				{
					output += formname.elements[i].name + ' option ' + k + ' is still selected' + '\n';
					altered = false;
				}
			}
		}
	}

	// enable this line if you wish to see output of items not changed
	//if (output != '') alert(output);
	return altered;
}

function yesno() // confirmation popup
{
	args = yesno.arguments; // get arguements
	question = ((args[0]) ? args[0] : 'Are you sure you wish to remove this record?');
	if (confirm(question)) return true;
}

// disable a range of elements
function disableelements(formname, setto)
{
    frm_name = document.forms[formname].elements;
    for (x = 0; x < frm_name.length; x++)
    {
		if (frm_name[x].name && frm_name[x].name.substring(0,4) == 'com_') frm_name[x].disabled = ((!setto) ? true : false);
	}
}

// check boxes in the grid (all if checkall var is = 1 else check to see if all are checked and check header one if so)
// also if you use startCheck and countCheck only the items within those boundaries will be checked
function check_grid(status, formname, singleidname, checkall, xClassName, startCheck, countCheck)
{
	if (!xClassName) xClassName = 'grid_item'; // default classname to change to when selecting
	frm_name = document.forms[formname].elements;

	startLoop = (startCheck) ? startCheck : 1;
	endLoop = (countCheck) ? countCheck : frm_name.length; // if using countCheck then end after looped to that amount

	for (i = startLoop, x = startLoop; x < frm_name.length; x++)
	{
		// check if any boxes are checked ( for with selected)
		if (status == -1) { if (frm_name[x].checked) return true; }
		else if (frm_name[x].type == 'checkbox')
		{
			if (checkall)
			{
				frm_name[x].checked = status;
				// colour background of row if checked
				if (document.getElementById(formname+i)) document.getElementById(formname+i).className = (frm_name[x].checked) ? xClassName+'_sel' : xClassName;
			}
			else
			{
				if (!frm_name[x].checked)
				{
					frm_name[0].checked = false;
					break;
				}
				else frm_name[0].checked = true;
			}
			i++;
		}
	}
	// colour item if checked
	if (!checkall) document.getElementById(formname+singleidname).className = (status.checked) ? xClassName+'_sel' : xClassName;
}

// select first object in a form that isnt hidden or hidden
function focusFirst(formname)
{
	frm_name = document.forms[formname];
	if (frm_name.elements.length > 0)
	{
		for (x = 0; x <= frm_name.length; x++) if (frm_name.elements[x].type != 'hidden' && frm_name.elements[x].disabled == false)
		{
			frm_name.elements[x].focus();
			break;
		}
    }
}

// checks to make sure user isnt inputting an incorrect page number for pages
function checkPageInput(formname, currentPage, totalPages)
{
	pg_number = formname.pagenum; // name of input containing page number

	if (pg_number.value != currentPage && pg_number.value <= totalPages && pg_number.value > 0) return true;
    else return false;
}

// this will check and remove items from the other dropdowns to stop from duplicate selection
function noDuplicateSelect(senderID, selectCount)
{
	xSenderID = senderID.id; // get id of select that is active
	xValue = senderID.selectedIndex; // value to remove from other selects
	xGroup = xSenderID.substr(0, xSenderID.lastIndexOf('_')); // get name for group of selects to be working with

	for (x = 1; x <= selectCount; x++)
	{
		if (xValue)
		{
			xSelect = document.getElementById(xGroup+'_'+x); // the select that will have the value removed
			if (xSenderID != xGroup+'_'+x && xValue == xSelect.selectedIndex) { senderID.selectedIndex = 0; alert('Category already selected'); }
		}
	}
}

// generate random number
function generate_key(xLength, xType)
{
	// start with a blank password
	var xPassword = '';
	var xChar = '';
	// define possible characters
	if (xType == 'alpha') xPossible = 'abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ';
	else if (xType == 'num') xPossible = '0123456789';
	else xPossible = '0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ';

	// add random characters to $password until $length is reached
	for (i = 1; i <= xLength; i++)
	{
		// pick a random character from the possible ones
		xChar = xPossible.substr(Math.floor(Math.random()*xPossible.length-1), 1);
		// we don't want this character if it's already in the password
		xPassword += xChar;
	}
	return xPassword;
}

var toform_edited = 0; // if the shop form has been edited then dont run auto_text
function auto_text(fromid, toid)
{
	// input text into shop fields according to users fields
	if (!toform_edited && !toid.defaultValue) toid.value = fromid.value;
}