/*
 *	AjaxAnywhere overriden functions.
 * 
 *  Graeme Young
 * 	May 2007
 * 
 */

/**
 * 
 * Override prototype.showLoadingMessage
 * 
 * Display the 'Loading...' message, when the ajax request is sent.
 * 
 */
AjaxAnywhere.prototype.showLoadingMessage = function(){
	
	var div = document.getElementById("AA_" + this.id + "_loading_div");
    if (div == null) {
        div = document.createElement("DIV");

        document.body.appendChild(div);
        div.id = "AA_" + this.id + "_loading_div";
		
        div.innerHTML = "<div class=\"raised-type4\" style=\"width: 180px;\">" + 
						"	<b class=\"top\"><b class=\"b1\"></b><b class=\"b2\"></b><b class=\"b3\"></b><b class=\"b4\"></b></b>" +
						"	<div class=\"boxcontent\" style=\"padding-top: 10px; padding-left: 20px\">" +
						"      	<div style=\"float: left;\">" + 
						"			<img src=\"/images/throbber.gif\" alt=\"x\" />" +
						"		</div>" +
						"		<div>" +
						"			<h4 class=\"weka\">&nbsp;&nbsp;Please wait ... </h4>" +
						"			<h5 class=\"weka\">&nbsp;</h5>" +
						"		</div>" +
						"	</div>" +
						"	<b class=\"bottom\"><b class=\"b4b\"></b><b class=\"b3b\"></b><b class=\"b2b\"></b><b class=\"b1b\"></b></b>" +
						"</div>";
                
        div.style.position = "absolute";
        div.style.zIndex = "100";
    }

    div.style.display = "";
    centerDiv(div, 180, 20);
}

/**
 * 
 * Override prototype.hideLoadingMessage
 * 
 * Hide the 'Loading...' message, after the ajax response is received.
 * 
 */
AjaxAnywhere.prototype.hideLoadingMessage = function(){
	
	var div = document.getElementById("AA_" + this.id + "_loading_div");
    if (div != null)
        div.style.display = "none";
}

/**
 * 
 * Center a div element on the screen.
 * 
 * @param divWidth is the width in pixels of the div.
 * @param topOffSet is the number of extra pixels vertically down from 200px
 * 
 */
function centerDiv(divName, divWidth, topOffSet){
	
    var divToDisplay;
    var theTop;
    var theLeft;
    divToDisplay = $(divName);
    if (document.documentElement){
    	
        theTop = document.documentElement.scrollTop;
    }
    else if (document.body){
    	
        theTop = document.body.scrollTop;
    }
    
    theTop += 200;												// div 200px from top
    divToDisplay.style.top = theTop + topOffSet + 'px';
	divToDisplay.style.left = '50%';							// center div in center horizontally
	divToDisplay.style.marginLeft = '-' + divWidth/2 + 'px';
}

/**
 * Override AjaxAnywhere.prototype.handleException (actual sever exception)
 * 
 * @param type = java class exception
 * @param detials = stack trace
 * 
 */
AjaxAnywhere.prototype.handleException = function(type, details) {
	
	alert("wekaworx exception: [" + type + "]\n\nIf this continues please report this to the wekaworx help desk.");
	
	try {
		console.log("Exception from wekaworx server: " + details);
	}
	catch (err){
		// do nothing
	}
}

/**
 * Override AjaxAnywhere.prototype.handleHttpErrorCode
 * 
 * Error handler for ajax request http error codes.
 * 
 */
AjaxAnywhere.prototype.handleHttpErrorCode = function(code) {
    
    var errorMessage = "";
    switch (code){
    	case 400:
    	 	errorMessage = "Bad Request";
    	 	break;
		case 401:
		 	errorMessage = "Unauthorized";
		 	break;
		case 403:
		 	errorMessage = "Forbidden";
		 	break;
		case 404:
		 	errorMessage = "Not Found";
		 	break;
		case 405:
		 	errorMessage = "Method Not Allowed";
		 	break;
		case 406:
		 	errorMessage = "Not Acceptable";
		 	break;
		case 407:
		 	errorMessage = "Proxy Authentication Required";
		 	break;
		case 408:
		 	errorMessage = "Request Timeout";
		 	break;
		case 413:
		 	errorMessage = "Request Entity Too Large";
		 	break;
		case 414:
		 	errorMessage = "Request-URI Too Long";
		 	break;
		case 415:
		 	errorMessage = "Unsupported Media Type";
		 	break;
		case 500:
		 	errorMessage = "Internal Server Error";
		 	break;
		case 502:
		 	errorMessage = "Bad Gateway";
		 	break;
		case 503:
		 	errorMessage = "Service Unavailable";
		 	break;
		case 504:
		 	errorMessage = "Gateway Timeout";
		 	break;
    	
    }
    
    alert("wekaworx server error: HTTP Error code: " + code + "\n\n" + errorMessage);
}

/**
 * Override AjaxAnywhere.prototype.handlePrevousRequestAborted
 * 
 * Error handler for handling an aborted ajax request.
 * 
 */
AjaxAnywhere.prototype.handlePrevousRequestAborted = function() {
	
    alert("wekaworx warning: Previous AJAX request dropped.\n\nPlease wait for the first load to finish.")
}

/**
 * 
 * Do an ajax request to the server, to update the time at the top of each page.
 * 
 */
function ajaxPollServer(){
			
	var today = new Date();
	today.toString().replace(" ", "%20");
	var timezoneOffset = today.getTimezoneOffset();
	var url = document.URL;
	if (url.indexOf("localhost/") != -1){
		
		ajaxAnywhere.getAJAX('http://localhost/ajaxhelper.go?action=AJAX_POLL_SERVER&timezoneOffset=' + timezoneOffset + '&browserDate=' + today, 'ajaxIntDate', false);
	}
	else if (url.indexOf("localhost:8080") != -1){
		
		ajaxAnywhere.getAJAX('http://localhost:8080/ajaxhelper.go?action=AJAX_POLL_SERVER&timezoneOffset=' + timezoneOffset + '&browserDate=' + today, 'ajaxIntDate', false);
	}
	else if (url.indexOf("wekaworx.co.nz") != -1){
		
		ajaxAnywhere.getAJAX('http://www.wekaworx.co.nz/ajaxhelper.go?action=AJAX_POLL_SERVER&timezoneOffset=' + timezoneOffset + '&browserDate=' + today, 'ajaxIntDate', false);
	}
	else if (url.indexOf("wekaworx.com") != -1){
		
		ajaxAnywhere.getAJAX('http://www.wekaworx.com/ajaxhelper.go?action=AJAX_POLL_SERVER&timezoneOffset=' + timezoneOffset + '&browserDate=' + today, 'ajaxIntDate', false);
	}
}

/**
 * 
 * Return true if the zone exists in the zones array.
 * 
 */
function isZoneInList(zones, zoneName){
	
	for (var i = 0 ; i < zones.length; i++){
		//console.log('zones = ' + zones[i].getAttribute("name"));
		if (zones[i].getAttribute("name") == zoneName){
			return true;
		}
	}
	return false;
}

/* script end */

