/////////////////////////////////////////////////////////////////////////////////////
/*
**	Filename:	script.js
**	Date:		9/1/2006
**	Author:		Troy Lutton 
**	Purpose:	To provide the scripts for the comp form
**
**	Company:	Troy Lutton, ABN 42 118 890 064
**	Copyright:	Copyright (c) 2002-2006, Troy Lutton <troy.lutton at gmail dot com>, All rights reserved.
**	License:	Software can not be distributed or modified without the explicit consent of the copyright holder.	
*/
/////////////////////////////////////////////////////////////////////////////////////

// validate the com form
function checkCompForm(form)
{
	// prep by removing any highlight classes
	removeHighlights(form);

	with (form)
	{
		try
		{	
			// get and set the filtered numbers
			mobilePhone.value = filterNumber(mobilePhone.value);
			postcode.value = filterNumber(postcode.value);

			// check the fields
			if (name.value == "")
			{
				window.alert("Please enter your name");
				name.className = "hightlightField formField";
				name.focus();
				return false;
			}
			if (email.value == "")
			{
				window.alert("Please enter an email address");
				email.className = "hightlightField formField";
				email.focus();
				return false;
			}		
			// validate the email
			if (!checkEmail(email.value))
			{
				window.alert("Please ensure that you have entered a valid email");
				email.className = "hightlightField formField";
				email.focus();
				return false;
			}					
			// check it
			if (mobilePhone.value == "")
			{
				window.alert("Please enter a mobile phone number");
				mobilePhone.className = "hightlightField formField";
				mobilePhone.focus();
				return false;
			}				
			// validate it
			if (isNaN(parseInt(mobilePhone.value)))
			{
				window.alert("Your mobile phone contains non numerical characters.\r\nPlease make sure that the home phone number has numbers only");
				mobilePhone.className = "hightlightField formField";
				mobilePhone.focus();
				return false;
			}			
			// address details
			if (street.value == "")
			{
				window.alert("Please enter a street address");
				street.className = "hightlightField formField";
				street.focus();
				return false;
			}	
			if (citySuburb.value == "")
			{
				window.alert("Please enter a city / suburb");
				citySuburb.className = "hightlightField formField";
				citySuburb.focus();
				return false;
			}
			if (state.value == "")
			{
				window.alert("Please enter a state");
				state.className = "hightlightField formField";
				state.focus();
				return false;
			}
			if (postcode.value == "" || isNaN(parseInt(postcode.value)) || postcode.length < 4)
			{
				window.alert("Please enter a 4 digit postcode (all numbers)");
				postcode.className = "hightlightField formField";
				postcode.focus();
				return false;
			}
			if (comments.value == "")
			{
				window.alert("Using the text field below, Tell us about your video clip in around 10 words");
				comments.className = "hightlightField formField";
				comments.focus();
				return false;
			}
			/*
			// check captcha
			if (captchaField.value == "" || captchaField.value == "Enter letters here...")
			{
				window.alert("Please ensure that you have entered the verification letters");
				captchaField.className = "hightlightField";
				captchaField.focus();
				return false;
			}
			*/
			if (readTerms.checked == false)
			{
				window.alert("Please indicate that you have read the terms and conditions");
				//name.className = "hightlightField";
				readTerms.focus();
				return false;
			}

			// show progress bar
			//document.getElementById('uploadProgress').style.display = "block";
			
			// all good return
			return true;	
		
		}
		catch (e)
		{
			throwError(e)
		}
	}
}

// remove any highlighting of fields
function removeHighlights(form)
{
	for (i = 0;i < form.length;i++)
	{
		if (form.elements[i].type == "text" || form.elements[i].type == "textarea")//"submit" && form.elements[i].type != "fieldset" && form.elements[i].type != "legend" && form.elements[i].type != "checkbox" && form.elements[i].type != "undefined")
		{				
			form.elements[i].className = "formField";
		}		
	}
}

// filter a number eg: phone
function filterNumber(number)
{
	return number.replace(/[^0-9]/g, '');
}

// validate an email address
function checkEmail(email)
{
	// build the regex filter
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	// test the email
	if (filter.test(email))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// function to refresh the image
function refreshCAPTCHA()
{
	document.captchaImg.src='./includes/captcha.php';
}

// generic error function
function throwError(e)
{
	var msg = e.name + "\r\n" + e.message;
	window.alert(msg);
	return false;
}

// show a popup window
function popWindow(url, width, height, winProps) 
{	
	var left = Math.round((screen.width-width)/2);
    var top = Math.round((screen.height-height)/2);
	var wprops = "toolbar=0, scrollbars=1, location=0, status=1, menubar=0, resizable=1";	
	
	// override the default if properties exist
	if (winProps != null){wprops = winProps;}

	var win = window.open(url, 'thewin', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',' + wprops);
	win.focus();
	//return win;
}
