// SimpleSwap by Jehiah Czebotar
// Distributed under Creative Commons
// <img src="/images/ss_img.gif" oversrc="/images/ss_img_over.gif">

function SimpleSwap(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}




document.write('<link rel="stylesheet" type="text/css" href="/files/inputs.css" media="screen" />');

var Input = {
	initialize: function() {
		if(document.getElementsByTagName("form")) {
			var divs = document.getElementsByTagName("div");
			for(var i = 0; i < divs.length; i++) {
				if(divs[i].className.match("checkbox") || divs[i].className.match("radio")) {
					divs[i].onmousedown = Input.effect;
					divs[i].onmouseup = Input.handle;
					document.onmouseup = Input.clear;
				}
			}
		}
	},
	
	effect: function() {
		if(this.className == "checkbox" || this.className == "radio") {
			this.style.backgroundPosition = "0 -30px";
		} else {
			this.style.backgroundPosition = "0 -90px";
		}
	},
	
	handle: function() {
		selector = this.getElementsByTagName("input")[0];
		if(selector) {
			if(this.className == "checkbox") {
				selector.checked = true;
				this.className = "checkbox selected";
				this.style.backgroundPosition = "0 -60px";
			} else if(this.className == "checkbox selected") {
				selector.checked = false;
				this.className = "checkbox";
				this.style.backgroundPosition = "0 0";
			} else {
				selector.checked = true;
				this.className = "radio selected";
				this.style.backgroundPosition = "0 -60px";
				inputs = document.getElementsByTagName("input");
				for(i = 0; i < inputs.length; i++) {
					if(inputs[i].getAttribute("name") == selector.getAttribute("name")) {
						if(inputs[i] != selector) {
							inputs[i].parentNode.className = "radio";
							inputs[i].parentNode.style.backgroundPosition = "0 0";
						}
					}
				}
			}
		}
	},
	
	clear: function() {
		divs = document.getElementsByTagName("div");
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className == "checkbox" || divs[i].className == "radio") {
				divs[i].style.backgroundPosition = "0 0";
			} else if(divs[i].className == "checkbox selected" || divs[i].className == "radio selected") {
				divs[i].style.backgroundPosition = "0 -60px";
			}
		}
	}
}


if (typeof document.attachEvent!='undefined') {
	window.attachEvent('onload',Input.initialize);
} else {
	window.addEventListener('load',Input.initialize,false);
}




var posting = false;
var geting = false;
var loaded = false;

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup(); initFileUploads(); loaded = true;}


var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	var input = document.createElement('input');
	input.className = 'tabl7';
	input.size = 11;
	fakeFileUpload.appendChild(input);
	var image = document.createElement('img');
	image.align = 'top'
	image.src='/images/browse.gif';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].getAttribute('noscript')) continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file_hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
		if (x[i].value) {
			x[i].onchange();
		}
	}
}

function executeAjax(callback, url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = callback;
		req.open("GET", url, true);
		req.send(null);
	} // branch for IE/Windows ActiveX version
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = callback;
			req.open("GET", url, true);
			req.send();
		}
	}
}

var http_request = false;
function makePOSTRequest(url, parameters) {
	http_request = false;
	posting = true;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = alertContents;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function getContent(url,id) {
	if(loaded) {
		var obj = document.getElementById(id);
		geting = true;
	}
	
	if(!loaded || posting) {
		setTimeout("getContent('" + url + "', '" +id + "')",250);		
	} else {
		var callback = function() {
			if(req.readyState == 4) {
				if(req.status == 200) {
					obj.innerHTML = req.responseText;
					geting = false;
				}
			}
		}
		executeAjax(callback, url);
	}
}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
						posting = false;
		} else {
			alert('There was a problem with the request.');
			posting = false;
		}
	}
}

function post(obj, script) {
	var poststr = '';
	for(var i = 0; i < obj.length; i++) {
		if(obj.elements[i].type == 'radio' || obj.elements[i].type == 'checkbox') {
			if(obj.elements[i].checked && !obj.elements[i].disabled) {
				poststr += obj.elements[i].name + '=' + encodeURI(checkRadio(obj.elements[i])) + '&';
			}
		} else {
			if(!obj.elements[i].disabled){
				poststr += obj.elements[i].name + '=' + encodeURI(obj.elements[i].value) + '&';
			}
		}
	}
	makePOSTRequest(script, poststr);
}

function checkRadio(r) {
	var f;
	if(r.length) {
		for(var i = 0; i < r.length; i++) {
			if(r[i].checked) {  f = r[i].value; }
		}
	} else {
		if(r.checked) { f = r.value; }
	}
	return f;
}

