// Highlights the current page's link
menu_items = new Object();

var menu_count = 0;

function build_menu(div_id) {
			menudiv = document.getElementById(div_id);
			menudiv.innerHTML = '';
			
			for (i=0; i< menu_count; i++) {
				menudiv.innerHTML += '<div id="' + menu_items[i].name + "_head" + '" class="topmenuh" onclick="changebody(' + "'" + menu_items[i].name  + "'" + ')"><a href="?' + menu_items[i].name + '" onclick="return false;">' + menu_items[i].description + '</a></div>';	
			}
		}
		
// Highlights the current page's link
function currentmenu(name) {
		build_menu("topmenu");

		changetotopmenucurrent = document.getElementById(name + '_head');
		
		changetotopmenucurrent.setAttribute('id','topmenucurrent');
}

//Changes the body inside the page
function changebody(name) {
	new_location = document.location.href.split("#");

	currentmenu(name);
	document.location.href = new_location[0] + "#" + name;
	xml_fetch( name + ".xml","insideleft" );
	
}

function menu_item(name, description) {
	this.name = name;
	this.description = description;

	menu_count++;
}

//Checks if 'n textbox in a form is empty
function isEmpty(parm) {
	if (parm == "") return true;
}

//Checks if the text filled into the textbox is a valid number
function isValidNumber(parm) {
	var numb = '0123456789';
	
	if (parm == "") return false;
	
	for (i=0; i<parm.length; i++) {
		if (numb.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}

//Checks if the text filled into the textbox is a email address
function isValidEmail(parm) {
	var apos=parm.indexOf('@');
	var dotpos=parm.indexOf('.');
	
	if (parm == "") return false;
	
	if (apos<1 || dotpos <1) {
		return false;
	}

	return true;
}

//Calculates the price according the the data and displays it.
function pricecalcform(units,compus,pastel){
	//Define variables
	var showcalc = 0;
	var numunits, numcompus, pastelprice, compusprice, showcalc1, showcalc2, total; 

	//Get the httpElement to write the output into
	httpelement = document.getElementById('insideboxtxt');
	httpelement.innerHTML = '';

	//Check if units value is valid and show error when not
	if (isValidNumber(units.value)) {
		if(units.value > 0) {
			showcalc1 = 1;
			numunits = units.value;
		} else {
			httpelement.innerHTML += '<b>You must manage at least 1 unit</b><br>';	
			showcalc1 = 0;
		}
	} else {
		httpelement.innerHTML += '<b>Enter correct number of units</b><br>';	
		showcalc1 = 0;
	}
	
	//Check if computers value is valid and show error when not		
	if (isValidNumber(compus.value)) {
		if(compus.value > 0) {
			showcalc2 = 1;
			numcompus = compus.value;
		} else {
			httpelement.innerHTML += '<b>You must use at least 1 computer</b><br>';	
			showcalc2 = 0;
		}
	} else {
		httpelement.innerHTML += '<b>Enter correct number of computers</b><br>';	
		showcalc2 = 0;
	}

	//Assign value to parameter when pastel integration is checked
	if (pastel.checked) {
		pastelprice = 2;
	} else {
		pastelprice = 0;
	}
	
	//Cap the number of units at 20 units
	if(numunits < 20) {
		numunits = 20;
	}
	
	//Work out price for number of licenses
	if(numcompus == 1) {
		compusprice = 10;
	} else if(numcompus == 2) {
		compusprice = 12;
	} else {
		compusprice = 12 + (numcompus-2);
	}
	
	//Calculate the final price
	total = numunits * (compusprice + pastelprice);
	
	//Show the price
	if ((showcalc1 == 1) && (showcalc2 == 1)) {
		httpelement.innerHTML += '<b>Price per month : R ';
		httpelement.innerHTML += total;
		httpelement.innerHTML += '</b>';
	}
	
	
}

//Sends the entered infomation to a e-mail address
function supportform(name,company,tel,email,query){
	
	//Variables
	var save = 1;
	var params;
	var message;
	var phpreturn;
		
	//Get the httpElement to write the output into
	httpelement = document.getElementById('insideboxtxt');
	httpelement.innerHTML = '';
	
	//Check if all fields are valid
	if(isEmpty(name.value)) {
		httpelement.innerHTML += '<b>Please enter NAME</b><br>';	
		save = 0;
	}
	if(isEmpty(company.value)) {
		httpelement.innerHTML += '<b>Please enter COMPANY</b><br>';	
		save = 0;
	}
	if((isValidNumber(tel.value) == false) || (tel.value.length < 10)) {
		httpelement.innerHTML += '<b>Please enter valid TELEPHONE NUMBER (no spaces)</b><br>';	
		save = 0;
	}
	if(isValidEmail(email.value) == false) {
		httpelement.innerHTML += '<b>Please enter valid E-MAIL</b><br>';	
		save = 0;
	}
	if(isEmpty(query.value)) {
		httpelement.innerHTML += '<b>Please enter your QUERY</b><br>';
		save = 0;	
	}

	//Saves the information into a XML file and displays message on screen
	if (save == 1) {
	
		//Do the PHP script
		try {
			// Firefox, Opera 8.0+, Safari
			cgipost = new XMLHttpRequest();
		} 
		catch (e) {
			// Internet Explorer
			try {
				cgipost = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			} 
			catch (e) {
				try {
					cgipost = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e) {
					alert("Your browser does not support AJAX!");
					return null;
				}
			}
		}
		
		//Setup parameters to send mail
		message = "&message= name: " + name.value + "\n company: " + company.value + "\n tel: " + tel.value + "\n e-mail: " + email.value + "\n query: " + query.value;
		params = "subject=cssd_website : query-" + company.value + message;
		
		//Set return functions from PHP
		cgipost.onreadystatechange = function post_reply(){
			if (cgipost.readyState != 4) {
				return;
			}
			phpreturn = cgipost.responseText;
			
			if (phpreturn == "mailsent") {
				httpelement = document.getElementById("insideleft");
				httpelement.innerHTML = '<div id="insidebox"><b>Query successfully submitted. We will contact you soon!</b></div><br>';
			}
			else {
				save = 0;
				httpelement.innerHTML = '<b>Query not submitted. Please try again!<br>';
			}
		}
		
		//Execute PHP post
		cgipost.open("POST", "./php/sendmail.php", true);
		cgipost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		cgipost.setRequestHeader("Content-length", params.length);
		cgipost.setRequestHeader("Connection", "close");
		cgipost.send(params);
	}

}

