var contents1="";
var contents3="";

$(document).ready(function() {
	$("#alert-bar").delay(400).animate({opacity: "show"}, 650);
	contents1 = $("#contents-1").html();
	contents3 = $("#contents-3").html();
	/*$(window).bind("beforeunload",function() {
		$.post('logout.php', function(data) { } );	
	});*/	

});
/*
window.onunload = function() {
	$.post('logout.php', function(data) { } );	
};
*/
function logout() {
	$.post('logout.php', function(data) { } );	 
 //var str = $.cookie("process-4");
 //alert(str.substring(0, str.indexOf("$")) );
}

function roll(o) {
	var src, ftype, newsrc;
	src = document.getElementById(o).src;
 	ftype = src.substring(src.lastIndexOf('.'), src.length);
 	if(/_a/.test(src)) {
		newsrc = src.replace('_a','');
 	} else {
		newsrc = src.replace(ftype,'_a'+ftype);
 	}
	document.getElementById(o).src = newsrc;
}

function loading(id) {

document.getElementById(id).innerHTML="<img src='images/loading.gif' alt='Logging into Strategy Institute File Server' title='Logging into Strategy Institute File Server' />";
id="#"+id;

}

function highlight(id) {

id = "#"+id;
$(id).css("border-bottom-color","#04b692");

}

function activateAdmin() {
	var adminInterface = "<form action='admin_process.php' method='post' onsubmit='return validate_form(this);'>" +
						"<table width='100%' cellpadding='0' cellspacing='0' border='0'>" +
						"<tr><td>Admin ID:</td><td><input type='text' width='100%' name='adminID' id='adminID' /></td></tr>" +
						"<tr><td>Password:</td><td><input type='password' width='100%' name='password' id='password'/></td></tr>" +
						"<tr><td colspan='2' height='10'></td></tr>" +
						"<tr><td colspan='2' align='right'><input type='submit' width='100'/></td></tr>" +
						"</table></form>"
						
	$("#adminImg").attr("src","images/login/login_03_a.jpg");
	$("#userImg").attr("src","images/login/login_02_a.jpg");
	$("#login-box").html(adminInterface);
}

function activateUser() {
	var userInterface = "<form action='process.php' method='post' onsubmit='return validate_form(this)'>" +
						"<table width='100%' cellpadding='0' cellspacing='0' border='0'>" +
						"<tr><td>User ID:</td><td><input type='text' width='100%' name='userID' id='userID' /></td></tr>" +
						"<tr><td>Password:</td><td><input type='password' width='100%' name='password' id='password'/></td></tr>" +
						"<tr><td colspan='2' height='10'></td></tr>" +
						"<tr><td colspan='2' align='right'><input type='submit' width='100'/></td></tr>" +
						"</table></form>"
	$("#adminImg").attr("src","images/login/login_03.jpg");
	$("#userImg").attr("src","images/login/login_02.jpg");
	$("#login-box").html(userInterface);
}

function validate_required(field)
{
    with (field)
    {
        if (value==null||value=="")
        	return false;
        else {return true}
    }
}

function validate_email(field)
{
	with (field)
	{
		var apos=value.indexOf("@");
		var dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
  			return false;
		else return true;
	}
}

function validate_form(thisform)
{
    $("#userID").css('background-color','#ffffff');
	$("#password").css('background-color','#ffffff');
	with (thisform)
    {
        if (validate_required(userID) == false)
        {
            document.getElementById("form-alert").innerHTML ="* Username required";
			document.getElementById("userID").focus();
			$("#userID").css('background-color','#ecec62');
            return false;
        }
		
		if (validate_required(password) == false)
        {
            document.getElementById("form-alert").innerHTML ="* Password required";
			document.getElementById("password").focus();
			$("#password").css('background-color','#ecec62');
            return false;
        }
		
		return true;

	}
	return false;
}


/* ================================ FeedBack Panel ================================ */
function showAdminUpdate(id) {
	var offset = $(id).offset();
	var offsetY = offset.top - 50;
	offsetY = Math.floor(offsetY) + "px";
	
	var height = $(document).height()+"px";
	
	$("#admin-update").fadeIn(500);
	$("#admin-update").css("height",height);
	$("#update-box").css("margin-top",offsetY);	
}

function showFeedBackPanel() {
	$("#contents-1").html(contents1);
	$("#contents-3").html(contents3);
	$("#captcha").html("");
	$("#contents-1").show();
	$("#contents-2").show();
	$("#contents-3").show();
	$("#contents-4").hide();
	Recaptcha.reload();	
	$("#feedback-panel").fadeIn(200).css("height",$(window).height());
	$("#feedback-main").animate({left: ['+=15%','swing'],opacity: 'toggle'},600).animate({left: ['-=20','swing']},200).animate({left: ['+=20','swing']},400);
	return false;
}

function hideFeedBackPanel() {
	$("#feedback-main").animate({left: ['+=5%','swing']},400).animate({left: ['-=20%','swing'],opacity: 'toggle'},300);
	$("#feedback-panel").fadeOut(200);
}

function validateFeedBack(thisform) {
	$("input").css("background-color","#FFFFFF");
	with (thisform) {
		if(!validate_required(fd_name)) {
			$("#fd_name_txt").html("Name required");
			$("#fd_name").focus();
			$("#fd_name").css('background-color','#d8e5f7');
			
		}
		else if (!validate_email(fd_email)) {
			$("#fd_email_txt").html("A valid Email is required");
			$("#fd_email").focus();
			$("#fd_email").css('background-color','#d8e5f7');
		}
		else if (!validate_required(fd_subject)) {
			$("#fd_subject_txt").html("Subject required");
			$("#fd_subject").focus();
			$("#fd_subject").css('background-color','#d8e5f7');

		}
		else if (!validate_required(fd_contents)) {
			$("#fd_contents_txt").html("Comments or questions must be provided.");
			$("#fd_contents").focus();
			$("#fd_contents").css('background-color','#d8e5f7');

		}
		else {
			$.ajax({
				type: "POST",
				url: "FS_send_feedback.php",
				data: "name="+$("#fd_name").val()+"&email="+$("#fd_email").val()+"&phone="+$("#fd_tel").val()+"&subject="+$("#fd_subject").val()+"&message="+$("textarea#fd_contents").val()+"&recaptcha_response_field="+$("#recaptcha_response_field").val()+"&recaptcha_challenge_field="+$("#recaptcha_challenge_field").val(),
				success: function(html) {
					
					if(html == "0\n") {
						$("#captcha").html("The reCAPTCHA wasn't entered correctly. Please try it again.");
						Recaptcha.reload();	
					}
					else if (html == "1\n") {
						$("#contents-4").html("<p style='font-weight:bold; font-size=120%'>Feedback submitted successfully.<br>We will get back to you as soon as possible.</p><p align='center'><input type='button' value='Ok' onclick='hideFeedBackPanel();' /></p>");
						$("#contents-1").hide();
						$("#contents-2").hide();
						$("#contents-3").hide();
						$("#contents-4").show();
					}
					
				}
			});
		}
		
		return false;
	}
	
	return false;
}
