var http_request = false;
var myHeight;

/*window.onload = function() {
	myHeight = new fx.Height('nav', {duration: 400});
	myHeight.hide();
}*/

function makeRequest(url, parameters) {
	
    http_request = false;
        
	if (window.XMLHttpRequest) {
        
		http_request = new XMLHttpRequest();
		
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
		
	} else if (window.ActiveXObject) {
		  
		try {
			
			// code for IE7+, Firefox, Chrome, Opera, Safari
			http_request = new XMLHttpRequest();
				
		} catch (e) {
				 
			try {
				// code for IE6, IE5
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			   
			} catch (e) {}
				
		}
			 
	}
		  
	if (!http_request) {
	    alert('Cannot create XMLHTTP instance');
		return false;
	}

	hidediv('subscriptionLayer');
	
	document.getElementById('myspan').innerHTML = 'Subscribing...';
	
	http_request.open('GET', url + parameters, true);
	http_request.send(null);

	http_request.onreadystatechange = alertContents;
	
}

function hidediv(id) {

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	
	else {
		
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		
		else { // IE 4
			document.all.id.style.display = 'none';
		}
		
	}
	
}

function showdiv(id) {

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	
	else {
		
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		
		else { // IE 4
			document.all.id.style.display = 'block';
		}
		
	}
	
}

function alertContents() {
	   
    if (http_request.readyState == 4) {
		  
        if ((http_request.status == 200) || (http_request.status == '200') || (http_request.status == undefined) || (http_request.status == '0') || (http_request.status == 0)) {
			 
			result = http_request.responseText;
            document.getElementById('myspan').innerHTML = 'Thank you, you are now subscribed.';
			
			setTimeout(hideTab, 3000);
			
        } else {

            alert('There was a problem with the request. Status code is ' + http_request.status);
			
        }
		 
    }

}
  
function hideTab() {
	myHeight.toggle();
	setTimeout(hideComplete, 400);
}

function hideComplete() {
	document.getElementById('myspan').innerHTML = '';
	showdiv('subscriptionLayer');
}

function get(obj) {
	
    var getstr = "?";
    
	for (i=0; i < obj.childNodes.length; i++) {

		if (obj.childNodes[i].tagName == "DIV") {

			for (j=0; j < obj.childNodes[i].childNodes.length; j++) {
			
				if (obj.childNodes[i].childNodes[j].tagName == "INPUT") {
					
					if (obj.childNodes[i].childNodes[j].type == "text") {
					   getstr += obj.childNodes[i].childNodes[j].name + "=" + obj.childNodes[i].childNodes[j].value + "&";
					}
					
				}
				
			}
			
		}
         
    }

    makeRequest('http://change2.com.au/subscribe.php', getstr);
	  
}

function navToggle() {

	myHeight.toggle();
	
	id = 'subscriptionLayer';
	
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == 'none') {
			setTimeout(hideComplete, 400);
		}
	}
	
	else {
		
		if (document.layers) { // Netscape 4
			if (document.id.display == 'none') {
				setTimeout(hideComplete, 400);
			}
		}
		
		else { // IE 4
			if (document.all.id.style.display == 'none') {
				setTimeout(hideComplete, 400);
			}
		}
		
	}
	
}

function create_menu(basepath)
{
	myHeight = new fx.Height('nav', {duration: 400});
	myHeight.hide();

	var base = (basepath == 'null') ? '' : basepath;

	var action = "javascript:get(document.getElementById('subscriptionForm'))";
	var formClass = "autoform";
	
	document.write(

		'<div id = "mainLayer">' + 
	
			'<div id= "subscriptionLayer">' +
			
				'<table id="table" cellpadding="2" cellspaceing="2" border="0" style="width:98%"><tr>' +
					
					'<td class="td" valign="top">' +
						
						"<form name='subscriptionForm' id='subscriptionForm' action = " + action + " class = " + formClass + ">" + 
						
							//'<div><instructions>If you wish to subscribe to our newsletter, simply fill in your name and email address.</instructions></div>' +
							'<div class="instructions">If you wish to subscribe to our newsletter, simply fill in your name and email address.</div>' +
			
							'<div><label>Name:</label><input type="text" name="name" class="name" id="name" maxlength="30"/></div>' +
							
							'<div><label>Email:</label><input type="text" name="email" class="email" id="email"/></div>' +
	
							//' <div id="submitdiv"><input id="submit" type="submit" value="Submit"/></div>' +
							'<div class="submitdiv"><input id="submit" type="submit" value="Submit"/></div>' +
	
						'</form>' +
				
				'</td></tr></table>' +

			'</div>' +			
				
			'<span id="myspan" class="myspan"></span>' +
			
		'</div>'
		
	);

}