// Base URL
var livesite = 'http://www.woolacombe.co.uk/';

// Global Ajax variable
var ajaxRequest;

// Function to initialise Ajax
function intAjax() {
	
	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("HTTP Request Error");
				return false;
			}
		}
	}
	
}


// Function to lookup address
function lookupAddress() {

	intAjax();
	
	var resultDisplay = document.getElementById('address');
	
	var postcode = document.getElementById('find_postcode').value;
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
	
	    if(ajaxRequest.readyState == 4){
	
	    	resultDisplay.innerHTML = ajaxRequest.responseText;
	    	
	    } else {
	    
	    	resultDisplay.innerHTML = '<img src="' + livesite + 'images/ajax-loader.gif" />';
	    
	    }
	    
	}
	
	var queryString = "?postcode=" + postcode + "&count=" + Math.random();
	
	var url = livesite + "staticfiles/nusoap/lib/login.php";
	
	ajaxRequest.open("GET", url + queryString, true);
	ajaxRequest.send(null);

}

/* Calculates x and y coords of an element */
function findPos(obj) {
	
	// Initialise variables
	var curleft = curtop = 0;
	
	// Calculate coords relative to parent
	if (obj.offsetParent) {
	
		do {
			
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			
			} 
			
		while (obj = obj.offsetParent);
		
		// Return array of coords
		return [curleft,curtop];
	}

}


/* Relocate element to attach to another */

function relocate_el(elName, container, attach, padding) {
	
	// get element ids of container and element
	var el = document.getElementById(elName);
	var cont = document.getElementById(container);
	
	// Initialise node count variable
	var nodes = 0;
	
	// Count number of divs in left column
	for (i = 0; i < cont.childNodes.length; i++) {
	
		if (cont.childNodes[i].tagName == 'DIV') {
		
			nodes += 1;
			
			// If the class name matches the name of the attach element, mark the position
			if (cont.childNodes[i].className == attach) {
				
				var pos = i;
			
			}
		
		}
	
	}

	// Get element coords
	var coords = findPos(cont.childNodes[pos]);
	
	// Get width & height of element
	var elWidth = el.width;
	var elHeight = el.height;
	
	// Position the element
	el.style.top = (coords[1] - cont.offsetHeight) + 'px';
	//el.style.left = coords[0] - (cont.offsetWidth + (elWidth * 1.75) - padding[1] + padding[3]) + 'px';
	el.style.left = '-140px';
	
}
