// JavaScript Document
function launchTutorials(){
	$("#dimScreen").fadeIn('normal', function(){
								frames['tutorialFrame'].location.href = 'http://admin.bigblackbag.net/client_admin/tutorials/tutorial_intro.html';
								centerElement('tutorialPos');
								$('#tutorialPos,#closeOverlay').show();
								resizeOverlay();
								}
							
							);
}

function resizeOverlay(){
	w = $(window).width();
	//h = $('#content').height();
	h = $(document).height();
	$("#dimScreen").css({height:parseInt(h)+"px",width:parseInt(w)+"px"});
}

function centerElement(theElem){
	vpW = parseInt($(window).width());
	vpH = parseInt($(window).height());
	
	elemW = parseInt($('#' + theElem).outerWidth());
	elemH = parseInt($('#' + theElem).outerHeight());
	
	elemLeft = parseInt(vpW - elemW)/2;
	elemTop = parseInt(vpH - elemH)/2;
	
	topOffset = $(window).scrollTop();
	
	if (theElem == 'loginPos' || theElem == 'popAd'){
	$('#' + theElem).css({top:elemTop, left:elemLeft});
	}else{
	$('#' + theElem).css({top:topOffset + 25, left:elemLeft});
	}
	
}

function closeOverlay(){
	if(frames['tutorialFrame']){
	frames['tutorialFrame'].location.href = 'http://admin.bigblackbag.net/client_admin/tutorials/tutorial_intro.html';
	}
	$('#tutorialPos,#loginPos,#dimScreen,#diagramPos,.misc').hide();
	resizeOverlay();
}

function launchDiagram(img,h,w){
	$("#dimScreen").fadeIn('normal', function(){
								$('#diagramPos').css({'width' : w +'px', 'height' : h + 30 + 'px'});
								$('#diagram').css({'width' : w +'px', 'height' : h + 'px', 'backgroundImage' : 'url(' + img + ')' , 'backgroundRepeat' : 'no-repeat'});
								centerElement('diagramPos');
								$('#diagramPos,#closeOverlay2').show();
								resizeOverlay();
								}
							
							);
}

function launchMisc(miscName){
	$("#dimScreen").fadeIn('normal', function(){
								centerElement(miscName + 'Pos');
								$('#' + miscName + 'Pos').show();
								resizeOverlay();
								}
							
							);
}

function launchLogin(){
	$("#dimScreen").fadeIn('normal', function(){
								resizeOverlay();
								centerElement('loginPos');
								$('#loginPos').show();
								retryLogin();
								}
							
							);
}

//set Form for ajax
function setFormValues(theForm,theTarget){
		var options = { 
			target:        '#' + theTarget,   // target element(s) to be updated with server response 
			beforeSubmit:  showRequest,  // pre-submit callback 
			success: function(data) {
					//showResponse();
	
					if(theForm == 'loginForm'){
					var version = $('#verdict').html();
					doLogin(version);
					}
					
					// rebind to the form's submit event 
						$('#' + theForm).unbind('submit').submit(function() { 
							$(this).ajaxSubmit(options); 
							return false; 
						});
			}
		};

    // bind to the form's submit event 
	$('#' + theForm).unbind('submit').submit(function() { 
    $(this).ajaxSubmit(options)
	return false; 
	});

}

function showRequest(formData, jqForm, options){
	return validateForm(jqForm.attr('id'));
}

function validateForm(theForm){
window.username = '';
window.password = '';
var errMsg = '';

	if(theForm == 'loginForm'){
		if(document.forms[theForm].username.value == ''){
			errMsg = 'A username is required.\n';
		}
		if(document.forms[theForm].password.value == ''){
			errMsg = errMsg + 'A password is required.\n';
		}
		
		if(errMsg != ''){
			alert(errMsg);
			return false;
		}else{
			window.username = document.forms[theForm].username.value;
			window.password = document.forms[theForm].password.value;
			return true;
		}
	}else if(theForm == 'retrieveLoginForm'){
		if(document.forms[theForm].email.value == ''){
			errMsg = 'An email is required.\n';
		}

		if(errMsg != ''){
			alert(errMsg);
			return false;
		}else{
			return true;
		}
	}
		
}

function doLogin(version){
	if(version == 1){
		$('#loginContainer').html('Success! Log-in in process. Please wait.');
		var newURL = "http://www.bigblackbag.net/client_admin/sign_in.asp?Username=" + encodeURI(window.username) + "&Password=" + encodeURI(window.password) + "&impersonate=1";
		window.location=newURL;
	}else if(version == 2){
		$('#loginContainer').html('Success! Log-in in process. Please wait.');
		var newURL = "http://admin.bigblackbag.net/client_admin/sign_in.asp?Username=" + encodeURI(window.username) + "&Password=" + encodeURI(window.password) + "&impersonate=1";
		window.location=newURL;
	}else if (version == 0){
		$('#loginContainer').html('The login failed. <a href="javascript:launchLogin();">Click here</a> to try again.');
	}
	
}

function retryLogin(){
	$('#loginHeading').html('Client Log-in');
	$('#loginContainer').html('<form action="log-in.asp" method="post" name="loginForm" id="loginForm"><table width="100%" border="0" cellspacing="0" cellpadding="3"><tr><td align="right">Username:</td><td><input name="username" id="username" type="text" value=""/></td></tr><tr><td align="right">Password:</td><td><input name="password" id="password" type="password" value=""/></td></tr><tr><td align="center" colspan="2"><a href="javascript:retrieveLogin();"><nobr>Retrieve login info</nobr></a>.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="Log-in" type="submit" value="Log-in"/></td></tr></table></form>');
	setFormValues('loginForm','loginContainer');
}

function retrieveLogin(){
	$('#loginHeading').html('Retrieve Log-in Info');
	$('#loginContainer').html('<form action="retrieve-log-in.asp" method="post" name="retrieveLoginForm" id="retrieveLoginForm"><table width="100%" border="0" cellspacing="0" cellpadding="3"><tr><td align="right">Email:</td><td><input name="email" id="email" type="email" value=""/></td></tr><tr><td align="right" colspan="2"><input name="Log-in" type="submit" value="Retrieve Log-in"/></td></tr></table></form>');
	setFormValues('retrieveLoginForm','loginContainer');

}