//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)
{
	var alerts = new Array();
	var tmpCount = 0;
	var submitOK = true;
	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=="numberNotRequired")
		{
			if(tmpFieldValue!="")
			{
				
				if(tmpValidationArgs[0]=='')
				{
					strValidChars=""
				}
				else
				{
					strValidChars=tmpValidationArgs[0];
				}
				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")
		{
			alert(tmpFieldDisplayName);
			tmpCompareFieldValue = document.getElementsByName(tmpValidationArgs[0])[0].value;
			if(tmpFieldValue != tmpCompareFieldValue)
			{
				alerts[tmpCount] = tmpFieldDisplayName + " field does not match the Confirm " + tmpFieldDisplayName + " field";
				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 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 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)
{
	if(isInternetExplorer())
	{
		document.getElementById(elementID).style.display='none';
	}
	else
	{
		document.getElementById(elementID).style.visibility='hidden';
	}
}

function showElement(elementID)
{
	if(isInternetExplorer())
	{
		document.getElementById(elementID).style.display='inline';
	}
	else
	{
		document.getElementById(elementID).style.visibility='visible';
	}
}

function pageTypeChanged()
{
	if(document.getElementsByName('page_type')[1].checked==true)
	{
		hideElement('content_element');
		showElement('redirect_to_element');
	}
	else
	{
		showElement('content_element');
		hideElement('redirect_to_element');
	}
}

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

function watermarkCheckboxChanged()
{
	if(document.getElementById('use_watermark').checked)
	{
		showTableRows('watermark_options');
	}
	else
	{
		hideTableRows('watermark_options');
	}
}

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';
	}
}