function getPorts(country)	//populates ports depending on selection of country
{
	var strURL = "http://www.stockwells.com.au/roqdb-getports.php?country=" + country;	
	var req = getXMLHTTP(); // fuction to get xmlhttp object
	if (req)
	{
		req.onreadystatechange = function()
		{
			if (req.readyState == 4) //data is retrieved from server
			{
				if (req.status == 200) // which reprents ok status
					document.getElementById('portdiv').innerHTML=req.responseText;
				else
					alert("There was a problem while using XMLHTTP:\n");
			}            
		}        
		req.open("GET", strURL, true); //open url using get method
		req.send(null);
	}
}

function updateCurrency(from, to, amt)	//populates ports depending on selection of country
{
	var strURL = "http://www.stockwells.com.au/currency-convert.php?from=" + from + "&to=" + to + "&amt=" + amt;	
	var req = getXMLHTTP(); // fuction to get xmlhttp object
	if (req)
	{
		req.onreadystatechange = function()
		{
			if (req.readyState == 4) //data is retrieved from server
			{
				if (req.status == 200) // which reprents ok status
					document.getElementById('curr').innerHTML=req.responseText;
				else
					alert("There was a problem while using XMLHTTP:\n");
			}            
		}        
		req.open("GET", strURL, true); //open url using get method
		req.send(null);
	}
}

function getXMLHTTP() //fuction to return the xml http object
{
	var xmlhttp=false;	
	try
	{
		xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{		
		try
		{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1)
			{
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}


//online quick quote form validation
function validate_form(thisform)
{
	with (thisform)
	{
		var e = checkSizeBox(thisform);		//checks which size box is selected	
		var eStr = getChars(thisform, e);	//gets chars to validate
		
		//validate empty fields
		if (!validate_empty(name) || !validate_empty(company) || !validate_empty(email) || !validate_empty(number) ||
			!validate_empty(e) || !validate_empty(ports) || !validate_empty(destination) || !validate_empty(shipments))
		{
			alert("Please fill out all fields.");
			return false;
		}
		
		//validate email syntax
		if (!validate_email(email))
		{
			alert("Please enter a valid email");
			email.focus();
			return false;
		}
		
		//validates contact number
		if (!validate_chars(number, "0123456789()-+ "))
		{
			alert("Please enter a valid contact number");
			number.focus();
			return false;
		}
		
		//validates numeric size
		if (!validate_chars(e, eStr))
		{
			alert("Please enter a valid size");
			e.focus();
			return false;
		}

		//validates numeric number
		if (!validate_chars(shipments, "0123456789"))
		{
			alert("Please enter a valid number");
			shipments.focus();
			return false;
		}		
	}
}
//validates if not empty
function validate_empty(field)
{
	with (field)
		if (value==null||value==""||value==0)
			return false;
	return true;
}

//validates via certain characters
function validate_chars(field, validChars)
{
	with(field)
		for(i = 0; i < value.length; i++)
			if(validChars.indexOf(value.charAt(i)) == -1)
				return false;
	return true;
}

//validates email syntax
function validate_email(field)
{
	with (field)
		var str = value;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1)
	   return false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;

	 if (str.indexOf(at,(lat+1))!=-1)
		return false;

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	 if (str.indexOf(dot,(lat+2))==-1)
		return false;
	
	 if (str.indexOf(" ")!=-1)
		return false;

	 return true;					
}


function getChars(thisform, e)
{
	with(thisform)
	{
		if(e == FCLsize)
			return "0123456789";
		return "0123456789.";
	}
}


function checkSizeBox(thisform)
{
	with(thisform)
	{
		if(airmode.checked == true)
			return AIRsize;
		else if(fclmode.checked == true)
			return FCLsize;
		else if(lclmode.checked == true)
			return LCLsize;
	}
}



function sea()
{
	document.getElementById('searow').style.display="";
	if(document.getElementById('fclmode').checked == true)
		FCL();
	else if(document.getElementById('lclmode').checked == true)
		LCL();
}

function air()
{
	document.getElementById('searow').style.display="none";
	document.getElementById('fclrow').style.display="none";
	document.getElementById('fclsize').style.display="none";
	document.getElementById('lclsize').style.display="none";
	document.getElementById('airsize').style.display="";
}

function FCL()
{
	document.getElementById('fclrow').style.display="";
	document.getElementById('fclsize').style.display="";
	document.getElementById('lclsize').style.display="none";
	document.getElementById('airsize').style.display="none";
}

function LCL()
{
	document.getElementById('fclrow').style.display="none";
	document.getElementById('fclsize').style.display="none";
	document.getElementById('lclsize').style.display="";
	document.getElementById('airsize').style.display="none";
}



























function searchField(val)
{
	switch(val)
	{
		case "mode": 	searchMode();				
						break;
		case "origin":	searchOrigin();
						break;
		case "dest":	searchDest();
						break;
		case "price":	searchPrice();
						break;
		case "ship":	searchShip();
						break;
		default:		searchID();
	}
}

function searchID()
{
	document.getElementById('searchid').style.display="";
	document.getElementById('modeid').style.display="none";
	document.getElementById('originid').style.display="none";
	document.getElementById('destid').style.display="none";
	document.getElementById('priceid').style.display="none";
	document.getElementById('shipid').style.display="none";
}

function searchMode()
{
	document.getElementById('searchid').style.display="none";
	document.getElementById('modeid').style.display="";
	document.getElementById('originid').style.display="none";
	document.getElementById('destid').style.display="none";
	document.getElementById('priceid').style.display="none";
	document.getElementById('shipid').style.display="none";
}

function searchOrigin()
{
	document.getElementById('searchid').style.display="none";
	document.getElementById('modeid').style.display="none";
	document.getElementById('originid').style.display="";
	document.getElementById('destid').style.display="none";
	document.getElementById('priceid').style.display="none";
	document.getElementById('shipid').style.display="none";
}

function searchDest()
{
	document.getElementById('searchid').style.display="none";
	document.getElementById('modeid').style.display="none";
	document.getElementById('originid').style.display="none";
	document.getElementById('destid').style.display="";
	document.getElementById('priceid').style.display="none";
	document.getElementById('shipid').style.display="none";
}

function searchPrice()
{
	document.getElementById('searchid').style.display="none";
	document.getElementById('modeid').style.display="none";
	document.getElementById('originid').style.display="none";
	document.getElementById('destid').style.display="none";
	document.getElementById('priceid').style.display="";
	document.getElementById('shipid').style.display="none";
}

function searchShip()
{
	document.getElementById('searchid').style.display="none";
	document.getElementById('modeid').style.display="none";
	document.getElementById('originid').style.display="none";
	document.getElementById('destid').style.display="none";
	document.getElementById('priceid').style.display="none";
	document.getElementById('shipid').style.display="";
}


















function validate_edit(thisform)
{
	with (thisform)
	{	
		//validates numeric data
		if (!validate_chars(val, "0123456789.") || !validate_empty(val))
		{
			alert("Please enter a valid number (no special characters e.g. %, $).");
			return false;
		}		
	}
}

function validate_adduser(thisform)
{
	with(thisform)
	{
			
		//validates email
		if (!validate_email(username) || !validate_empty(username))
		{
			alert("Please enter a valid email");
			return false;
		}

		if(!validate_empty(password) || !validate_empty(password2))
		{
			alert("Please enter and re-enter a password");
			password.value = '';
			password2.value = '';
			return false;
		}
		
		if(password.value != password2.value)
		{
			alert("Passwords do not match.");
			password.value = '';
			password2.value = '';
			return false;
		}
	
	var text = "Are you sure you wish to make these changes?";
	if (!confirm(text))
		return false;
	}
}

function selectAll(thisform)
{
	//alert("select   " + thisform);
	with(thisform)
	{
		for(i = 0; i < deletes.length; i++)
			deletes[i].checked = true;
	}
}

function deselectAll(thisform)
{
	//alert("deselect   " + thisform);
	with(thisform)
	{
		for(i = 0; i < deletes.length; i++)
			deletes[i].checked = false;
	}
}

function validate_quotes(thisform)
{
	with (thisform)
	{
		switch(select.value)
		{
			case "id":		if(!validate_empty(search) || search.value == "Search" || !validate_chars(search, "0123456789"))
							{
								alert("Please enter a valid ID No.");
								return false;
							}
							break;
							
			case "company":	if(!validate_empty(search) || search.value == "Search")
							{
								alert("Please enter a search value");
								return false;
							}
							break;
							
			case "origin":	if(!validate_empty(ports))
							{
								alert("Please select origin port");
								return false;
							}
							break;
							
			case "dest":	if(!validate_empty(destination))
							{
								alert("Please select destination port");
								return false;
							}		
							break;
			
			case "price":	if(!validate_empty(minprice) && !validate_empty(maxprice))
							{
								alert("Error, both fields set to 0");
								return false;
							}
							if(!validate_chars(minprice, "0123456789.") || !validate_chars(maxprice, "0123456789."))
							{
								alert("Invalid price, please enter a valid number (e.g. 1234 or 12345.56 etc)");
								return false;
							}
							if(minprice.value > maxprice.value && !validate_empty(maxprice))
							{
								alert("Minimum price: " + minprice.value + " cannot be greater than maximum price: " + maxprice.value + ".");
								return false;
							}
							break;
							
			case "ship":	if(!validate_empty(shipments))
							{
								alert("Please select a shipment.");
								return false;
							}
							break;
		}
		return true;
	}
	return true;}

function validate_user(thisform)
{	
	with (thisform)
	{	
		//validates email
		if (!validate_email(username))
		{
			alert("Please enter a valid email");
			return false;
		}

		if(password.value != password2.value)
		{
			alert("Passwords do not match.");
			password.value = '';
			password2.value = '';
			return false;
		}
	}
	
	var text = "Are you sure you wish to make these changes?";
	if (!confirm(text))
		return false;
}


