﻿var Softsmart_Ajax_FieldDelimiter = "#@#";
var Softsmart_Ajax_FieldEqual = "===";

function Softsmart_Ajax_Start(){
    var object = null;
    
    if (window.XMLHttpRequest){
        object = new XMLHttpRequest();
        
    } else if (window.ActiveXObject){
        try {object = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){}
        if (object == null){try {object = new ActiveXObject("Microsoft.XMLHTTP");} catch(e){}}
    }
       
    if (object == null){alert("Sorry! Your browser does not support XmlHttp Posts."); return;}
    return object;
}

function Softsmart_Ajax_ValuePair(fieldName, fieldValue){
	this.fieldName = fieldName;
	this.fieldValue = fieldValue;
}

function Softsmart_Ajax_Dictionary(valuePairs){
	var myValuePairs = valuePairs.split(Softsmart_Ajax_FieldDelimiter);
	this.dict = new Array();
	
	for (var index = 0; myValuePairs.length > index; index++){
		var myRow = myValuePairs[index].split(Softsmart_Ajax_FieldEqual);
		var myValuePair = new Softsmart_Ajax_ValuePair(myRow[0].toLowerCase(), myRow[1]);
		this.dict[index] = myValuePair;
	}
}

function Softsmart_Ajax_SetFormValues(valuePairs){
	var myForm = new Softsmart_Ajax_Dictionary(valuePairs);
	
	for (var index = 0; myForm.dict.length > index; index++){
		try {
			var id = String(myForm.dict[index].fieldName).toLowerCase();
			var type = document.getElementById(id).type;
			
			if (type == 'button' || type == 'file' || type == 'image' || type == 'submit' || type == 'reset'){continue;}
			
			if (type == 'radio'){
				id = String(myForm.dict[index].fieldName + myForm.dict[index].fieldValue).toLowerCase();
				document.getElementById(id).checked = true;
				continue;
			}

			if (type == 'checkbox'){
				if (document.getElementById(id).value == myForm.dict[index].fieldValue){
					document.getElementById(id).checked = true;
				}
				
				continue;
			}

			document.getElementById(id).value = myForm.dict[index].fieldValue;
		
		} catch(e){}
	}
}

function Softsmart_Ajax_GetFormValues(){
	var valuePairs = "";
	var delimiter = Softsmart_Ajax_FieldDelimiter;

	for (var index = 0; document.forms[0].elements.length > index; index++){
		try {
			if (document.forms[0].elements[index].type == 'button'){continue;}
			if (document.forms[0].elements[index].type == 'file'){continue;}
			if (document.forms[0].elements[index].type == 'image'){continue;}
			if (document.forms[0].elements[index].type == 'submit'){continue;}
			if (document.forms[0].elements[index].type == 'reset'){continue;}
			if (document.forms[0].elements[index].type == 'radio' && document.forms[0].elements[index].checked == false){continue;}
			if (document.forms[0].elements[index].type == 'checkbox' && document.forms[0].elements[index].checked == false){continue;}
			
			if (valuePairs == ""){delimiter = "";} else {delimiter = Softsmart_Ajax_FieldDelimiter;}
			valuePairs = valuePairs + delimiter + document.forms[0].elements[index].name + Softsmart_Ajax_FieldEqual + document.forms[0].elements[index].value;
		
		} catch (e){}
	}

	return "##VALUEPAIRSTART##" + valuePairs + "##VALUEPAIREND##";
}

function Softsmart_Ajax_Action(divName, actionName, async, debug){
	var myAjaxObject = Softsmart_Ajax_Start();
	
	if (async == null || async == undefined){async = true;}
	
	var url = document.URL;
	if (url.indexOf('?') >= 0){url = url + '&ajax=' + actionName;} else {url = url + '?ajax=' + actionName;}

	myAjaxObject.onreadystatechange = function(){
		try {
			if (myAjaxObject.readyState == 4 && myAjaxObject.status == 200){
				document.body.style.cursor = 'auto';

				var result = myAjaxObject.responseText;
				var start = 0; end = 0;
				var myResults = result.split('@@@@');

				for (var index = 0; myResults.length > index; index++){
					result = myResults[index];
					
					if (result.indexOf('##VALUEPAIRSTART##') >= 0 && result.indexOf('##VALUEPAIREND##') >= 0){
						start = eval(result.indexOf('##VALUEPAIRSTART##') + '##VALUEPAIRSTART##'.length);
						end = result.indexOf('##VALUEPAIREND##');
						result = result.substring(start, end);
						if (result.length){Softsmart_Ajax_SetFormValues(result);}
						if (debug == true){alert(result);}

					} else if (result.indexOf('##RENDERSTART##') >= 0 && result.indexOf('##RENDEREND##') >= 0){
						start = eval(result.indexOf('##RENDERSTART##') + '##RENDERSTART##'.length);
						end = result.indexOf('##RENDEREND##');
						result = result.substring(start, end);
						document.getElementById(divName).innerHTML = result;
						if (debug == true){alert(result);}

					} else if (result.indexOf('##CUSTOMSTART##') >= 0 && result.indexOf('##CUSTOMEND##') >= 0){
						start = eval(result.indexOf('##CUSTOMSTART##') + '##CUSTOMSTART##'.length);
						end = result.indexOf('##CUSTOMEND##');
						result = result.substring(start, end);
						if (result.length > 0){eval(result);}
						if (debug == true){alert(result);}
					}
				}
			}
			
		} catch(e){alert(e.message + ' ' + result);}
	}
	
	myAjaxObject.open('post', url, async);
	myAjaxObject.send(Softsmart_Ajax_GetFormValues());
	document.body.style.cursor = 'wait';
}
