// JavaScript Document

// Left Fold Out Menu

startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

window.onload=startList;	


// Report table mouse overs

function reportTable(table,status)
	{
	if(status==1)
		{
		table.style.background='#efefef';
	}
	else
		{
		table.style.background='#FFFFFF';
	}
}



// Form Validation

function validateForm()
	{
	// Usage - array 0 is the form name, then array 1+ the name of required fields
	
	// Get the name of fields that are required
	var argv = validateForm.arguments;
  	var argc = argv.length;
  	var form_name = argv[0];
	var errors="";
	
  	for (var i = 1; i < argc; i++) 
  		{
		if (document.forms[form_name].elements[argv[i]].value == "") { // Check to see whether the name field is blank
		
		if(argv[i].substr(argv[i].length-11)=="COMPANYNAME") { argv[i]="Please enter the company name."; }
		if(argv[i].substr(argv[i].length-8)=="ADDRESS1") { argv[i]="Please enter the address line 1."; }
		if(argv[i].substr(argv[i].length-4)=="CITY") { argv[i]="Please enter the town/city."; }
		if(argv[i].substr(argv[i].length-8)=="POSTCODE") { argv[i]="Please enter the postcode."; }
		if(argv[i].substr(argv[i].length-13)=="REL_countryID") { argv[i]="Please select the country."; }
		if(argv[i].substr(argv[i].length-9)=="TELEPHONE") { argv[i]="Please enter the telephone number."; }
		if(argv[i].substr(argv[i].length-7)=="PROFILE") { argv[i]="Please enter a profile name."; }
		if(argv[i].substr(argv[i].length-9)=="companyID") { argv[i]="Please select a company."; }
		if(argv[i].substr(argv[i].length-9)=="jobsREL_companyID") { argv[i]="Please select a company or internal office."; }
		if(argv[i].substr(argv[i].length-7)=="SUBJECT") { argv[i]="Please enter a subject."; }
		if(argv[i].substr(argv[i].length-7)=="MESSAGE") { argv[i]="Please enter a message."; }
		if(argv[i]=="departmentsNAME") { argv[i]="Please enter a department name."; }
		if(argv[i]=="usersUSERNAME") { argv[i]="Please enter a username."; }
		if(argv[i]=="usersPASSWORD") { argv[i]="Please enter a password."; }
		if(argv[i]=="usersNAME") { argv[i]="Please enter a name."; }
		if(argv[i]=="usersEMAIL") { argv[i]="Please enter an email address."; }		
		if(argv[i]=="companyNAME") { argv[i]="Please enter a company name."; }
		if(argv[i]=="miscNAME") { argv[i]="Please enter a misc. value."; }
		if(argv[i]=="organisationsNAME") { argv[i]="Please enter an organisation name."; }
		if(argv[i]=="jobtitleNAME") { argv[i]="Please enter a job title."; }
		if(argv[i]=="contactsNAME") { argv[i]="Please enter a name."; }
		if(argv[i]=="contactsUSERNAME") { argv[i]="Please enter a username."; }
		if(argv[i]=="contactsPASSWORD") { argv[i]="Please enter a password."; }
		if(argv[i]=="company_database") { argv[i]="Please select a CSV file."; }
		if(argv[i]=="contacts_database") { argv[i]="Please select a CSV file."; }
		if(argv[i]=="field1") { argv[i]="Please select a value for Field 1."; }
		if(argv[i]=="jobsNAME") { argv[i]="Please enter a Contract title."; }
		if(argv[i]=="categoryNAME") { argv[i]="Please enter a category name."; }
		if(argv[i]=="foldersNAME") { argv[i]="Please enter a folder name."; }
		if(argv[i]=="filesUPLOAD") { argv[i]="Please select a file to upload."; }
		if(argv[i]=="filesNAME") { argv[i]="Please enter a name for this file."; }
		if(argv[i]=="message") { argv[i]="Please enter a message for the SMS."; }

		errors=errors+"\n\n- "+argv[i]+" ";
		}
   	}
   	
   	// Extra validation for the add contact form
   	if(form_name=="change_password")
   		{
   		if(document.getElementById('password1').value!=document.getElementById('password2').value)
   			{
   			errors=errors+"\n\n- Your password confirmation is different ";
   		}
   	}

	if(errors)
		{
		alert("\nSorry you have not completed all of the required fields."+errors+"\n\n"); return(false);
	}
	else
		{	
		// Disable the submit button to stop people clicking twice
		document.forms[form_name].elements['Submit'].disabled = true;
		document.forms[form_name].elements['Submit'].value = 'Please Wait...';
		document.forms[form_name].submit();
	}
}


// Functin to check that it is a number

function IsNumeric(sText)
	{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
   		{ 
      		Char = sText.charAt(i); 
      		
      		if (ValidChars.indexOf(Char) == -1) 
         		{
         		IsNumber = false;
         	}
      	}
      	
	return IsNumber;
}


// Function to correctly display a price (with 0 padding etc)

function format_price(amount)
	{
	amount=Math.round(amount*100)/100;
	amount -= 0;
	return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}



// Function to show create new group box

function showNewGroup()
	{
	if(document.forms['form_add_contact'].elements['group'].value=="new")
		{
		document.getElementById('NewGroupRow').style.display="";
	}
	else
		{
		document.getElementById('NewGroupRow').style.display="none";
	}
}


// Function to show custom repeat boxes

function showCustomRepeat()
	{
	if(document.forms['form_create_campaign'].elements['repeat'].value!="")
		{
		document.getElementById('CustomRepeatRow1').style.display="";
		document.getElementById('CustomRepeatRow2').style.display="";
	}
	else
		{
		document.getElementById('CustomRepeatRow1').style.display="none";
		document.getElementById('CustomRepeatRow2').style.display="none";
	}
}

// Function to show custom delay boxes

function showCustomDelay()
	{
	if(document.forms['form_create_campaign'].elements['delivery'].value=="custom")
		{
		document.getElementById('CustomDelayRow1').style.display="";
		document.getElementById('CustomDelayRow2').style.display="";
	}
	else
		{
		document.getElementById('CustomDelayRow1').style.display="none";
		document.getElementById('CustomDelayRow2').style.display="none";
	}
}
