﻿//---------------------------------------------------------------------------------------------------------------
//ONLOAD - Event:
if (window.onload){
	var funWindowLoad = window.onload;
	window.onload = new Function("funWindowLoad(); Window_OnLoad();");
}
else
	window.onload = Window_OnLoad;
	
//---------------------------------------------------------------------------------------------------------------
function Window_OnLoad(){		
    
		
}
//-----------------------------------------------------------------------------------------------------------------------
var _cellElement = null;
var _lastSortColumn = "R.AnnualRate";
var _lastSortDirection = "asc";

function SortAgentList(cellElement, isReady){    

    if (isReady == null) isReady = false;
    
    if (!isReady){
    
        _cellElement = cellElement;
        cellElement.style.cursor = "wait";
        GetHourGlass().Show("Sorting");
        window.setTimeout("SortAgentList(_cellElement, true);", 1000);
        return;
    
    }   

	//First, figure out the new sort direction:
	var columnName = cellElement.getAttribute("sortdatacolumn");	
	var sortDirection = "asc";
	
	var isIE = (navigator.appName.toLowerCase().indexOf("microsoft") == -1?false:true);
		
	//Find out if the current column is the last column sorted, if it is, then we want to sort in a new
	//direction, else, if this is a different column than the last one sorted, then we sort in ascending
	//order to start with:
	if (_lastSortColumn == columnName){
	
		sortDirection = (_lastSortDirection == "asc"?"desc":"asc");
	
	}	
	
	//Update the module level variables so they can be reference on the next column sort:
	_lastSortColumn = columnName;
	_lastSortDirection = sortDirection;	
	
	//Finally, initiate the Callback request to reload data with new sort order:	
    var resultArray = cbGetSortedAgentList(columnName, sortDirection);     

    //Evaluate the result, if 0, then set the newly sorted agentlist html, if 1, then show exception:
	if (resultArray[0] == "0"){	
	    
	    document.getElementById("divAgentList").innerHTML = resultArray[1];
	    
	}
	else {
	
	    alert(resultArray[1]);
	
	}
	
	//Hide the hourglass:
	GetHourGlass().Hide();
	
	//Turn the cell cursor back to a pointer:
	_cellElement.style.cursor = "pointer";

}
//-----------------------------------------------------------------------------------------------------------------------
function Login(){

	var buttonText = document.frmMain.cmdLogin.value.toLowerCase();
	
	if (buttonText == "logout"){
	
		Logout();
		return;
	
	}	

	//User's Id:
	if (Trim(document.frmMain.txtUid.value) == ""){

		alert("Please enter your email address.");
		document.frmMain.txtUid.focus();
		return;

	}

	if (!IsEmail(Trim(document.frmMain.txtUid.value))){

		alert("Please enter a valid email address.");
		document.frmMain.txtUid.focus();		
		return;
	}
	
	//Password:	
	if (Trim(document.frmMain.txtPwd.value) == ""){

		alert("Please enter your password.");
		document.frmMain.txtPwd.focus();
		return;

	}
	
	document.frmMain.cmdLogin.disabled = true;
	
	var resultArray = cbLogin(escape(document.frmMain.txtUid.value), escape(document.frmMain.txtPwd.value)).split("<cb_col>");	
	
	if (resultArray[0] == "0"){	
		
		var fictionalPage = resultArray[1];		
		
		document.frmMain.cmdLogin.value = "Logout";
	
		document.location.href = fictionalPage;
	
	}
	else {
	
		alert(resultArray[1]);
	
	}	
	
	document.frmMain.cmdLogin.disabled = false;

}
//-----------------------------------------------------------------------------------------------------------------------
function Logout(){	
	
	var result = cbLogout();
	
	if (result == "-1"){
	
		document.frmMain.cmdLogin.value = "Login";
	
		alert("You have successfully logged out.");
	
	}
	else {
	
		alert("The logout failed.  Please try again.");
	
	}	

}
//-----------------------------------------------------------------------------------------------------------------------
function LoadAgentSite(url){		
									
	//Define the left and top coordinates:	
	var width = 599;
	var height = 500;
	var left;
	var top;			
			
	left = (document.body.offsetWidth / 2) - (width / 2) + document.body.scrollLeft;			
	top = 50;
	
	var features = "location=yes,menubar=yes,resizable=yes,toolbar=yes,scrollbars=yes," +
		"top=" + top + ",left=" + left + ",width=" + width + ",height=" + height;

	window.open(url, "", features);

}
//-----------------------------------------------------------------------------------------------------------------------
