/* ************************************************************************
    Javascript Misc Functions

    Andy Flisher - dotUK

    andy@dotuk.net
    www.dotuk.net
************************************************************************ */

/*
	CSS & Visual Effects
*/



function checkPasswords(sUrl,element,element2)
{
	//$.getJSON("$sysHomeUrl/custom/inc_custom_ajax.func.php", {sMethod: "checkUserAvail", sRecord: $(this).val()}, function(z)
	var tmpStatus=false;
	if($(element).val()==$(element2).val())
	{
		tmpStatus=true;
	}
	
	if(tmpStatus==false)
	{
		$(element).addClass("inputError");
		alert('Sorry, passwords do not match');
		$(element).focus();
	}	
	return tmpStatus;
}

function checkUsername(sUrl,element)
{
	//$.getJSON("$sysHomeUrl/custom/inc_custom_ajax.func.php", {sMethod: "checkUserAvail", sRecord: $(this).val()}, function(z)
	var tmpStatus=false;
	$.ajax({
		type: 'GET',
		url: sUrl+'/custom/inc_custom_ajax.func.php?sMethod=checkUserAvail&sRecord='+$("input#UserName").val(),
		dataType: 'json',
		async: false,
		success: function(z)
		{
			if(z[0].Status=="FAIL")
			{
				tmpStatus=false;
			}else{
				tmpStatus=true;
			}
		},
		error: function(oRequest, oError)
		{
			alert('There has been an error, please try again or seek support');
		}
	});
	
	if(tmpStatus==false)
	{
		$(element).addClass("inputError");
		alert('Sorry, that username is unavailable');
		$(element).focus();
	}	
	return tmpStatus;
}
		
function resetErrorList(sElement)
{
	$(sElement).hide();
	$(sElement+' ul').html('<li class="hidden"></li>');
	return;
}

function doParmoCalc(sUrl)
{
	
	$("#sOverride").val("Ajax");
	var aForm = $("#parmocalcform").serialize();
	$("div#ParmoOverContent").load(sUrl, aForm, function()
	{			

	
	});
}

function doParmoReset(sUrl)
{
	sUrl=sUrl+'?sOverride=Ajax&bOutput=0';
	$("div#ParmoOverContent").load(sUrl);
}

function doSwapActive(sRequestAction, sMethod, sMode, iID)						//Assume standard naming convention of DOM elements					
{	
	$.ajax({
		type: 'GET',
		url: sRequestAction,
		dataType: "json",
		timeout: 5000,
		success: function(oResponse)
		{			
			if(oResponse.Status=="OK")		//Happy Place
			{
				if($("#tr_"+iID+":not(:hidden)"))		//Weird IE / 1.4.2 reasons why toggle and class swapping not working!
				{
					$("#tr_"+iID).hide();
				}else{
					$("#tr_"+iID).show();
				}
				
				if(sMode=='ACT')
				{
					//Reverse function of the originally clicked request
					sRequestAction=sRequestAction.replace('sAction=Activate', 'sAction=DeActivate');
					var sHTML='<a id="aDeActivate_'+iID+'" class="aDeActivate'+sMethod+'" href="'+sHomeUrl+'" onclick="return false;" title="DeActivate '+sMethod+'"><img src="'+sHomeUrl+'/images/icons/disconnect.png" class="icon" title="DeActivate '+sMethod+'"/>DeAct.<a/>';
					$("#td_swap_"+iID).html(sHTML);	
					$("#aDeActivate_"+iID).bind('click', function(){
						doSwapActive(sRequestAction, sMethod, 'DEACT', iID);
					});
				}else{
					//Reverse function of the originally clicked request
					sRequestAction=sRequestAction.replace('sAction=DeActivate', 'sAction=Activate');
					var sHTML='<a id="aActivate_'+iID+'" class="aActivate'+sMethod+'" href="'+sHomeUrl+'" onclick="return false;" title="Activate '+sMethod+'"><img src="'+sHomeUrl+'/images/icons/connect.png" class="icon" title="Activate '+sMethod+'"/>Act.<a/>';
					$("#td_swap_"+iID).html(sHTML);	
					$("#aActivate_"+iID).bind('click', function(){
						doSwapActive(sRequestAction, sMethod, 'ACT', iID);
					});
				}
			}else{					//invalid Login	
				alert('There has been an error, please try again');
			}
		},
		error: function()
		{
			alert('There has been an error, please try again or seek support');
		}
	});
	return false;
}

function doFileUpload(sElementName, sFormAction)
{
	$("#Submit").hide();
	$("#PopOverLoadClose").hide();
	$("#LoadingHolder").show();

	//alert(sElementName+' '+sFormAction);
	$.ajaxFileUpload
	({
		url: sFormAction,
		secureuri:false,
		fileElementId: sElementName,
		dataType: 'json',
		success: function(oResponse)
		{
			$("#Submit").show();
			$("#PopOverLoadClose").show();
			$("#LoadingHolder").hide();
			if(oResponse.Status=="OK")		//Happy Place
			{
				$("#FormMsg").hide();
				doPostSuccess(oResponse.iRecord);						//This must be defined, eg in dynamic functions
			}else{					//invalid 
				$("#FormMsg ul").html("<li>"+oResponse.error+"</li>");
				$("#FormMsg").show();
				$("#FormMsgList").show();
			}
		},
		error: function(oRequest, oError)
		{
			if(oRequest.status==200)			//Probably an ajax call on the direct page
    		{
    			alert("Successfully Uploaded");
    			return false;
    		}else{
				alert('There has been an error, please try again or seek support');
				$("#Submit").show();
				$("#FormMsgList").show();
				$("#LoadingHolder").hide();
			}
		}
	});

	
	return false;

}

function doPost(sFormName, sFormAction, sFormMethod)									//Assumes standard object id's eg Submit, PopOverloadClose, LoadingHolder etc
{
	$("#Submit").hide();
	$("#PopOverLoadClose").hide();
	$("#LoadingHolder").show();
	
	$("#sOverride").val("Ajax");
	var aForm = $("#"+sFormName).serialize();
	$.ajax({
		type: sFormMethod,
		url: sFormAction,
		data: aForm,
		dataType: "json",
		timeout: 5000,
		success: function(oResponse)
		{
			$("#Submit").show();
			$("#PopOverLoadClose").show();
			$("#LoadingHolder").hide();
			if(oResponse.Status=="OK")		//Happy Place
			{
				$("#FormMsg").hide();
				doPostSuccess(oResponse.iRecord);						//This must be defined, eg in dynamic functions
			}else{					//invalid 
				$("#FormMsg ul").html("<li>"+oResponse.Status+"</li>");
				$("#FormMsg").show();
				$("#FormMsgList").show();
			}
		},
		error: function()
		{
			alert('There has been an error, please try again or seek support');
			$("#Submit").show();
			$("#FormMsgList").show();
			$("#LoadingHolder").hide();
		}
	});
	
	return false;
}

function prepLogin()
{
	$("#aLogin").click(function(){PopOverLoad('OpaqueGrey', 'PopOverLogin', 350, 450, 'PopOver',sHomeUrl+'/login.php?sOverride=Ajax&bOutput=0', 1)});
	$("#aFooterLogin").click(function(){PopOverLoad('OpaqueGrey', 'PopOverLogin', 350, 450, 'PopOver',sHomeUrl+'/login.php?sOverride=Ajax&bOutput=0', 1)});
}

function prepLogout()
{
	$("#aLogout").click(function(){PopOver('OpaqueGrey', 'PopOverLogout', 150, 400, 'PopOver', 1)});
	$("#aFooterLogout").click(function(){PopOver('OpaqueGrey', 'PopOverLogout', 150, 400, 'PopOver', 1)});
	$("#aCloseConfirmLogout").click(function()
	{
		$("div#PopOverLogout").fadeOut();
		$("div#OpaqueGrey").fadeOut();
	});
	$("#aConfirmLogout").click(function()
	{
		doLogout();	
	});
}

function prepRegister()
{
	$("#RegSubmit").click(function()
	{
		
	});
}

function prepJourney()	//Add journey
{
	$("#aAddJourney").click(function()
	{
		PopOverLoad('OpaqueGrey', 'PopOverLoad', 500, 600, 'PopOver',sHomeUrl+'/addjourney.php?sOverride=Ajax&bOutput=0', 0);
	});
}

function doPromptLogin()	//Clicked Add journey but not signed in
{
	$("#aAddJourney").click(function()
	{
		$("#aLogin").click();	//Emulate clicking on Login link, and thus initiating login / register pop up
		return false;
	});
}

function doRegister()		//Registration Popover (from the Login PopOver)
{
	
	$("#sOverride2").val("Ajax");
	$("#bOutput2").val(0);
	var aForm = $("#Registerform").serialize();

	var sUrl=sHomeUrl+'/register.php';
	
	ResizePopOver('PopOverLogin', ($(window).height()*.9), ($(window).width()*.9));	
	
	$("div#PopOverLogin").load(sUrl, aForm);
}

function doRegistration()			//Login or Register PopOver
{
	$("#RegSubmit").hide();
	$("#PopOverLoginClose").hide();
	$("#LoadingHolder").show();
	
	$("#sOverride").val("Ajax");
	var aForm = $("#Registerform").serialize();
	$.ajax({
		type: "POST",
		url: "register.php",
		data: aForm,
		dataType: "json",
		timeout: 5000,
		success: function(oResponse)
		{
			$("#RegSubmit").show();
			$("#PopOverLoginClose").show();
			$("#LoadingHolder").hide();
			if(oResponse.Status=="OK")		//Happy Place
			{
				$("#FormMsg").hide();
				window.location = sHomeUrl+"/myprofile.php?newreg=1";
			}else{					//invalid 
				$("#FormMsg ul").html("<li>Registration Failed - Please Retry</li>");
				$("#FormMsg").show();
				$("#FormMsgList").show();
			}
		},
		error: function(oRequest, oError)
		{
			if(oRequest.status==200)			//Probably an ajax call on the direct page
    		{
    			window.location = sHomeUrl+"/redirect.php?target=newprofile";
    			return false;
    		}else{
				alert('There has been an error, please try again or seek support');
				$("#Submit").show();
				$("#FormMsgList").show();
				$("#LoadingHolder").hide();
			}
		}
	});
	return false;
}

function doLogin()			//Login or Register PopOver
{
	$("#Submit").hide();
	$("#PopOverLoginClose").hide();
	$("#LoadingHolder").show();
	
	$("#sOverride").val("Ajax");
	var aForm = $("#Loginform").serialize();
	$.ajax({
		type: "POST",
		url: "login.php",
		data: aForm,
		dataType: "json",
		timeout: 5000,
		success: function(oResponse)
		{;
			$("#Submit").show();
			$("#PopOverLoginClose").show();
			$("#LoadingHolder").hide();
			if(oResponse.Status=="OK")		//Happy Place
			{
				$("#FormMsg").hide();
				window.location = sHomeUrl+"/redirect.php?target=profile";
			}else{					//invalid 
				$("#FormMsg ul").html("<li>Login Failed - Please Retry</li>");
				$("#FormMsg").show();
				$("#FormMsgList").show();
			}
		},
		error: function(oRequest, oError)
		{
			//alert("readyState: "+oRequest.readyState+"\nstatus: "+oRequest.status);
    		//alert("responseText: "+oRequest.responseText);
    		if(oRequest.status==200)			//Probably an ajax call on the direct page
    		{
    			window.location = sHomeUrl+"/redirect.php?target=profile";
    			return false;
    		}else{
				alert('There has been an error, please try again or seek support');
				$("#Submit").show();
				$("#FormMsgList").show();
				$("#LoadingHolder").hide();
			}
		}
	});
	return false;
}

function doForgot()			//Login or Register PopOver
{
	$("#Submit").hide();
	$("#PopOverLoginClose").hide();
	$("#LoadingHolder").show();
	
	$("#sOverride").val("Ajax");
	var aForm = $("#Forgotform").serialize();
	$.ajax({
		type: "POST",
		url: "forgot.php",
		data: aForm,
		dataType: "json",
		timeout: 5000,
		success: function(oResponse)
		{;
			$("#Submit").show();
			$("#PopOverLoginClose").show();
			$("#LoadingHolder").hide();
			if(oResponse.Status=="OK")		//Happy Place
			{
				$("#FormMsg").hide();
				window.location = sHomeUrl+"/redirect.php?target=forgotmessage";
			}else{					//invalid 
				$("#FormMsg ul").html("<li>Reminder Failed - Please Retry</li>");
				$("#FormMsg").show();
				$("#FormMsgList").show();
			}
		},
		error: function(oRequest, oError)
		{
			//alert("readyState: "+oRequest.readyState+"\nstatus: "+oRequest.status);
    		//alert("responseText: "+oRequest.responseText);
    		if(oRequest.status==200)			//Probably an ajax call on the direct page
    		{
    			window.location = sHomeUrl+"/redirect.php?target=forgotmessage";;
    			return false;
    		}else{
				alert('There has been an error, please try again or seek support');
				$("#Submit").show();
				$("#FormMsgList").show();
				$("#LoadingHolder").hide();
			}
		}
	});
	return false;
}

function doJourney()			//Log Journey
{
   
	$("#Submit").hide();
	$("#SubmitRepeat").hide();
	$("#PopOverLoginClose").hide();
	$("#LoadingHolder").show();
	
	$("#sOverride").val("Ajax");
	var aForm = $("#Journeyform").serialize();
	$.ajax({
		type: "POST",
		url: "addjourney.php",
		data: aForm,
		dataType: "json",
		timeout: 5000,
		success: function(oResponse)
		{;
			$("#Submit").show();
			$("#SubmitRepeat").show();
			$("#PopOverLoginClose").show();
			$("#LoadingHolder").hide();
			if(oResponse.Status=="OK")		//Happy Place
			{
				$("#FormMsg").hide();
				window.location = sHomeUrl+"/redirect.php?target=profile";
    			return false;
    		}else if(oResponse.Status=="REDO"){
				$("#FormMsg").hide();
				//Blank Defaults
				$("#JourneyDistance").val('');
				$("#JourneyTime").val('');
				$("#JourneyDistance").focus();
			}else{					//invalid 
				$("#FormMsg ul").html("<li>Could Not log Journey - Please Retry</li>");
				$("#FormMsg").show();
				$("#FormMsgList").show();
			}
		},
		error: function(oRequest, oError)
		{
    		if(oRequest.status==200)			//Probably an ajax call on the direct page
    		{
    			window.location = sHomeUrl+"/redirect.php?target=profile";
    			return false;
    		}else{
				alert('There has been an error, please try again or seek support');
				$("#Submit").show();
				$("#SubmitRepeat").show();
				$("#FormMsgList").show();
				$("#LoadingHolder").hide();
			}
		}
	});
	return false;
}

function doLogout()
{
	window.location = sHomeUrl+"/logout.php";
}

function ResizePopOver(sDivPopOver, iHeight, iWidth)
{
	iCurrentHeight=$("div#"+sDivPopOver).height(); 
	iCurrentWidth=$("div#"+sDivPopOver).width();
	
	//IE can't comprehend that a border is *outside* the element
	if(!$.support.opacity)
	{
		iHeight=iHeight+($("div#"+sDivPopOver).outerHeight()-$("div#"+sDivPopOver).height());  //basically outer - inner = border! 
		iWidth=iWidth+($("div#"+sDivPopOver).outerWidth()-$("div#"+sDivPopOver).width());
	}

	var iTop=1-(iHeight/2);
	var iLeft=1-(iWidth/2);
	
	$("div#"+sDivPopOver).animate({height: iHeight, marginTop: iTop, width: iWidth, marginLeft: iLeft},500, function () {});

}

function PopOver(sDivContainer, sDivPopOver, iHeight, iWidth, sIEClass)
{
	//Assumes Jquery
	$(document).ready(function()
	{
		//IE can't comprehend that a border is *outside* the element
		if(!$.support.opacity)
		{
			iHeight=iHeight+($("div#"+sDivPopOver).outerHeight()-$("div#"+sDivPopOver).height());  //basically outer - inner = border! 
			iWidth=iWidth+($("div#"+sDivPopOver).outerWidth()-$("div#"+sDivPopOver).width());
		}
	
		//Specifically Set Height and Width
		$("div#"+sDivPopOver).height(iHeight);
		$("div#"+sDivPopOver).width(iWidth);
		$("div#"+sDivPopOver).css('margin-top', 1-(iHeight/2));
		$("div#"+sDivPopOver).css('margin-left', 1-(iWidth/2));
	
		//IE doesn't support opacity
		if(!$.support.opacity)
		{
			//Rounded Corners for IE
			ie_rounded.addRule('.'+sIEClass, '5px');
			
			$("div#"+sDivContainer).show("fast");				//Do without fade to prevent jaggies
			$("div#"+sDivPopOver).show("fast");
		}else{
		
			$("div#"+sDivContainer).fadeTo("slow",0.4);
			$("div#"+sDivPopOver).fadeTo("slow",1.0);
		};
		
		$("div#"+sDivContainer).click(function()
		{
			$(this).fadeOut();
			$("div#"+sDivPopOver).fadeOut();
		});
		
		$("img#"+sDivPopOver+"Close").click(function()							/*Assumes an <img> tag with id same as sDivPopover but with 'Close' suffix*/
		{
			$("div#"+sDivContainer).fadeOut();
			$("div#"+sDivPopOver).fadeOut();
		});
	});
}

function PopOverLoad(sDivContainer, sDivPopOver, iHeight, iWidth, sIEClass, sUrl, bReload)
{
	if(bReload==null)
	{
		var bReload=0;
	}

	//Assumes Jquery
	$(document).ready(function()
	{
		//IE can't comprehend that a border is *outside* the element
		if(!$.support.opacity)
		{
			iHeight=iHeight+($("div#"+sDivPopOver).outerHeight()-$("div#"+sDivPopOver).height());  //basically outer - inner = border! 
			iWidth=iWidth+($("div#"+sDivPopOver).outerWidth()-$("div#"+sDivPopOver).width());
		}

		//Specifically Set Height and Width and then reset margins
		$("div#"+sDivPopOver).height(iHeight);
		$("div#"+sDivPopOver).width(iWidth);
		$("div#"+sDivPopOver).css('margin-top', 1-(iHeight/2));
		$("div#"+sDivPopOver).css('margin-left', 1-(iWidth/2));

		//Assume Container Div is sDivPopOver wirth 'Content' suffix
		$("div#"+sDivPopOver+"Content").load(sUrl, function(){
			
			$("div#"+sDivPopOver+"Wait").fadeOut(); 
			//IE doesn't support opacity
			if(!$.support.opacity)
			{
				ie_rounded.addRule('.'+sIEClass, '5px');
				
				$("div#"+sDivContainer).show("fast");				//Do without fade to prevent jaggies
				$("div#"+sDivPopOver).show("fast");
			}else{
			
				$("div#"+sDivContainer).fadeTo("slow",0.4);
				$("div#"+sDivPopOver).fadeTo("slow",1.0);
			};
			
		});
		
		$("div#"+sDivContainer).click(function()
		{
			$("div#"+sDivContainer).fadeOut();
			$("div#"+sDivPopOver).fadeOut();
			if(bReload==1)
			{
				location.reload(true);
			}
		});
		
		$("img#"+sDivPopOver+"Close").click(function()							/*Assumes an <img> tag with id same as sDivPopover but with 'Close' suffix*/
		{
			$("div#"+sDivContainer).fadeOut();
			$("div#"+sDivPopOver).fadeOut();
			if(bReload==1)
			{
				location.reload(true);
			}
		});
	});
}


