//generic validation system
//below is an example of the validation array that gets passed
/*

validationRules = Array(
	Array('companyName','Company Name','required',''),
	Array('ABN','ABN','required',''),
	Array('businessOwnersText','Business Owner','required',''),
	Array('phoneNumber','Phone Number','required',''),
	Array('tradeAddress1','Trading Address','required',''),
	Array('tradeCity','Trading Suburb','required',''),
	Array('tradeState','Trading State','required',''),
	Array('tradePostCode','Trading Post Code','required',''),
	Array('tradeCountry','Trading Country','required',''),
	Array('accountingBasis','Accounting Basis','required',''),
	Array('emailAddress','Email Address','compareRequired',Array('emailAddressConfirm'))
	)

*/

function validateForm(validationArray,additionalMessagesArray)
{
	var alerts = new Array();
	var tmpCount = 0;
	var submitOK = true;

	if(additionalMessagesArray)
	{
		for(kk=0;kk<additionalMessagesArray.length;kk++)
		{
			alerts[tmpCount]=additionalMessagesArray[kk];
			tmpCount++;
			submitOK=false;
		}
	}

	for (i=0; i<validationArray.length; i++)
	{
		tmpFieldName=validationArray[i][0];
		tmpFieldDisplayName=validationArray[i][1];
		tmpValidationType=validationArray[i][2];
		tmpValidationArgs=validationArray[i][3];
		tmpFieldValue = document.getElementsByName(tmpFieldName)[0].value;
		if((tmpValidationType=="" || tmpValidationType=="required") && trim(tmpFieldValue)=="")
		{
			alerts[tmpCount] = tmpFieldDisplayName + " is a required field";
			tmpCount++;
			submitOK = false;
		}
		else if(tmpValidationType=="emailAddressRequired")
		{
			if(trim(tmpFieldValue)=="")
			{
				alerts[tmpCount] = tmpFieldDisplayName + " is a required field";
				tmpCount++;
				submitOK = false;
			}
			else
			{
				var emailRegExp  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				
				if(!emailRegExp.test(tmpFieldValue))
				{
					alerts[tmpCount] = tmpFieldDisplayName + " is not a valid email address";
					tmpCount++;
					submitOK = false;
				}
			}
		}
		else if(tmpValidationType=="emailAddressNotRequired")
		{
			if(trim(tmpFieldValue)!="")
			{
				var emailRegExp  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				
				if(!emailRegExp.test(tmpFieldValue))
				{
					alerts[tmpCount] = tmpFieldDisplayName + " is not a valid email address";
					tmpCount++;
					submitOK = false;
				}
			}
		}
		else if(tmpValidationType=="numberRequired")
		{
			if(trim(tmpFieldValue)=="")
			{
				alerts[tmpCount] = tmpFieldDisplayName + " is a required field";
				tmpCount++;
				submitOK = false;
			}
			else
			{				
				if(tmpValidationArgs=='')
				{
					//by leaving blank we let inNumeric function decide default
					strValidChars="";
				}
				else
				{
					strValidChars=tmpValidationArgs;
				}
				if(isNumeric(tmpFieldValue,strValidChars)==false)
				{
					alerts[tmpCount] = tmpFieldDisplayName + " must be numeric";
					tmpCount++;
					submitOK = false;
				}
			}
		}
		else if(tmpValidationType=="numberNotRequired")
		{
			if(tmpFieldValue!="")
			{
				
				if(tmpValidationArgs=='')
				{
					//by leaving blank we let inNumeric function decide default
					strValidChars="";
				}
				else
				{
					strValidChars=tmpValidationArgs;
				}
				if(isNumeric(tmpFieldValue,strValidChars)==false)
				{
					alerts[tmpCount] = tmpFieldDisplayName + " must be numeric";
					tmpCount++;
					submitOK = false;
				}
			}
		}
		else if(tmpValidationType=="compareRequired")
		{
			tmpCompareFieldValue = document.getElementsByName(tmpValidationArgs[0])[0].value;
			if(trim(tmpFieldValue)=="" && trim(tmpCompareFieldValue)=="")
			{
				alerts[tmpCount] = tmpFieldDisplayName + " is a required field";
				tmpCount++;
				submitOK = false;
			}
			else if(tmpFieldValue != tmpCompareFieldValue)
			{
				alerts[tmpCount] = tmpFieldDisplayName + " field does not match the Confirm " + tmpFieldDisplayName + " field";
				tmpCount++;
				submitOK = false;
			}
			
		}
		else if(tmpValidationType=="compareNotRequired")
		{
			tmpCompareFieldValue = document.getElementsByName(tmpValidationArgs[0])[0].value;
			if(tmpFieldValue != tmpCompareFieldValue)
			{
				alerts[tmpCount] = tmpFieldDisplayName + " field does not match the Confirm " + tmpFieldDisplayName + " field";
				tmpCount++;
				submitOK = false;
			}
		}
		else if(tmpValidationType=="radioRequired")
		{

			var radio_selected = false;

			// Loop from zero to the one minus the number of radio button selections
			for (radiocounter = 0; radiocounter < document.getElementsByName(tmpFieldName).length; radiocounter++)
			{
			
				if (document.getElementsByName(tmpFieldName)[radiocounter].checked)
				{
					radio_selected = true;
				}
			}

			if(radio_selected==false)
			{
				alerts[tmpCount] = tmpFieldDisplayName + " is a required field.";
				tmpCount++;
				submitOK = false;
			}
		}
		else if(tmpValidationType=="checkboxRequired")
		{
			if(document.getElementsByName(tmpFieldName)[0].checked==false)
			{
				alerts[tmpCount] = tmpFieldDisplayName + " must be ticked";
				tmpCount++;
				submitOK = false;
			}
		}
	}
	validationMessage(alerts);
	return submitOK;
}



//Outputs validation popup message in one sentence
function validationMessage(alerts)
{

	if(alerts.length==0)
	{
		return;
	}
	var alertstring = "Please correct the following errors:\n\n";
	
	for (i=0; i<alerts.length; i++)
	{
		alertstring += "- " + alerts[i] + "\n"
	}
	
	alert(alertstring);

}

function checkDomainPrefixValidity(domainPrefix)
{	
	var re = /^[a-z0-9][a-z0-9\-]*$/i;
	var ret=true;

	if(domainPrefix.length<1 || re.test(domainPrefix)==false)
	{
		ret=false;
	}

	return ret;
}



function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

//  check for valid numeric strings	
function isNumeric(strString,strValidChars)
{
	//default to decimal (positive or negative) if strValidChars is empty
	if(strValidChars=="")
	{
		strValidChars = "0123456789.-";
	}
	
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (j = 0; j < strString.length && blnResult == true; j++)
	{
		strChar = strString.charAt(j);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function removeSpaces(string)
{
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(k=0; k < splitstring.length; k++)
	{
		tstring += splitstring[k];
	}
	return tstring;
}

function isInternetExplorer()
{
	var detect = navigator.userAgent.toLowerCase();
	pos = detect.indexOf('msie') + 1;
	return pos;
}

function hideTableRows(elementID)
{
	document.getElementById(elementID).style.display='none';
}

function showTableRows(elementID)
{
	if(isInternetExplorer())
	{
		document.getElementById(elementID).style.display='inline';
	}
	else
	{
		document.getElementById(elementID).style.display='table-row-group';
	}
}

function toggleElement(elementID)
{
	if(document.getElementById(elementID).style.display=='none')
	{
		document.getElementById(elementID).style.display='inline';
	}
	else
	{
		document.getElementById(elementID).style.display='none';
	}
}

function hideElement(elementID)
{
	document.getElementById(elementID).style.display='none';
}

function showElement(elementID)
{
	document.getElementById(elementID).style.display='';
}


function orderNowCheckboxChanged()
{
	if(document.getElementById('order_now').checked)
	{
		showTableRows('credit_card_section');
	}
	else
	{
		hideTableRows('credit_card_section');
	}
}

//function to add menu items to adxmenu after rendering using Javascript.
//Originally made to make links that will not be followed by Google.

//set target to '_blank' to open in new window or leave as empty string to open in same window
function addCustomJSMenuItem(menu_ul_name, menu_position, menu_item_name, menu_item_href, menu_item_css_class, menu_item_target)
{
	//this is the UL we are working on (could be the main list of a submenu list)
	var menu_ul=document.getElementById(menu_ul_name);

	var menu_ul_children=menu_ul.childNodes;

	var menu_lis=new Array();

	//loop through child nodes and make array of the LI elements only
	for(var ii=0;ii<menu_ul_children.length;ii++)
	{
		if(menu_ul_children[ii].nodeName=='LI')
		{
			menu_lis.push(menu_ul_children[ii]);
		}
	}

	//here we create the new LI element and the A subelement
	var new_li = document.createElement('li');

	if(menu_item_css_class!='')
	{
		new_li.className=menu_item_css_class;
	}

	//here we check if menu_item_href is set
	//if not, we skip the a tag
	if(menu_item_href!='')
	{
		var new_a = document.createElement('a');
		new_a.href=menu_item_href;

		//here check if this should open in new window
		if(menu_item_target=='_blank')
		{
			new_a.target=menu_item_target;
		}


		new_a.appendChild(document.createTextNode(menu_item_name));
		new_li.appendChild(new_a);
	}
	else
	{
		new_li.appendChild(document.createTextNode(menu_item_name));
	}

	//store number of existing items
	var num_existing_lis=menu_lis.length;

	//if menu_position is less than or equal to the current number of items, we use the "insertBefore" method. If not we use the the appendChild method.
	if(menu_position<=num_existing_lis)
	{
		//this is the reference node that we will insert the new element after
		var ref_node=menu_lis[menu_position-1];
		menu_ul.insertBefore(new_li, ref_node);
	}
	else
	{
		menu_ul.appendChild(new_li);
	}
}