function SwapImage(src, id)
{
	ClearImgs();
	document.getElementById(id).src = src;
}

function SwapImageR(src, id)
{
	ClearImgsR();
	document.getElementById(id).src = src;
}

function ClearImgs()
{
	document.getElementById('btn_company_profile').src = 'images/btn_company_profile.jpg';
	document.getElementById('btn_investor_info').src = 'images/btn_investor.jpg';
	document.getElementById('btn_regulations').src = 'images/btn_regulations.jpg';
	document.getElementById('btn_customers').src = 'images/btn_customers.jpg';
	document.getElementById('btn_safety').src = 'images/btn_safety.jpg';
	document.getElementById('btn_sponsorship').src = 'images/btn_sponsorship.jpg';
}

function ClearImgsR()
{
	document.getElementById('btn_news').src = 'images/btn_news.jpg';
	document.getElementById('btn_faq').src = 'images/btn_faq.jpg';
	document.getElementById('btn_network_status').src = 'images/btn_network_status.jpg';
	document.getElementById('btn_contact').src = 'images/btn_contact.jpg';
	document.getElementById('btn_jobs').src = 'images/btn_jobs.jpg';
}
// AJAX FUNCTIONS

//var http_request = null; // null by defult
//var elementId = false; 
   
function makeRequest(url,objName) {
	elementId=objName; 
	if (window.XMLHttpRequest) { // Mozilla, Safari,... 
		   http_request = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { // IE 
		   http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	http_request.onreadystatechange = updateItem; 
	http_request.open('GET', uniqueURL (url), true); 
	http_request.send(null); 
} 
 
function makePostRequest(url,objName,parameters) {
	elementId=objName; 
	if (window.XMLHttpRequest) { // Mozilla, Safari,... 
		   http_request = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { // IE 
		   http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 

	http_request.onreadystatechange = updateItem; 
	http_request.open('POST', uniqueURL (url), true); 
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
} 

function updateItem() {
	try {
		if (http_request.readyState == 4) {
			if (http_request.status) if (http_request.status == 200) {  //alert(http_request.responseText); 
				document.getElementById(elementId).innerHTML=http_request.responseText; 
			} else { 
				document.getElementById(elementId).innerHTML="There was a problem with the request"; 
			} 
		} 
	}
	catch (e) {
		document.getElementById(elementId).innerHTML="There was a problem with the request"; 
	}
}

function uniqueURL (url) {
	ms = new Date().valueOf();
	if (url.indexOf("?", 0) == -1) url += '?';
	else url += '&';
	return url+ms;
}

//---------ADMIN FORM-----------------------------------------------
function submitLoginForm()
{
	submitted = document.getElementById('issubmitted').value;
	logintype = document.getElementById('logintype').value;
	login = document.getElementById('txtLogin').value;
	pass = document.getElementById('txtPassword').value;
	//alert('admin.php?submitted=' + submitted + '&login=' + login + '&pass=' + pass);
	makePostRequest('extranet.php', 'content', 'submitted=' + encodeURI(submitted) + 
													'&logintype=' + encodeURI(logintype) + 
													'&login=' + encodeURI(login) + 
													'&pass=' + encodeURI(pass));
}


//----------NETWORK STATUS FORM-------------------------------------
function submitStatusForm()
{
	statuschanged = document.getElementById('statuschanged').value;
	if (document.getElementsByName('radStatus')[0].checked)
	{	radStatus = 1;	}
	else
	{	radStatus = 0;	}
	
	txtMessage = escape(document.getElementById('txtMessage').value);
	makePostRequest('netstatus_edit.php', 'subcontent', 'statuschanged=' + encodeURI(statuschanged) + 
													'&radStatus=' + encodeURI(radStatus) + 
													'&txtMessage=' + encodeURI(txtMessage));
	setTimeout("makeRequest('r_network_status.php','panel')",100); 
								
}

//----------NEWS FORMS-------------------------------------------------
function submitAddNewsForm($newsform)
{
	if (validateNewsForm() == false) return;
	
	submitted = document.getElementById('submitted').value;
	txtHeading = escape(document.getElementById('txtHeading').value);
	txtContent = escape(document.getElementById('txtContent').value);
	if ($newsform == 'nzx_add.php') {txtNZXID = escape(document.getElementById('txtNZXID').value);}
	if ($newsform == 'takeover_add.php') {txtPDFid = escape(document.getElementById('txtPDFid').value);}
	txtPublished = escape(document.getElementById('dpPublished').value); 
	txtExpiry = escape(document.getElementById('dpExpiry').value); 

	txtSQL = 'submitted=' + encodeURI(submitted) + '&txtHeading=' +  encodeURI(txtHeading) + 
		'&txtPublished=' +  encodeURI(txtPublished) + '&txtExpiry=' +  encodeURI(txtExpiry) + 
		'&txtContent=' + encodeURI(txtContent);
	if ($newsform == 'nzx_add.php') {
		txtSQL = txtSQL + '&txtNZXID=' + encodeURI(txtNZXID);
		}
	if ($newsform == 'takeover_add.php') {
		txtSQL = txtSQL + '&txtPDFid=' + encodeURI(txtPDFid);
		}

	makePostRequest($newsform, 'subcontent', txtSQL);

//	makePostRequest($newsform, 'subcontent', 'submitted=' + encodeURI(submitted) + 
//												'&txtHeading=' + encodeURI(txtHeading) + 
//												'&txtContent=' + encodeURI(txtContent) + 
//												'&txtPublished=' + encodeURI(txtPublished) + 
//												'&txtExpiry=' + encodeURI(txtExpiry));
	setTimeout("makeRequest('r_news.php','panel')",100); 
	if ($newsform == 'nzx_add.php') setTimeout("makeRequest('r_news_NZX.php','NewsPanel')",150); 
}

function submitEditNewsForm($newsform)
{
	if (validateNewsForm() == false) return;
	
	submitted = document.getElementById('submitted').value;
	id = escape(document.getElementById('id').value);
	txtHeading = escape(document.getElementById('txtHeading').value); 
	txtContent = escape(document.getElementById('txtContent').value); //alert("1");
	txtPublished = escape(document.getElementById('dpPublished').value); 
	txtExpiry = escape(document.getElementById('dpExpiry').value);
	if ($newsform == 'nzx_edit.php') {txtNZXID = escape(document.getElementById('txtNZXID').value);}
	if ($newsform == 'takeover_edit.php') {txtPDFid = escape(document.getElementById('txtPDFid').value);}

	txtSQL = 'submitted=' + encodeURI(submitted) + '&txtHeading=' +  encodeURI(txtHeading) + 
		'&txtPublished=' +  encodeURI(txtPublished) + '&txtExpiry=' +  encodeURI(txtExpiry) + 
		'&id=' + encodeURI(id) + '&txtContent=' + encodeURI(txtContent);
	if ($newsform == 'nzx_edit.php') {
		txtSQL = txtSQL + '&txtNZXID=' + encodeURI(txtNZXID);
		}
	if ($newsform == 'takeover_edit.php') {
		txtSQL = txtSQL + '&txtPDFid=' + encodeURI(txtPDFid);
		}

	makePostRequest($newsform, 'subcontent', txtSQL);
	setTimeout("makeRequest('r_news.php','panel')",100); 								
	if ($newsform == 'nzx_edit.php') {
		setTimeout("makeRequest('r_news_NZX.php','NewsPanel')",150); 
		}
	if ($newsform == 'takeover_edit.php') {
		setTimeout("makeRequest('r_news_Takeover.php','NewsPanel')",150); 
		}
}

function DeleteNewsItem(id)
{
	var x = confirm('Are you sure you want to delete this News item?');
	if(x == true) 
	{
		makePostRequest('news_delete.php','subcontent', 'id=' + encodeURI(id));
		setTimeout("makeRequest('r_news.php','panel')",150); 
	} 
}

function validateNewsForm() {
	//validate published date
	var published = document.getElementById('dpPublished').value;
	var d = parseDMYDate(published);
	if (d == null) {
		alert('Please enter a date in the form dd/mm/yyyy for the Published Date field');
		return false;
	}
	else {
		document.getElementById('dpPublished').value = d;
	}
	//validate expiry date
	var expiry = document.getElementById('dpExpiry').value;
	d = parseDMYDate(expiry);
	if (d == null) {
		alert('Please enter a date in the form dd/mm/yyyy for the Expiry Date field');
		return false;
	}
	else {
		document.getElementById('dpExpiry').value = d;
	}
	return true;
}

//----------NZXNEWS FORMS-------------------------------------------------
function DeleteNZXNewsItem(id)
{
	var x = confirm('Are you sure you want to delete this NZX News item?');
	if(x == true) 
	{
		makePostRequest('nzx_delete.php','subcontent', 'id=' + encodeURI(id));
		setTimeout("makeRequest('r_news.php','panel')",100); 
		setTimeout("makeRequest('r_news_NZX.php','NewsPanel')",150); 
	} 
}

//----------Takeover FORMS-------------------------------------------------
function DeleteTakeoverItem(id)
{
	var x = confirm('Are you sure you want to delete this Takeover News item?');
	if(x == true) 
	{
		makePostRequest('takeover_delete.php','subcontent', 'id=' + encodeURI(id));
		setTimeout("makeRequest('r_news.php','panel')",100); 
		setTimeout("makeRequest('r_news_Takeover.php','NewsPanel')",150); 
	} 
}


//---------JOBS FORMS----------------------------------------------------------------
function submitAddJobForm()
{
	if (validateJobsForm() == false) return;

	submitted = document.getElementById('submitted').value;
	txtTitle = escape(document.getElementById('txtTitle').value);
	txtDescription = escape(document.getElementById('txtDescription').value);
	txtshortdescription = escape(document.getElementById('txtshortdescription').value);
	txtExpiry = escape(document.getElementById('dpExpiry').value); 
	if (document.getElementsByName('radActive')[0].checked)
	{	radActive = 1;	}
	else
	{	radActive = 0;	}
	makePostRequest('job_add.php', 'subcontent', 'submitted=' + encodeURI(submitted) + 
												'&txtTitle=' + encodeURI(txtTitle) + 
												'&txtDescription=' + encodeURI(txtDescription) + 
												'&txtshortdescription=' + encodeURI(txtshortdescription) + 
												'&txtExpiry=' + encodeURI(txtExpiry) + 
												'&radActive=' + encodeURI(radActive));
	setTimeout("makeRequest('r_jobs.php','panel')",100); 								
}

function submitEditJobsForm()
{
	if (validateJobsForm() == false) return;

	submitted = document.getElementById('submitted').value;
	id = escape(document.getElementById('id').value);
	txtTitle = escape(document.getElementById('txtTitle').value);
	txtDescription = escape(document.getElementById('txtDescription').value);
	txtshortdescription = escape(document.getElementById('txtshortdescription').value); 
	txtExpiry = escape(document.getElementById('dpExpiry').value); 
	if (document.getElementsByName('radActive')[0].checked)
	{	radActive = 1;	}
	else
	{	radActive = 0;	}
	makePostRequest('job_edit.php', 'subcontent', 'submitted=' + encodeURI(submitted) + 
													'&id=' + encodeURI(id) + 
													'&txtTitle=' + encodeURI(txtTitle) + 
													'&txtDescription=' + encodeURI(txtDescription) + 
													'&txtshortdescription=' + encodeURI(txtshortdescription) + 
													'&txtExpiry=' + encodeURI(txtExpiry) + 
													'&radActive=' + encodeURI(radActive));
	setTimeout("makeRequest('r_jobs.php','panel')",100); 								
}

function DeleteJob(id)
{
	var x = confirm('Are you sure you want to delete this job listing?');
	if(x == true) 
	{
		makePostRequest('job_delete.php','subcontent','id=' + encodeURI(id));
		setTimeout("makeRequest('r_jobs.php','panel')",100); 
	} 
}

function validateJobsForm() {
	//validate expiry date
	var expiry = document.getElementById('dpExpiry').value;
	d = parseDMYDate(expiry);
	if (d == null) {
		alert('Please enter a date in the form dd/mm/yyyy for the Expiry Date field');
		return false;
	}
	else {
		document.getElementById('dpExpiry').value = d;
	}
	return true;
}

//---------FAQs FORMS----------------------------------------------------------------
function submitAddFaqForm()
{
	submitted = document.getElementById('submitted').value;
	txtQuestion = escape(document.getElementById('txtQuestion').value);
	txtAnswer = escape(document.getElementById('txtAnswer').value); //alert("1");
	makePostRequest('faq_add.php', 'subcontent', 'submitted=' + encodeURI(submitted) + 
												'&txtQuestion=' + encodeURI(txtQuestion) + 
												'&txtAnswer=' + encodeURI(txtAnswer));
	setTimeout("makeRequest('r_faqs.php','panel')",100); 								
}

function submitEditFaqsForm()
{
	submitted = document.getElementById('submitted').value;
	id = escape(document.getElementById('id').value);
	txtQuestion = escape(document.getElementById('txtQuestion').value);
	txtAnswer = escape(document.getElementById('txtAnswer').value); //alert("1");
	makePostRequest('faq_edit.php', 'subcontent', 'submitted=' + encodeURI(submitted) + 
													'&id=' + encodeURI(id) + 
													'&txtQuestion=' + encodeURI(txtQuestion) + 
													'&txtAnswer=' + encodeURI(txtAnswer));
	setTimeout("makeRequest('r_faqs.php','panel')",100); 								
}

function DeleteFaq(id)
{
	var x = confirm('Are you sure you want to delete this FAQ entry?');
	if(x == true) 
	{
		makePostRequest('faq_delete.php','subcontent','id=' + encodeURI(id));
		setTimeout("makeRequest('r_faqs.php','panel')",100); 
	} 
}

function moveFAQUp(faqid) {	
	makeRequest('faq_view.php?moveUp=' + faqid,'subcontent');
}

function moveFAQDown(faqid) {
	makeRequest('faq_view.php?moveDown=' + faqid,'subcontent');
}

//---------USERS FORMS----------------------------------------------------------------
function verifyPassword(pass1, pass2, mode)
{
	if ((mode == "add") && (pass1 == ""))
	{
		alert("You have to enter a password");
		return false;
	}
	
	if (pass1 != pass2)
	{
		alert("The passwords was not identical. Please enter password and re-confirm");
		document.getElementById('txtPassword').value = "";
		document.getElementById('txtPassword2').value = "";
		document.getElementById('txtPassword').focus();
		return false;
	}
	else
	{	return true;	}	
}

function verifyLogin()
{
	if (document.getElementById('txtLogin').value == "")
	{
		alert("You have to enter a login name.");
		document.getElementById('txtLogin').focus();
		return false;
	}
	else
		return true;
}
function submitAddUserForm()
{
	if (verifyLogin())
	{
		submitted = document.getElementById('submitted').value;
		txtLogin = escape(document.getElementById('txtLogin').value);
		txtPassword = escape(document.getElementById('txtPassword').value); 
		txtPassword2 = escape(document.getElementById('txtPassword2').value); 
		if (document.getElementsByName('radUserType')[0].checked)				{	userType = 'admin';	}
		else if (document.getElementsByName('radUserType')[1].checked)			{	userType = 'trader';	}
		else if (document.getElementsByName('radUserType')[2].checked)		{	userType = 'contractor';	}
		txtUrl = escape(document.getElementById('txtUrl').value);
		
		if (verifyPassword(txtPassword, txtPassword2, "add"))
		{	
			makePostRequest('user_add.php', 'subcontent', 'submitted=' + encodeURI(submitted) + 
															'&txtLogin=' + encodeURI(txtLogin) + 
															'&txtPassword=' + encodeURI(txtPassword) + 
															'&radUserType=' + encodeURI(userType) + 
															'&txtUrl=' + encodeURI(txtUrl));
		}
	}
}

function submitEditUsersForm()
{
	if (verifyLogin())
	{
		submitted = document.getElementById('submitted').value;
		id = document.getElementById('id').value; 
		txtLogin = escape(document.getElementById('txtLogin').value);
		txtPassword = escape(document.getElementById('txtPassword').value); 
		txtPassword2 = escape(document.getElementById('txtPassword2').value); 
		if (document.getElementsByName('radUserType')[0].checked)				{	userType = 'admin';	}
		else if (document.getElementsByName('radUserType')[1].checked)			{	userType = 'trader';	}
		else if (document.getElementsByName('radUserType')[2].checked)			{	userType = 'contractor';	}
		txtUrl = escape(document.getElementById('txtUrl').value);
		
		if (verifyPassword(txtPassword, txtPassword2, "edit"))
		{
			makePostRequest('user_edit.php', 'subcontent', 'submitted=' + encodeURI(submitted) + 
															'&id=' + encodeURI(id) + 
															'&txtLogin=' + encodeURI(txtLogin) + 
															'&txtPassword=' + encodeURI(txtPassword) + 
															'&radUserType=' + encodeURI(userType) + 
															'&txtUrl=' + encodeURI(txtUrl));
		}
	}
}

function DeleteUsers(id)
{
	var x = confirm('Are you sure you want to delete this user?');
	if(x == true) 
		makePostRequest('user_delete.php','subcontent','id=' + encodeURI(id));
}

//---------GLOSSARY FUNCTIONS----------------------------------------------------------------
function glossary_update($action)
{
	$id = document.getElementById('id').value;
	$phrase = document.getElementById('phrase').value;
	$meaning = document.getElementById('meaning').value;
	$source = document.getElementById('source').value;

	if ($action == "update") {			
		$query = "UPDATE glossary SET " +
			"phrase='" + $phrase + "' , " +
			"meaning='" + $meaning + "' , " + 
			"source='" + $source + "' " + 
			"WHERE id=" + $id ;
		} else if ($action == "add") {
			$query = "INSERT INTO glossary SET " +
			"phrase='" + $phrase + "' , " +
			"meaning='" + $meaning + "' , " + 
			"source='" + $source + "'";
		} else if ($action == "delete") {
			$query = "DELETE FROM glossary WHERE id=" + $id ;
		}
		
	$url = 'r_glossary.php?$sqlcmd=' + encodeURI($query );
//alert($url);
	makeRequest($url ,'panel');	
}

function glossary_faq()
{
	makeRequest('glossary.php','content')
	setTimeout("makeRequest('r_faqs.php','panel')",100); 
}

//---------SPONSORSHIP FUNCTIONS----------------------------------------------------------------
function sponsorship_update($action)
{
	$id = document.getElementById('id').value;
	$organisation = document.getElementById('organisation').value;
	$website = document.getElementById('website').value;
	$email = document.getElementById('email').value;
	$description = document.getElementById('description').value;
//	$logo = document.getElementById('logo').value;
	$value = document.getElementById('value').value;

	if ($action == "update") {			
			$query = "UPDATE sponsorship SET ";
			if ($organisation != null) { $query = $query + "organisation='" + $organisation + " ' ";}
			if ($website != null) { $query = $query + ", website='" + $website + " ' ";}
			if ($email != null) { $query = $query + ", email='" + $email + " ' ";}
			if ($description != null) { $query = $query + ", description='" + $description + " ' ";}
//			if ($logo!= null) { $query = $query + ", logo='" + $logo + " ' ";}
			if ($value != null) { $query = $query + ", value='" + $value + " ' ";}
			$query = $query + "WHERE id=" + $id ;
		} else if ($action == "add") {
			$query = "INSERT INTO sponsorship SET ";
			if ($organisation != null) { $query = $query + "organisation='" + $organisation + " ' ";}
			if ($website != null) { $query = $query + ", website='" + $website + " ' ";}
			if ($email != null) { $query = $query + ", email='" + $email + " ' ";}
			if ($description != null) { $query = $query + ", description='" + $description + " ' ";}
//			if ($logo!= null) { $query = $query + ", logo='" + $logo + " ' ";}
			if ($value != null) { $query = $query + ", value='" + $value + " ' ";}
		} else if ($action == "delete") {
			$query = "DELETE FROM sponsorship WHERE id=" + $id ;
		}
		
	$url = 'r_sponsorship.php?$sqlcmd=' + encodeURI($query );
//alert($url);
	makeRequest($url ,'panel');	
}

function sponsorship_done()
{
	makeRequest('Sponsor_Other.php','subcontent')
}

//---------------------------------------------------------------------------------------------------------------