// JavaScript Document
var mx=0;
var my=0;

document.onmousemove = getMouseLocation;
document.onclick = hideProdInfo;

function hideSearch()
{
	var el = document.getElementById( "searchTable" );
	el.style.visibility = "hidden";
}

function hideProducts()
{
	var el = document.getElementById( "productTable" );
	el.style.visibility = "hidden";
}

function showSearch()
{
	var el = document.getElementById( "searchTable" );
	el.style.visibility = "visible";	
}

function showProducts()
{
	var el = document.getElementById( "productTable" );
	el.style.visibility = "visible";	
}

function agreeTerms(){

	var el = document.getElementById( "terms" );
	var btn = document.getElementById( "payBtn" );
	
	
	if( el.checked == true ){
		btn.style.visibility="visible";
	}else{
		btn.style.visibility="hidden";
	}
}


function validateRegistration( edit )
{
	var result = "";
	var error = false;
	var msg = "";
	
	// First Check that all essential fields have been filled in
	for( var i =0; i < document.regForm.length-1; i++ )
	{
		document.regForm.elements[i].style.border="inset";
		document.regForm.elements[i].style.borderColor="#888888";
		document.regForm.elements[i].style.borderWidth="thin";
		
		if( document.regForm.elements[i].value.length <= 0 && document.regForm.elements[i].name != "address2" )
		{
			error = true;
			msg = "Field cannot be empty";
			document.regForm.elements[i].style.borderStyle="solid";
			document.regForm.elements[i].style.borderWidth="thin";
			document.regForm.elements[i].style.borderColor="#FF0000";
		}
		
		result += document.regForm.elements[i].name +"="+ document.regForm.elements[i].value + "&"
	}
		
	
	if( !error )
	{
		if( !validDate(document.regForm.weddingdate.value ) )
		{
			error = true;
			msg = "Date format incorrect, or date has transpired.";
			document.regForm.weddingdate.style.borderStyle="solid";
			document.regForm.weddingdate.style.borderWidth="thin";
			document.regForm.weddingdate.style.borderColor="#FF0000";
		}
	}
	
	//If no other errors have been thrown, check that the passwords match
	if( !edit )
		if( !error )
		{
			if( document.regForm.password.value != document.regForm.password2.value )
			{
				error = true;
				msg = "Passwords do not match!";
				document.regForm.password2.style.borderStyle="solid";
				document.regForm.password2.style.borderWidth="thin";
				document.regForm.password2.style.borderColor="#FF0000";
			}
		}
		
	

	
	if( error )
	{
		alert( msg );
	}
	else
	{
		if( edit )
			location.href="editDetails.php?"+result;
		else
			location.href="showProductList.php?type=confirmReg&"+result;
	}
}


function validDate( date )
{
	var dSplit = date.split( "/" );
	
	if( dSplit.length < 3 )
	{
		alert( "Wrong format: " + dSplit.length );
		return false;
	}
	
	var a = Date.parse(Date().valueOf());
	var d = Date.parse( date );
	
	if( d < a ) 
	{
		return false;
	}
	
	return true;

}



function overNav( el )
{
	el.style.backgroundImage = "url('siteImgs/buttonOver.png')";
	el.style.cursor = "pointer";
	
}

function outNav( el )
{
	el.style.backgroundImage = "url('siteImgs/buttonOrig.png')";
	el.style.cursor = "default";
}

function handleNav( el )
{
	var nav = el.innerHTML;
	
	if( nav == "Main" )
	 	location.href = "index.php";
	 
	if( nav == "Shopping" )
	{
	 	location.href = "showProductList.php?start_pos=0&type=special";
	}
	
	
	if( nav == "Wedding Lists" )
	 	location.href = "showProductList.php?start_pos=0&type=wedding";
	 
	if( nav == "Contact" )
	 	location.href = "contactPage.php";
}


var map;
var directions;

function loadMap() 
{
      if (GBrowserIsCompatible()) 
	{
        	map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		directions = new GDirections(map);
		//setDirections("54.49686353736871, -6.653165817260742 to 54.4480788736342, -6.690351963043213 " );
		
		GEvent.addListener(map, "moveend", function() {
  			var center = map.getCenter();
  			//document.getElementById("locationText").innerHTML = center.toString();
		});
		
		GEvent.addListener(map,"dblclick", function(overlay,point) 
		{     
			setDirections( point.lat() + ", " + point.lng() +" to 54.4480788736342, -6.690351963043213 " );
		});


        	map.setCenter(new GLatLng(54.44793539525618, -6.686725616455078), 13);
		
		map.addOverlay( placeMarker( 54.4480788736342, -6.690351963043213, "Nutmeg Treasures" ) );
		
		
		
      	}
}


function changeHandling()
{
	var val = document.getElementById('delMethod').options[document.getElementById('delMethod').selectedIndex].value;
	location.href="showProductList.php?start_pos=0&type=basket&handling="+val;
}


function prodInfo( i )
{
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var el = document.getElementById("prodInfo");
			el.innerHTML = ajaxRequest.responseText;
			el.style.visibility = "visible";
			el.onmouseout = hideProdInfo;
			el.style.top= my+"px";
			el.style.left= "364px";
		}
	}
	
	ajaxRequest.open("GET", "productInfo.php?id="+i, true);
	ajaxRequest.send(null);
	
}


function hideProdInfo( e )
{

	var el = document.getElementById("prodInfo");
	el.style.visibility = "hidden";
	el.style.top= my+"px";
	el.style.left= "364px";
}


function getMouseLocation( e )
{
	if( !e )
	{
		var e = window.event;
		mx= e.clientX;
		my= e.clientY; 
	}
	else
	{
		mx = e.pageX;
    	my = e.pageY;
	}
	
	
}



function placeMarker(  lng, lat, t )
{

	var point = new GLatLng(lng, lat);

	var marker = new GMarker( point );
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<b>" + t + "</b>");
	});

	return marker;
}


// Create a directions object and register a map and DIV to hold the 
// resulting computed directions

function setDirections( query ) {
	directions.clear();
	directions = new GDirections(map, document.getElementById( "mapDetails") );
	directions.load( query );
	  
	//Hide the detaile directions at the start
	var el = document.getElementById( "mapDetails" );
	el.visibility = 'hidden';

	  
}

function changeDirection()
{
	var el = document.getElementById( "directions" );
	var loc = el.options[el.selectedIndex].value;
	
	if( loc == "armagh" )
		setDirections(  "54.348552775876065,-6.6542816162109375 to 54.4480788736342, -6.690351963043213 " );

	if( loc == "belfast" )
		setDirections("54.60369305229609, -5.9278106689453125 to 54.4480788736342, -6.690351963043213 " );
		
	if( loc == "portadown" )
		setDirections("54.42032829154646, -6.443824768066406 to 54.4480788736342, -6.690351963043213 " );		
}
	


/**
 * Called in every page, draws main menu buttons and sets up their properties
 */
function setupMainMenu()
{
	var el = document.getElementById( "mainNav" );
	var content = "";
	
	content += '<table class="mainNav"><tr>';
	content += '<td class="mainNavItem" onMouseOver="overNav(this)" onMouseOut="outNav(this)" onClick="handleNav(this)">Main</td>';
	content += '<td class="mainNavItem" onMouseOver="overNav(this)" onMouseOut="outNav(this)" onClick="handleNav(this)">Shopping</td>';
	content += '<td class="mainNavItem" onMouseOver="overNav(this)" onMouseOut="outNav(this)" onClick="handleNav(this)">Wedding Lists</td>';
	content += '<td class="mainNavItem" onMouseOver="overNav(this)" onMouseOut="outNav(this)" onClick="handleNav(this)">Contact/Links</td>';
	content += '</tr></table>';
	
	el.innerHTML = content;

}		

function updateListPurchasedSelect( listId, itemNum )
{
	
	ajaxRequest = createRequest();
	
	var sel = document.getElementById( "purchasedSelection"+itemNum );
	
	var val = sel.options[sel.selectedIndex].value;
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
				

		}
	}
	
	ajaxRequest.open("GET", "changeListStatus.php?id="+listId+"&value="+val, true);
	ajaxRequest.send(null);
	
	
}




/**
 * Called in every page, draws search dialog
 */
 function setupSearchBox()
 {
	 
	var el = document.getElementById( "searchBox" );
	var content = ""; 
	
	content += "Product Keyword:<br />";
	content += '<form action="showProductList.php?start_pos=0&type=search" method="post" >';
	content += '<input type="text" name="searchBox" class="searchInput" /><br />';
	content += '<input type="submit" value="Search" class="searchBtn"/>';
	content += '</form>';
	
	el.innerHTML = content;
 }


//Returns an AJAX request Object
function createRequest()
{

	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
}

