// JavaScript Document

var hex_color_error = "#FF0000";
var hex_color_goood = "#000000";

//this returns the label reference of the form element
function GetLabel(e) {
	switch(e)	
	{	
		default:
			return document.getElementById('lbl' + e);		
	}	
}

//this returns the element (object)
function GEId(e){	
	switch(e)
	{
		case 'gender':
			return oform.gender;
			break;
		case 'emailformat':
			return oform.emailformat;
			break;
		default:
			return document.getElementById(e);		
	}	
}

//this function returns a form's element (object)
function GEName(e) {	
	var element = document.getElementsByName(e);
	return element.length > 1 ? element : element[0];	
}

//limits the textarea to 'length' characters
function checkTextarea(textarea, length) {
	textarea.value = textarea.value.substring(0,length);
}

//validate subscription forms
function validate_form(form){
	var error=false;		
	var highlight = false;
	var v_purchase;
	var first_error = '';
	var add_error_msg = '';
	
	//save copy of form globally
	oform = form;	
	
	//loop through each field to be validated
	for (i=0; i<input.length; i++)
	{
		switch(input[i])
		{			
			default:
				if (!validate_field(input[i],GEId(input[i]))){	
					error=true;
					highlight = true;
				}
		}
				
		//color code error
		GetLabel(input[i]).style.color = highlight ? hex_color_error : hex_color_goood;				
		highlight = false;
		first_error = (first_error == '' && error) ? input[i] : first_error;
		
	}
		
	if (error == true) {
		GEId('error').innerHTML = 'All fields marked with an * are required. ' + add_error_msg;
		GEId('error').className = "error";
		GEId('error').scrollIntoView(true);
		return false;
	} else {
		GEId('error').innerHTML = '';
		GEId('error').className = '';
		return true;
	}
}

//validates a field based on a certain definition
function validate_field(name, input)
{	
	var returnvalue;
	
	//check wheather the input is not a radio or checkbox (group or not) or its a select element
	if((input.type != 'checkbox' && input.type != 'radio' && input.length == null) || input.tagName == 'SELECT') {
		//this indicates its not a group of inputs
		
		//special condition testing
		switch(name)
		{
		case 'email': case 'confirmemail':	
		  return input.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/);
		  break;
		/*
		case 'firstname':
			//accept alpha characters only
		  return input.value.match(/^([a-zA-Z\-])+$/) && input.value != 'first name';	
		  break;
		case 'lastname': case 'surname':
		  //accept alpha characters only	
		  return input.value.match(/^([a-zA-Z\-\'])+$/) && input.value != 'last name';
		  break;	
		case 'address':
		  return input.value.match(/^[\w\s\/\.\-]+$/);
		  break;
		case 'suburb':
		  return input.value.match(/^([a-zA-Z\s])+$/);	
		  break;		
		*/ 
		case 'confirmpassword':
		  return input.value.length > 0 && (oform.password.value == input.value);	
		case 'postcode':
		  return input.value.match(/^\d{4}/);
		  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;
		}
	}		
}


//validate group of fields together
//1st parameter = condition type (and/or)
//2nd....Nth parameter = list of field to be validated together
function validate_condition() 
{
	if(validate_condition.arguments.length > 0) {
		var element_object, next_object;
		var condition_type = validate_condition.arguments[0];		
		var flag_and = true;
		var flag_or	 = false;
		var flag_equal = true;
		var flag_notequal = true;
		for(var i = 1; i < validate_condition.arguments.length; i++) {
			element_object = validate_condition.arguments[i];				
			switch(condition_type) {
				case 'and':
					flag_and = flag_and && validate_field(element_object,GEName(element_object));
					break;
				case 'or':
					flag_or = flag_or || validate_field(element_object,GEName(element_object));
					break;
				case 'equal':
					for (var j = 1; j < validate_condition.arguments.length; j++) {
						next_object = validate_condition.arguments[j];						
						if(i != j) {
							flag_equal = (flag_equal && validate_field(element_object,GEName(element_object)) && (GEName(element_object).value == GEName(next_object).value));
						}
					}
					break;
				case 'notequal':
					for (var j = 1; j < validate_condition.arguments.length; j++) {
						next_object = validate_condition.arguments[j];						
						if(i != j) {
							flag_notequal = (flag_notequal && validate_field(element_object,GEName(element_object)) && (GEName(element_object).value != GEName(next_object).value));
						}
					}
					break;
			}
		}
		
		//return flag
		switch(condition_type) {
			case 'and':
				return flag_and;
				break;
			case 'or':
				return flag_or;
				break;
			case 'equal':
				return flag_equal;
				break;
			case 'notequal':
				return flag_notequal;
				break;
			default:
				return false;			
		}
	} else {
		return false;
	}
}