function FindObj(id)
{
	if (document.getElementById)
	{
		return document.getElementById(id);
	}
	else if (document.all)
	{
		return document.all[id];
	}
	else if (document.layers)
	{
		if (document.layers[id])
			return document.layers[id];
		return document.forms[id];
	}
	return NULL;
}

function DaysMonth(iMonth)
{
	if (iMonth == 2) return 28;
	else if (iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) return 30;
	else return 31;
}

function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(x));
}

function CheckForm()
{
	var formn,n,obj,nm,test,val,p,p1,num,min,max,x,errors='',day,month,year,args=CheckForm.arguments;
	formn = args[0];
	for(n=1;n<args.length-1;n+=2)
	{
		test = args[n+1];
		obj = eval("document." + formn + "." + args[n]);
		if (obj)
		{
			val = obj.value;
			nm = obj.name;
			if (val!="" && val!=undefined)
			{
				if (test.indexOf('isEmail')!=-1)
				{
					if (!checkMail(val)) errors+='- '+nm+' must contain an e-mail address.\n'
					//p=val.indexOf('@');
					//if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
				}
				else if (test.indexOf('isNum')!=-1)
				{
					num = parseFloat(val);
					if (val!=''+num) errors+='- '+nm+' must contain a number.\n';
					if (test.indexOf('inRange')!=-1)
					{
						p=test.indexOf(':');
						min=test.substring(test.indexOf(',')+7,p);max=test.substring(p+1);
						if (num<min || num>max) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
					}
				}
				else if (test.indexOf('isDate')!=-1)
				{
					err=0;
					p=val.indexOf('/');
					if (p > 0)
					{
						day=parseInt(val.substring(0,p));
						p1=val.indexOf('/',p+1);
						if (p1 > p+1)
						{
							month=parseInt(val.substring(p+1,p1));
							year=parseInt(val.substring(p1+1));
							if (month<1 || month>12 || isNaN(month)) {errors+='- '+nm+' month invalid.\n';err++;}
							if (day<1 || (err==0 && day>DaysMonth(month)) || isNaN(day)) {errors+='- '+nm+' day invalid.\n';err++;}
							if (year<2004 || isNaN(year)) {errors+='- '+nm+' year invalid.\n';err++;}
						} else err++;
					} else err++;
					if (err) errors+='- '+nm+' format: dd/mm/yyyy.\n';err++;
				}
				else if (test.indexOf('isSDate')!=-1)
				{
					err=0;
					month=val.substring(0,2);
					year=val.substring(3);
					if (month<0 || month>12) {errors+='- '+nm+' month invalid.\n';err++;}
					if (year<1900) {errors+='- '+nm+' year invalid.\n';err++;}
					if (err) errors+='- '+nm+' format: mm/yyyy.\n';err++;
				}
				else if (test.indexOf('isPostcode')!=-1)
				{
					err=0;
					num = test.indexOf('isPostcode');
					if (test.length > 10+num)
					{
						test = test.substring(num+10,test.length);
						if (eval("document." + formn + "." + test +".value == 'UK'"))
						{
							err=1;
						}
					}
					if (err>0)
					{
						err=0;
						numbers=0;
						for(x=0;x<val.length;x++)
						{
							if (x >= 1 && x <= val.length-2)
							{
								num = parseInt(val.charAt(x));
								if (val.charAt(x)==''+num) {numbers++;}
							}
							else if (val.charAt(x) < 65 || val.charAt(x) > 90) {err++;break;}
						}
						if (numbers < 2) {err++;}
						if (err) errors+='- '+nm+' invalid (check no spaces).\n'
					}
				}
			}
			else if (test.charAt(0) == 'R')
			{
				if (test.indexOf('isOption')!=-1)
				{
					if (eval("document." + formn + "." + nm +".style.visibility == 'visible'"))
					{
						errors += '- '+nm+' is required.\n'
					}
				}
				else if (test.indexOf('isRadio')!=-1)
				{
					//Search form for all controls with matching name to find a radio that is selected
					num = eval("document." + formn + ".elements.length");
					obj = eval("document." + formn + ".elements");
					err = 1;
					for(p=0;p<num;p++)
					{
						if (obj[p].name == args[n] && obj[p].checked)
						{
							err = 0;
						}
					}
					if (err) errors += '- '+args[n]+' is required.\n'
				}
				else
				{
					errors += '- '+nm+' is required.\n'
				}
			}
		}
	}
	if (errors) alert('The following error(s) occurred:\n'+errors);
	return (errors == '');
}
function UpdateLimit(object,n)
{
	var Value;
	if (obj = FindObj(object+"Limit"))
	{
		//Get length of the current text
		Value = eval("document.OrderForm."+object+".value.length");

		//Update limit text
		obj.innerHTML = "(Limit "+ (n - Value) + ")";
	}
}
function Limit(object,n,e)
{
	var keyCode,Value;

	//Get key code
	if (e.keyCode) keyCode = e.keyCode;
	else keyCode = e.which;

	//If key pressed is BackSpace, Delete, Cursor Left/Right/Up/Down, Home, End then return true
	if (keyCode == 8 || keyCode == 46 || (keyCode >= 37 && keyCode <= 40) || keyCode == 35 || keyCode == 36) 
	{
		setTimeout("UpdateLimit('"+object+"',"+n+");",5);
		return true;
	}

	//Get length of the current text
	Value = eval("document.OrderForm."+object+".value.length");

	//If Current text exceeds limit return
	if (Value >= n) return false;
	if (obj = FindObj(object+"Limit"))
	{
		obj.innerHTML = "(Limit "+ (n - Value - 1) + ")";
	}
	
	//Else return true
	//alert(e.button);
	return true;
}
function UpdateOther(list,ctrl)
{
	if (list == "Where")
	{
		document.OrderForm.WhereInd.value = document.OrderForm.Where.selectedIndex;
	}
	if (eval("document.OrderForm."+list+".value") == "Other")
	{
		eval("document.OrderForm."+ctrl+".style.visibility = 'visible'");
	}
	else
	{
		eval("document.OrderForm."+ctrl+".style.visibility = 'hidden'");
	}
}
function UpdateList(onload)
{
	document.OrderForm.Where.options.length = 0;
	if (document.OrderForm.Category.value == "Show")
	{
		document.OrderForm.Where.options[0] = new Option('Please select an option','');
		document.OrderForm.Where.options[1] = new Option('NEC','NEC');
		document.OrderForm.Where.options[2] = new Option('Don Valley','Don Valley');
		document.OrderForm.Where.options[3] = new Option('Edgbaston','Edgbaston');
		document.OrderForm.Where.options[4] = new Option('Derby University','Derby University');
		document.OrderForm.Where.options[5] = new Option('Aylestone Leisure Centre Leicester','Aylestone Leisure Centre Leicester');
		document.OrderForm.Where.options[6] = new Option('Nottingham','Nottingham');
		document.OrderForm.Where.options[7] = new Option('Doncaster Dome','Doncaster Dome');
		document.OrderForm.Where.options[8] = new Option('Artstamps Newbury','Artstamps Newbury');
		document.OrderForm.Where.options[9] = new Option('Gmex Manchester','Gmex Manchester');
		document.OrderForm.Where.options[10] = new Option('Harrogate','Harrogate');
		document.OrderForm.Where.options[11] = new Option('Other','Other');
    	}
	else if (document.OrderForm.Category.value == "Internet")
	{
		document.OrderForm.Where.options[0] = new Option('Please select an option','');
		document.OrderForm.Where.options[1] = new Option('www.gilesoriginals.co.uk','www.gilesoriginals.co.uk');
		document.OrderForm.Where.options[2] = new Option('www.papercraftshows.com','www.papercraftshows.com');
		document.OrderForm.Where.options[3] = new Option('www.cc4c.co.uk (Craft4Crafters)','www.cc4c.co.uk (Craft4Crafters)');
		document.OrderForm.Where.options[4] = new Option('www.iseekhere.com','www.iseekhere.com');
		document.OrderForm.Where.options[5] = new Option('www.google.co.uk','www.google.co.uk');
		document.OrderForm.Where.options[6] = new Option('www.yahoo.co.uk','www.yahoo.co.uk');
		document.OrderForm.Where.options[7] = new Option('Other','Other');
    }
	else if (document.OrderForm.Category.value == "Magazine")
	{
		document.OrderForm.Where.options[0] = new Option('Please select an option','');
		document.OrderForm.Where.options[1] = new Option('Quick Cards Made Easy','Quick Cards Made Easy');
		document.OrderForm.Where.options[2] = new Option('Cardmaking and Papercraft','Cardmaking and Papercraft');
		document.OrderForm.Where.options[3] = new Option('Beautiful Cards','Beautiful Cards');
		document.OrderForm.Where.options[4] = new Option('Other','Other');
    }
	else if (document.OrderForm.Category.value == "Ebay")
	{
		document.OrderForm.Where.options[0] = new Option('Ebay','Ebay');
    }
	else if (document.OrderForm.Category.value == "Returning Customer")
	{
		document.OrderForm.Where.options[0] = new Option('Returning Customer','Returning Customer');
    }
	else if (document.OrderForm.Category.value == "Other")
	{
		document.OrderForm.Where.options[0] = new Option('Other','Other');
    }
	else
	{
		document.OrderForm.Where.options[0] = new Option('Please select an option','');
	}
	if (onload) document.OrderForm.Where.selectedIndex = document.OrderForm.WhereInd.value;
	UpdateOther('Where','Other');
}