/* Copyright June 23, 2009 Adapti Tech, Inc.  All Rights Reserved. */

/* common functions */

// this allows some function to be altered during execution
var someFunction
someFunction = function () {	
	confirm("someFunction called");
};

// map into a function recording how 
var onevent;
function ATIonload(id) {
	onevent = 'onload';
	eval(id+'()');	// map into one of the page node 
}

function ATIunonload(id) {
	onevent = 'unonload';
	eval(id+'()');	// map into one of the page node 
}

function ATIonclick(id) {
	onevent = 'onclick';
	//confirm(id+'()');
	eval(id+'()');	// map into one of the page node 
}

function ATIonmouseover(id) {
	onevent = 'onmouseover';
	eval(id+'()');	// map into one of the page node 
}

function ATIonmouseout(id) {
	onevent = 'onmouseout';
	eval(id+'()');	// map into one of the page node 
}

function ATIonchange(id) {
	onevent = 'onchange';
	eval(id+'()');	// map into one of the page node 
}

function ATIonerror(id) {
	onevent = 'onerror';
	eval(id+'()');	// map into one of the page node 
}

function ATIonfocus(id) {
	onevent = 'onfocus';
	eval(id+'()');	// map into one of the page node 
}

var tabIndex = 0;
var tabbing = false;
var section;

function get_section(child) {
	// get the parent node tag name
	section = document.getElementById(child.id).parentNode;
	var tag_name = document.getElementById(child.id).tagName;	
	if(tag_name != 'DIV') {
		//confirm('!DIV '+tag_name)
		child = section;	// new child if div is not found
		get_section(child);	// go up to next parent
		return false;
	}
	if(tag_name == 'DIV') {
		//confirm('DIV '+tag_name)
		return true; 
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var pos;	// [curleft,curtop]
function ATIonblur(id,x,y) {
	//confirm('ATIonblur('+id+','+x+','+y+')');
	var error_msg = '';
	var value = document.getElementById(id).value;
	var name = document.getElementById(id).name;
	setSessionVar(name,value);
	var type = document.getElementById(id).type;
	type = type.replace(/-/,"_");	// such as select-one to select_one
	var tag = document.getElementById(id).nodeName;
	
	if(!tabbing) {
		tabIndex = document.getElementById(id).tabIndex;	// update the tabIndex for focus
	}
	var tab = document.getElementById(id).tabIndex;	// get current tabIndex
	// if match, then go do work - this is a new field or the same field calling
	if(tab == tabIndex) {
		// get position ready for error
		pos = findPos(document.getElementById(id));
		var top = pos[1];
		content_top = document.getElementById('content').offsetTop;
		top = top - content_top;
		document.getElementById(type+'_error').style.top = top;	// move type error div 

		//confirm('ATI'+type+'("'+id+'","'+name+'","'+tag+'")');
		eval('ATI'+type+'("'+id+'","'+name+'","'+tag+'")');
		tabbing = true;
	}
}

function ATIonsubmit(id) {
	onevent = 'onsubmit';
	eval(id+'()');	// map into one of the page node 
}

function ATIonreset(id) {
	onevent = 'onreset';
	eval(id+'()');	// map into one of the page node 
}

var text_error;
function ATItext(id,name,tag) {
	// call text.php
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			//confirm(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				document.getElementById('text_error').style.visibility = 'visible';
				document.getElementById('form_instructions').style.visibility = 'hidden'; 
				document.getElementById('close_form_instructions').style.visibility = 'hidden'; 
				document.getElementById('view_form_instructions').style.visibility = 'visible';	
				getSessionVar('text_error', 'text_error'); // text.php will set session var
				eval(id+'_error()');
			} else {
				//confirm(xmlHttp.responseText);
				document.getElementById(id+'_error').innerHTML = '';
				document.getElementById(id).value = xmlHttp.responseText;
				document.getElementById('text_error').style.visibility = 'hidden';
				eval(id+'()');
			}
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/text.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function ATIradio(id,name,tag) {
	// call radio.php
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	checked = document.getElementById(id).checked;
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag+
		'&checked='+checked
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				document.getElementById('radio_error').style.visibility = 'visible';
				document.getElementById('form_instructions').style.visibility = 'hidden'; 
				document.getElementById('close_form_instructions').style.visibility = 'hidden'; 
				document.getElementById('view_form_instructions').style.visibility = 'visible';	
				getSessionVar('radio_error', 'radio_error');
				eval(id+'_error()');
			} else {
				document.getElementById('radio_error').style.visibility = 'hidden';
				document.getElementById(id+'_error').innerHTML = '';
				//document.getElementById(id).value = xmlHttp.responseText;
				eval(id+'()');
			}
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/radio.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function ATIselect_one(id,name,tag) {
	//confirm('ATIselect_one('+id+','+name+','+tag+')');
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				document.getElementById('select_one_error').style.visibility = 'visible';
				document.getElementById('form_instructions').style.visibility = 'hidden'; 
				document.getElementById('close_form_instructions').style.visibility = 'hidden'; 
				document.getElementById('view_form_instructions').style.visibility = 'visible';	
				getSessionVar('select_one_error', 'select_one_error');	// writes error data 
				eval(id+'_error()');
			} else {
				document.getElementById('select_one_error').style.visibility = 'hidden';
				document.getElementById(id+'_error').innerHTML = '';
				eval(id+'()');
			}
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/select_one.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function ATIcheckbox(id,name,tag) {
	// call checkbox.php
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	checked = document.getElementById(id).checked;
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag+
		'&checked='+checked
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				document.getElementById('checkbox_error').style.visibility = 'visible';
				document.getElementById('form_instructions').style.visibility = 'hidden'; 
				document.getElementById('close_form_instructions').style.visibility = 'hidden'; 
				document.getElementById('view_form_instructions').style.visibility = 'visible';	
				getSessionVar('checkbox_error', 'checkbox_error');
				eval(id+'_error()');
			} else {
				document.getElementById('checkbox_error').style.visibility = 'hidden';
				document.getElementById(id+'_error').innerHTML = '';
				//document.getElementById(id).value = xmlHttp.responseText;
				eval(id+'()');
			}
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/checkbox.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function ATItextarea(id,name,tag) {
	// call checkbox.php
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				document.getElementById('textarea_error').style.visibility = 'visible';
				document.getElementById('form_instructions').style.visibility = 'hidden'; 
				document.getElementById('close_form_instructions').style.visibility = 'hidden'; 
				document.getElementById('view_form_instructions').style.visibility = 'visible';	
				getSessionVar('textarea_error', 'textarea_error');
				eval(id+'_error()');
			} else {
				document.getElementById('textarea_error').style.visibility = 'hidden';
				document.getElementById(id+'_error').innerHTML = '';
				//document.getElementById(id).value = xmlHttp.responseText;
				eval(id+'()');
			}
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/textarea.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

function ATIselect_multiple(id,name,tag) {
	// not used
	confirm('ATIselect_multiple('+id+','+name+','+tag+')');
}