// Brilliant AJAX Functionalities

var baseURL = '/iRaceFull/';
var basePHP = "phps/";
var baseJAVA = "scripts/";
var baseHTML = "htmls/";
function ajax() { 
    var args = ajax.arguments; 
    var the_call = '';
    var the_func = '';
    var the_id = '';
    var the_html = '';
    var the_script = '';
    var the_css = '';
    var the_php = 'data-view.php';
    var into = null;
    switch (args[0]) {
    case 'Call':
	if (typeof(args[1]) == 'undefined' ||
            typeof(args[2]) == 'undefined' ||
	    typeof(args[3]) == 'undefined') {
            break;
        }
	the_php = args[1] + '.php';
	the_func = args[3];
	var re = new RegExp(args[4]);
        for (var i=0; i<args[2].length; i++) {
            var field = args[2][i];
            if (!field.id) continue;
            if (field.id.match('dont-send')) continue;
            var col = field.id.replace(re, '');
            the_call += "&" + col + '=' + encodeURIComponent(field.value);
            //the_call += "\n" + col + "=" + field.value;
        }
	//alert(the_call); return;
	break;
    case "Display":
        if (typeof(args[1]) == 'undefined' ||
            typeof(args[2]) == 'undefined') {
            break;
        }
        the_id = the_html = the_script = the_css = args[1];
	the_html += ".htm";
        the_script += ".js";
	the_css += ".css";
        into = args[2];
        break;
    case 'Edit':
    case "Enter":
	if (typeof(args[1]) == 'undefined' ||
            typeof(args[2]) == 'undefined') {
            break;
        }
        var re = new RegExp(args[3]);
        the_call = 'action=' + args[0] + args[1];
        for (var i=0; i<args[2].length; i++) {
            var field = args[2][i];
            if (!field.id) continue;
            if (field.id.match('dont-send')) continue;
            var col = field.id.replace(re, '');
            the_call += "&" + col + '=' + encodeURIComponent(field.value);
            //the_call += "\n" + col + "=" + field.value;
        }
        //alert(the_call); return;
        the_func = 'checkResult';
	the_php = 'data-edit.php';
	break;
    case "Get":
	if (typeof(args[1]) == 'undefined') {
            break;
        }
	the_call = 'action=' + args[0] + args[1];
	if (typeof(args[2]) == 'object') {
	    for (var i=0; i<args[2].length; i++) {
		var items = args[2][i].split('=');
		the_call += "&" + items[0] + '=' + encodeURIComponent(items[1]);
	    }
	}
	the_func = args[3] ? args[3] : args[0] + args[1];
	the_php = 'data-view.php';
	break;
    case "Set":
	if (typeof(args[1]) == 'undefined') {
            break;
        }
        the_call = 'action=' + args[0] + args[1];
	for (var i=0; i<args[2].length; i++) {
	    var items = args[2][i].split('=');
	    the_call += "&" + items[0] + '=' + encodeURIComponent(items[1]);
	}
	the_func = args[3];
	the_php = 'data-edit.php';
	break;
    default:
	break;    
    }
    var client = createClient();
    if (client && the_call) {
        client.onreadystatechange = function() {
            if (client.readyState == 4 && client.status == 200) {
		//alert(the_call + "\n\n" + client.responseText);
		if (client.responseText == 'login') {
		    window.location = baseURL;
		    return;
		} 
		if (typeof(the_func) == 'string') {
		    eval(the_func + "(client.responseXML);");
		} else if (typeof(the_func) == 'function') {
		    the_func(client.responseXML);
		}
            }
        };
        client.open('POST', baseURL + basePHP + the_php, true);
        client.setRequestHeader("Content-type",
                                "application/x-www-form-urlencoded");
        client.setRequestHeader("Content-length", the_call.length);
        client.setRequestHeader("Connection", "close");
        client.send(the_call);
    }  else if (client && the_html) {
        client.onreadystatechange = function() {
            if (client.readyState == 4 && client.status == 200) {
                into.innerHTML = client.responseText;		
		var head = tags(document, 'head')[0];
		/*
		head.removeChild(nodeOf('style'));
		foster(head, elementize('link', 'rel=stylesheet', 'id=style',
					'href=' + baseURL + 'style.css'));
		*/
		var linkId = the_id + '-css';
		var linkTag = nodeOf(linkId);
		if (linkTag) {
		    var head = linkTag.parentNode;
		    head.removeChild(linkTag);
		}
		linkTag =
		       elementize('link', 'rel=stylesheet', 'id=' + linkId,
				  'href=' + baseURL + baseHTML + the_css);
		foster(head, linkTag); 
		var head = tags(document, 'head')[0];
		var scriptId = the_id + '-script';
		var scriptTag = nodeOf(scriptId);
                if (scriptTag) {
		    var head = scriptTag.parentNode;
		    head.removeChild(scriptTag);
                }
		scriptTag = 
		elementize('script', 'id=' + scriptId,
			   'type=text/javascript',
			   'src=' + baseURL + baseJAVA + the_script);
		foster(head, scriptTag);
            }
        };
        client.open("GET", baseURL + baseHTML + the_html, true);
        client.send(null);
    }
}
