var debug = 0;

function genericCopy(frm_id, from, to){
	var frm = document.getElementById(frm_id);
	if (frm != null){
		for (var i = 0; i < frm.length; i++){
			if ((frm.elements[i].type) && (frm.elements[i].id.indexOf(from) == 0)){
				var fromelem = frm.elements[i];
				var elem_postfix = fromelem.id.split(from)[1];
				var toelem = document.getElementById(to+elem_postfix);

				if (toelem != null){
					if (toelem.type && fromelem.type){
						if (toelem.type == 'text' && fromelem.type == 'text'){
							toelem.value = fromelem.value;
						}else if (toelem.type == 'select-one' && fromelem.type == 'select-one'){
							if (toelem.options.length >= fromelem.selectedIndex){
								toelem.selectedIndex = fromelem.selectedIndex;
							}
						}
					}
				}
			}
		}
	}
}

var xmlhttp = null;

function loadurl(dest,method,args,func) {
	xmlhttp = null;

	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest( );
	}else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (! xmlhttp){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	}

	if (xmlhttp == null) {
		alert("Error creating request object");
		return false;
	}

	args += '&jsReq=1';

	// the xmlhttp object triggers an event everytime the status changes
	// triggered() function handles the events
	xmlhttp.onreadystatechange = func;
	// open takes in the HTTP method and url.

	// send the request. if this is a POST request we would have
	if(method=="POST"){
		xmlhttp.open(method, dest,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(args);
	} else {
		// Moz is fine with just send(); but
		// IE expects a value here, hence we do send(null);
		xmlhttp.open(method, dest + "?" + args,true);
		xmlhttp.send(null);
	}

	return true;
}

function validateForm(frm){
	if (frm != null){
		var args = '';
		for (var i = 0; i < document.forms[frm].length; i++){
			if (document.forms[frm].elements[i].type){
				var val = '';
				if (document.forms[frm].elements[i].type == 'text'){
					val = document.forms[frm].elements[i].name + '=' + escape(document.forms[frm].elements[i].value)
				}else if (document.forms[frm].elements[i].type == 'select-one'){
					if (document.forms[frm].elements[i].options.length > 0){
						val = document.forms[frm].elements[i].name+'='+escape(document.forms[frm].elements[i].options[document.forms[frm].elements[i].selectedIndex].value);
					}
				}else if (document.forms[frm].elements[i].type == 'checkbox'){
					if (document.forms[frm].elements[i].checked == true){
						val = document.forms[frm].elements[i].name+'='+escape(document.forms[frm].elements[i].value)
					}
				}else{
					val = document.forms[frm].elements[i].name+'='+escape(document.forms[frm].elements[i].value)
				}

				if ((val != '') && (args != '')){
					args += '&';
				}
				args += val;

			}
		}
		args += '&frmid='+document.forms[frm].id;
		loadurl(document.forms[frm].action,document.forms[frm].method.toUpperCase(),args,function(){evalReq()});
	}
	return false;
}

function evalReq(){
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			if (xmlhttp.getResponseHeader('Content-Type') == 'text/javascript'){
				eval(xmlhttp.responseText);
			}else{
				if (debug == 1){
					alert(xmlhttp.responseText);
				}
			}
		}else if (xmlhttp.status == 500){
			alert("Internal Server Error:\r\n"+xmlhttp.responseText.replace(/(<([^>]+)>)/ig,""));
		} else {
			if (debug == 1){
				alert(xmlhttp.status);
			}
		}
	}
}

function generateErr(msg, id){
	var obj = '';
	var err = msg;
	var d = document.getElementById(id);

	if (d != null){
		var lbl = d.parentNode
		if ((lbl != null) && (lbl.nodeName == 'LABEL')){
			obj = String (lbl.innerHTML);
			obj = obj.stripTags();
			obj = obj.replace(/\n|\r|\t|:|\*/g,'');
			obj = obj.trim();

			obj = ' \''+obj+'\'';
		}

		alert(err+obj);
		d.focus();
	} else {
		alert(err);
	}
}

//Taken from the prototype v1.4.0 code
String.prototype.stripTags = function(){
	return this.replace(/<\/?[^>]+>/gi, '');
}


String.prototype.trim = function(){
	var sString = this

	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}

	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}