/* 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; 
	}
}

var text_error;
var file_error;
function ATIonblur(id,x,y) {
	// this sets content height based on display_form() $top value stored in div content_height
	document.getElementById('content').style.height = document.getElementById('content_height').innerHTML;
	//confirm(document.getElementById('content_height').innerHTML);
	var error_msg = '';
	var value = document.getElementById(id).value;
	var name = document.getElementById(id).name;
	//confirm(name+','+value)
	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;
	/*
	Looking for the div that will have the top value.  Spans need to have id's
	*/
	// get the parent and section top for positioning type_error div
	var parent = document.getElementById(id).parentNode;	// get this node's parent
	do	{
		// keep looking up for div
		section = document.getElementById(parent.id).parentNode;
		var tag_name = document.getElementById(parent.id).tagName;
		if(tag_name != 'DIV') {
			parent = section;	// new child if div is not found
		}
		//confirm(tag_name)
  	} while (tag_name != 'DIV');	
	//confirm('ATI'+type+'("'+id+'","'+name+'","'+tag+'")');

	var ptop = parent.style.top;	// get parent top
	var section_top = section.style.top;	// get section top (parent's parent)
	ptop = Number(ptop.replace(/px/,""));	// strip px and convert to number
	section_top = Number(section_top.replace(/px/,""));
	var top = ptop+section_top;
	document.getElementById(type+'_error').style.top = top;	// move type error div
	
	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) {
		//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 
}

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(id+'()')
				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 {
				//confirm(id+'()')
				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 ATIfile(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('file_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('file_error', 'file_error');
				eval(id+'_error()');
			} else {
				document.getElementById('file_error').style.visibility = 'hidden';
				document.getElementById(id+'_error').innerHTML = '';
				//document.getElementById(id).value = xmlHttp.responseText;
				eval(id+'()');
			}
		}
    }
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/file.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+')');
}