// Miscellaneous generic javascript functions that do not use cookies

// Part of the benwillies web application development framework
// available for download from http://www.benwillies.com

// You are free to use, distribute and modify this code without
// restriction but you may not prevent others from enjoying the 
// same privileges even if you have changed something

//  If requestName is not specified then 'request' will be the default 
//  as in 'request.responseText' but if two different requests could
//  be active at the same time, then to avoid a conflict use two
//  request objects by specifying a different name for each one...
//      sendRequest(url1, 'callbackFunctionName1', 'requestName1';
//      sendRequest(url2, 'callbackFunctionName2', 'requestName2';
//  and then inside the callback functions...
//      var results1 = requestName1.responseText;
//      var results2 = requestName2.responseText;
//  but be sure to define the request names as global variables...
//      var requestName1, requestName2;

var request;

function createRequest(requestName) {
    try {
        var requestObject = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            var requestObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                var requestObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                var requestObject = false;
            }
        }
    }
    if (!requestObject) {
        alert("Your browser is not supported.  Please contact the webmaster for more information.");
        return;   
    }
    if (typeof(requestName) == 'undefined' || !requestName) 
        requestName = 'request';        
    eval(requestName + ' = requestObject');      
}

function sendRequest(url, functionName, requestName) {
    if (typeof(url) == 'undefined' || !url) {
        alert("sendRequest: URL not specified");   
        return false;
    }
    if (typeof(functionName) == 'undefined' || !functionName) {
        alert("sendRequest: Callback function not specified");   
        return false;
    }
    if (typeof(requestName) == 'undefined' || !requestName)
        requestName = 'request';
    try {    
        eval('var requestObject = ' + requestName);
    } catch (error) {
        alert("sendRequest: '" + requestName + "' is not a global variable");
        return;
    }
    if (typeof(requestObject) != 'object')   
        createRequest(requestName); 
    eval('var requestObject = ' + requestName); 
    if (typeof(requestObject) != 'object')
        return false;
    var components = url.split('?');
    requestObject.open("POST", components[0], true);
    requestObject.onreadystatechange = function() {
        if (requestObject.readyState == 4) {
            // alert(requestObject.getAllResponseHeaders() + "\nstatus code: " + requestObject.status); 
            if (requestObject.status == 200 || typeof(requestObject.status) == 'undefined')
                eval(functionName + '.call()');
            else if (requestObject.status == 404)
                alert("sendRequest: URL is invalid or does not exist");
            else
                alert("sendRequest: Server returned status code " + requestObject.status);
        }
    }    
    requestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    requestObject.send(components[1]);
}

function searchArray(array, string) {
    var i;
    for (i=0;i<array.length;i++) {
        if (array[i] == string)
            return i;
    }
    return -1;
}

function checkRange(pName, pValue, pMinimum, pMaximum, pPopup, pOut) {
	var out = ''
	pValue = pValue.replace(/\$/g, '')
	pValue = pValue.replace(/\,/g, '')
	if (!isNaN(pValue + 0)) {
		decimal = pValue.indexOf('.')
		if (decimal > 0) 
			pValue = pValue.substring(0,decimal)
		out = pValue * 12
		out = out.toString()
		if (out.length > 3)
			out = out.substring(0,(out.length - 3)) + ',' + out.substring(out.length - 3, out.length)
		out = '&nbsp;&nbsp;($' + out + ' per year)'
		if (typeof(pMinimum) == 'undefined')
			pMinimum = '';
		if (typeof(pMaximum) == 'undefined')
			pMaximum = '';
		if ((pMinimum > '' && pMinimum > pValue) || (pMaximum > '' && pMaximum < pValue)) {
			if (typeof(pPopup) != 'undefined') 
				popup(pPopup,'SHOW')
		}
	}	
	if (typeof(pOut) != 'undefined')
		document.getElementById(pOut).innerHTML = out;
}

function popup(pName, pAction, pValue) {
	styleObject = document.getElementById(pName).style
	if (pAction == 'HIDE') {
		styleObject.visibility = 'hidden'
		if (typeof(window.PopupTimer) != 'undefined') 
			clearTimeout(PopupTimer)		
	} else if (!pValue) {
		styleObject.visibility = 'visible'
//		PopupTimer = setTimeout("popup(pName,'HIDE',1)",30)
	}	
}

function setFocus(pForm, pField) {
	document.forms[pForm].elements[pField].focus()
	document.forms[pForm].elements[pField].select()			
}

function setStyle(pName, pStyle, pValue) {
	var styleObject = document.getElementById(pName).style		
	if (typeof(styleObject) != 'undefined') {	
		switch (pStyle) {
			case "fontSize" :
				styleObject.fontSize = pValue;
				break;
			case "fontColor" :
				styleObject.fontColor = pValue;
				break;
			case "fontFamily" :
				styleObject.fontFamily = pValue;
				break;
			case "backgroundImage" :			
				styleObject.backgroundImage = 'URL("' + pValue + '")';
				break;
			case "backgroundColor" :
				styleObject.backgroundColor = pValue;
				break;
			case "borderTopColor" :
				styleObject.borderTopColor = pValue;
				break;
			case "borderRightColor" :
				styleObject.borderRightColor = pValue;
				break;
			case "borderBottomColor" :
				styleObject.borderBottomColor = pValue;
				break;
			case "borderLeftColor" :
				styleObject.borderLeftColor = pValue;
				break;
			case "borderColor" :
				styleObject.borderTopColor = pValue;
				styleObject.borderRightColor = pValue;
				styleObject.borderBottomColor = pValue;
				styleObject.borderLeftColor = pValue;
				break;
			case "textTransform" :
				styleObject.textTransform = pValue;
				break;
			case "visibility" :			
				styleObject.visibility = pValue;
				break;
			case "display" :
				styleObject.display = pValue;
				break;
			default :
		}
	}			
}

function openWindow(pURL,pWindowName,pWidth,pHeight,pAttributes) {
	var max_width = screen.availWidth - 64
	var max_height = screen.availHeight - 64
	if (typeof(pAttributes) == 'undefined' || !pAttributes) {
		if (pWidth > max_width || pHeight > max_height) {		
			pAttributes = 'scrollbars=yes,resizable=yes,toolbar=no'
		} else {
			pAttributes = 'scrollbars=no,resizable=no,toolbar=no'
		}		
	}
	var screenX, screenY;
	if (pWidth > max_width) pWidth = max_width
	if (pHeight > max_height) pHeight = max_height
	if (pAttributes.indexOf('screen') == -1) {
		screenX = Math.floor((screen.availWidth - pWidth) / 2)
		if (screenX < 0) screenX = 0
		screenY = Math.floor((screen.availHeight - pHeight) / 4)
		if (screenY < 0) screenY = 0
		pAttributes += ',screenX=' + screenX + ',left=' + screenX + ',screenY=' + screenY + ',top=' + screenY
	}		
	newWindow = window.open(pURL,pWindowName,'width=' + pWidth + ',height=' + pHeight + ',' + pAttributes)
	newWindow.focus()		
}
