// Copyright (c) 2009 TheyerGFX Pty Ltd 


// is this an iphone?
function isiPhone() {
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		return true;
	}
	return false;
}

// open a new window
function openWindow( url ) {
	window.open( url );
}


// open a window
function openMobileSimulator( url, width, height ) {
	width  = width  + 17;
	height = height + 17;
	window.open( url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=no, scrollbars=yes, width=' + width + ', height=' + height );
}


// download a file
function downloadFile( url ) {
	window.open( url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=no, width=300, height=100' );
	window.status = 'loading...';
}


// open a fixed size window
function openBareWindow( url, width, height ) {
	window.open( url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=no, width=' + width + ', height=' + height );
	window.status = 'loading...';
}


// swap to the highlighted version of the label
function highlight(e) {
	if ( e.tagName == "IMG" ) {
		var url, name, regexp;
		//window.event.srcElement.hspace = 2;
		//window.event.srcElement.vspace = 2;
		url = e.src;
		regexp = /.gif$/i;
		name = url.replace(regexp,"_h.gif");
		e.src = name;
	}
}


// swap to the normal version of the label
function normal(e) {
	if ( e.tagName == "IMG" ) {
		var url, name, regexp;
		//window.event.srcElement.hspace = 0;
		//window.event.srcElement.vspace = 0;
		url = e.src;
		regexp = /_h.gif/i;
		name = url.replace(regexp,".gif");
		e.src = name;
	}
}


// reload content via AJAX
function reloadContent(element,url)
{
	var xmlhttp = initAjax();
	if (xmlhttp != null) {
	    
        // init
    	sendAjax(xmlhttp,url,element);
        
	} else {
	
	    // failed
		alert('failed');
	}
}


// async AJAX callback
function ajaxCallback(xmlhttp,parent) {
    	
	if (xmlhttp.readyState==4) {
	
	    // is the response ok?
        if (xmlhttp.status==200) {
        
            // update the element
            var element = document.getElementById(parent);
            if (element != null) {
                element.innerHTML = xmlhttp.responseText;
            } else {
                alert('cannot find ' + parent );
            }
            
        } else {
        
            // oops
            alert('request status = ' + xmlhttp.status);
        }
	}
}

var requestn = 0;
function sendAjax(xmlhttp,url,parent)
{
    if (xmlhttp != null) {
        
        // init
		requestn = requestn + 1;
        url = url + '?requestnum=' + requestn;

        // init
        xmlhttp.onreadystatechange= function() { ajaxCallback(xmlhttp,parent); };
		xmlhttp.open("GET",url);
        
        // send the request
        xmlhttp.send(null);
    }
}


function initAjax()
{
    // create the object
    var xmlhttp = null;

    if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
	    try {
		    xmlhttp=new XMLHttpRequest();
       }
       catch (e) {
		    // failed
		    return null;
       }
    } else if (window.ActiveXObject) { //IE
	    try { 
		    xmlhttp=new ActiveXObject("MSXML2.XMLHTTP");
	    }
	    catch (e) {
		    try {
			    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
		    }
		    catch (e) {
			    // failed
			    return null;
		    }
	    }
	}
	
	return xmlhttp;
}


