﻿var fadeInterval = '';
function ShowMore()
{
	var popup = window.open("Contact.aspx", "_blank", "status=0, toolbar=0, location=0, menubar=0, resizeable=0, height=440px, width=450px");
	if(!popup)
		alert('It seems like your browser does not allow popups. Please enable them for this site and try again.');
}
	
//Starts the fade animation, dependant on the zindex passes to which way it fades		
function StartFade(zindex, conID)
{
	if(zindex > 0) {
		ToggleOverlay(zindex);
		fade(0, 1, conID);
	}
	else {
		ToggleContact(zindex, conID);
		fade(66, -1, conID)
	}
}

//Toggles the zindex of the contact form to either show or not show
function ToggleContact(zindex, conID)
{
	var contactForm = document.getElementById(conID);
	contactForm.style.zIndex = zindex;
}

//Toggles the zindex of the transparent (semi) overlay to either show or not show
function ToggleOverlay(zindex)
{
	var overlay = document.getElementById("transparent_overlay");
	overlay.style.zIndex = zindex;
}

//toggles the listbox to fix the z-index ie bug
function ToggleLB(display)
{
	document.getElementById("lbHeard").style.display = display;
}

//recursive function to change the opacity of the overlay to appear to be fading in or out
function fade(opacity, multiplyer, conID)
{
	clearInterval(fadeInterval);
	
	if(opacity < 0) {
		ToggleOverlay(-1000)
		return;
	}
	else if(opacity > 66) {
		ToggleContact(1000, conID);
		return;
	}
	
	var overlay = document.getElementById("transparent_overlay");
	overlay.style.opacity = opacity/100;
	overlay.style.MozOpacity = opacity/100;
	overlay.style.filter = "alpha(opacity="+opacity+")";
	
	if(opacity <= 66 && opacity >= 0)
		fadeInterval = setInterval("fade(" + (opacity+(11*multiplyer)) + ", " + multiplyer + ", '" + conID + "')", 10);
		
}

function btnSend_Click()
{
	var name = document.getElementById("tbName").value;
	var email = document.getElementById("tbEmail").value;
	var phone = document.getElementById("tbPhone").value;
	var lbHeard = document.getElementById("lbHeard");
	var message = document.getElementById("tbMessage").value;
	
	if(CheckContactForm(name, email, phone)) {
		LoginNew.SendEnquiry(name, email, phone, lbHeard.options[lbHeard.selectedIndex].text, message, this.btnSend_Callback.bind(this));
		StartFade(-1000, "contact_overlay");
	}
}

function btnSend_Callback(res)
{
	if(res.error != null || !res.value) 
	{	
		alert("Sending the email failed. Please try again. (Try refresh the page)");
	}
	else
	{
		alert("Your Enquiry has been sent, you will hear back from us soon.");
	}
}

function CheckContactForm(name, email, phone)
{
	if(name.length == 0) {
		document.getElementById("lblNameError").innerHTML = "Please enter your name.";
		return false;
	}
	document.getElementById("lblNameError").innerHTML = "";
	
	if(email.length == 0) {
		document.getElementById("lblEmailError").innerHTML = "Please enter your email address.";
		return false;
	}
	document.getElementById("lblEmailError").innerHTML = "";
	
	if(phone.length == 0) {
		document.getElementById("lblPhoneError").innerHTML = "Please enter your phone number.";
		return false;
	}
	document.getElementById("lblPhoneError").innerHTML = "";
	
	return true;
}

function ShowForgotPassword() {
	StartFade(1000, 'overlay_ui');
	document.getElementById('tbUsernameFP').focus();
	document.getElementById('forgotPasswordError').style.display = "none";
}

function btnGetPassword_Click()
{
	var username = document.getElementById("tbUsernameFP").value;
	if(username.length <= 0) {
		alert("Please enter a username.");
		return;
	}
	LoginNew.ForgotPassword(username, this.btnGetPassword_Callback.bind(this));
}

function btnGetPassword_Callback(res) {
	var error = document.getElementById('forgotPasswordError');
	if(res.error != null) {
		if (res.error.Type == "ConnectFailure")
			alert("Sending the email failed. Please try again.");
		else if (res.error.Type = "System.Exception") {
			error.style.display = "block";
			error.innerHTML = res.error.Message;
		}
	}
	else {
		error.style.display = "none";
		alert("Password reset email has been sent to email address tied to this username.");
		StartFade(-1000, "overlay_ui");
	}
}

function tbUsernameFP_Keypress(event)
{
	if (event.keyCode == 13)
	{
		btnGetPassword_Click();
		return false;
	}
}

function IgnoreEnter(event)
{
	if (event.keyCode == 13)
		return false;
}

function ShowSysMessage(header, message)
{
	//hide password retrieval
	document.getElementById('forgotPassword').style.display = "none";
	//show sys message
	document.getElementById('systemMessage').style.display = "block";
	//set sys message elements
	document.getElementById('sys_title').innerHTML = header;
	document.getElementById('sys_message').innerHTML = message;
	//start fade in
	StartFade(1000, 'overlay_ui');
}

function CloseSysMessage()
{
	//hide sys message
	document.getElementById('systemMessage').style.display = "none";
	//show password retrieval
	document.getElementById('forgotPassword').style.display = "block";
	//start fade out
	StartFade(-1000, 'overlay_ui');
}
