// JavaScript Document

//this returns the element (object)
function GEId(e){
	
	switch(e)
	{		
		default:
			return document.getElementById(e);		
	}
	
}

//this returns the label reference of the form element
function GetLabel(e) {
	switch(e)
	{
	case 'fastlane': case 'roadmail': case 'special':
	  return 'lbloffers';
	  break;
	case 'p_fastlane': case 'p_roadmail': case 'p_special':
	  return 'lbloffers';
	  break;
	default:
	  return 'lbl' + e;
	}
}

function get_field_error_msg(field) {
	switch(field)
	{
		case 'firstname': case 'lastname':
			break;
		case 'postcode':
			return 'Postcode needs to be a 4 digit number'
			break;
		case 'tel':
			return 'Contact Number needs to be 8 to 10 characters'
			break;
		default:
			return null;
	}
}

//validates a field based on a certain definition
function validate_field(name, input)
{	
	var returnvalue;
		
	if((input.type != 'checkbox' && input.type != 'radio') || input.tagName == 'SELECT') {
		//this indicates its not a group of inputs
		switch(name)
		{
		case 'email': case 'confirmemail':	
		  return input.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
		  break;
		case 'firstname':
			//accept alpha characters only
		  return input.value.match(/^([a-zA-Z\-])+$/);	
		  break;
		case 'lastname':
		  //accept alpha characters only	
		  return input.value.match(/^([a-zA-Z\-\'])+$/);
		  break;	
		case 'address':
		  return input.value.match(/^[\w\s\/\.]+$/);
		  break;
		case 'suburb':
		  return input.value.match(/^([a-zA-Z\s])+$/);	
		  break;		
		case 'postcode':
		  return input.value.match(/^\d{4}/);
		  break;
		case 'tel':
		  return input.value.match(/^\d{8,10}/);
		  break;		
		default:
		   return input.value.length > 0;
		}
	} else {
		//this indicates that there is a group of inputs under the
		//the same (i.e. radio or checkboxes)
		//default value	
		returnvalue = false;	
		
		if(input.checked != null) {
			return input.checked;
		} else {	
			for(var index = 0; index < input.length; index++)
			{
				if(input[index].checked) {
					returnvalue = true;
				}
			}		    
			
			return returnvalue;
		}
	}		
}

function validate_form(form){
	var error=false;		
	var highlight = false;
	var v_purchase;
	var first_error = '';
	var add_error_msg = '\n\n';
	
	//save copy of form globally
	oform = form;
	
	//region of interest - combine into one field
	if(webpage != 'login') {
		var input_regionofinterest		= form.regionofinterest;
		input_regionofinterest.value	= concatenate_collection(form.P_regionofinterest);
	}
	
	//loop through each field to be validated
	for (i=0; i<input.length; i++)
	{
		switch(input[i])
		{
			//any special conditions
			case 'p_fastlane': case 'p_roadmail': case 'p_special':
				form.fastlane.value = form.p_fastlane.checked ? 1 : 0;
				form.roadmail.value = form.p_roadmail.checked ? 1 : 0;
				form.special.value = form.p_special.checked ? 1 : 0;
							
				if(!validate_field(input[i],form.p_fastlane) && !validate_field(input[i],form.p_roadmail) && !validate_field(input[i],form.p_special)) {
					error=true;
					highlight = true;
				}
				break;
			case 'fastlane': case 'roadmail': case 'special':
				if(!validate_field(input[i],form.fastlane) && !validate_field(input[i],form.roadmail) && !validate_field(input[i],form.special)) {
					error=true;
					highlight = true;
				}
				break;				
			default:
				if (!validate_field(input[i],GEId(input[i]))){	
					error=true;
					highlight = true;
				}
		}
		
		//put in place any addition error messages
		if(highlight && get_field_error_msg(input[i]) != null) {
			add_error_msg = add_error_msg + get_field_error_msg(input[i]) + '\n\n';
		}
		
		//color code error
		GEId(GetLabel(input[i])).style.color = highlight ? "#ff0000" : "#000000";				
		highlight = false;
		first_error = (first_error == '' && error) ? GetLabel(input[i]) : first_error;
		
	}
	if (error == true) {
		alert('Please complete all mandatory fields' + add_error_msg);
		GEId(first_error).scrollIntoView(true);
		return false;
	} else
		return true;
}
