var cookies = {
	name:"pipe",
	days:365,
	date:null,
	init:function(){
		if (this.date==null){
			this.date = new Date();
			this.date.setTime(this.date.getTime()+(this.days*24*60*60*1000));
			this.date.toGMTString()
		}
	},
	create:function(val){
		this.init();
		document.cookie=this.name+"="+val+"; expires="+this.date+"; path=/";
	},
	read:function(){
		var tmpName = this.name + "=";
		var ca = document.cookie.split(';');
		for (var a=0;a<ca.length;a++){
			var c = ca[a];
			while (c.charAt(0) == " ") c = c.substring(1,c.length);
			if (c.indexOf(tmpName) == 0) return c.substring(tmpName.length,c.length);
		}
		return null;
	},
	erase:function(){
		this.date = null;
		this.create("");
	}
};

var output = {
	elId:"output",
	on:false,
	ieFix:false,
	obj:null,
	el:null,
	form:null,
	dAngle:null,
	dA:null,
	dB:null,
	dC:null,
	dO:null,
	dL:null,
	init:function(){
		this.el = (this.el==null) ? document.getElementById(this.elId) : this.el ;
		this.dAngle = (this.dAngle == null) ? document.getElementById("dAngle") : this.dAngle ;
		this.dA = (this.dA == null) ? document.getElementById("dA") : this.dA ;
		this.dB = (this.dB == null) ? document.getElementById("dB") : this.dB ;
		this.dC = (this.dC == null) ? document.getElementById("dC") : this.dC ;
		this.dO = (this.dO == null) ? document.getElementById("dO") : this.dO ;
		this.dL = (this.dL == null) ? document.getElementById("dL") : this.dL ;
		this.form = (this.form == null) ? document.getElementById("frmPipe") : this.form ;
//		if (this.el.childNodes.length == 0) { this.on = false; }
		document.getElementById("R").focus();
	},
	write:function(){
		this.init();
		var tbody = document.createElement('tbody');
		var tr = document.createElement('tr');
		var td1 = document.createElement('td');
		td1.appendChild( document.createTextNode(this.obj.R) );
		td1.className = "angle";
		var td2 = document.createElement('td');
		td2.appendChild( document.createTextNode(this.obj.A) );
		td2.className = "a";
		var td3 = document.createElement('td');
		td3.appendChild( document.createTextNode(this.obj.B) );
		td3.className = "b";
		var td4 = document.createElement('td');
		td4.appendChild( document.createTextNode(this.obj.C) );
		td4.className = "c";
		var td5 = document.createElement('td');
		td5.appendChild( document.createTextNode(this.obj.L) );
		td5.className = "l";
		var td6 = document.createElement('td');
		td6.appendChild( document.createTextNode(this.obj.O) );
		td6.className = "o";

		tr.appendChild( td1 );
		tr.appendChild( td2 );
		tr.appendChild( td3 );
		tr.appendChild( td4 );
		tr.appendChild( td5 );
		tr.appendChild( td6 );
		
		tr.onclick=output.remove;
		tr.className = "current";

		tr.title = "Click to remove";
		tbody.appendChild( tr );
		var browser = navigator.appName;
		var ie = (browser == "Microsoft Internet Explorer") ? true : false ;
		var len = (ie && this.el.childNodes.length == 1) ? 0 : this.el.childNodes.length ;
		if (len == 0 && output.ieFix == false) {
			if (ie) {
				this.el.removeChild(this.el.childNodes.item(0)); // IE automatically adds an empty tbody we need to remove 
				output.ieFix = true;
			}
			this.el.appendChild( tbody );
		} else {
			if (output.on) {
				this.el.childNodes.item(0).childNodes.item(0).className = "on";
				output.on = false;
			} else {
				this.el.childNodes.item(0).childNodes.item(0).className = "";
				output.on = true;
			}
			this.el.insertBefore( tbody, this.el.childNodes.item(0) );
		}
		
		output.dAngle.innerHTML = this.obj.R;
		output.dA.innerHTML = this.obj.A;
		output.dB.innerHTML = this.obj.B;
		output.dC.innerHTML = this.obj.C;
		output.dL.innerHTML = this.obj.O;
		output.dO.innerHTML = this.obj.L;
	
		output.form.elements['A'].value = "";
		output.form.elements['B'].value = "";
		output.form.elements['C'].value = "";
		document.getElementById("indicate").innerHTML = "";
		output.form.elements['A'].focus();
//		cookies.create( this.el.innerHTML );
	},
	remove:function(){
		output.init();
		var tbody = this.parentNode;
		this.parentNode.parentNode.removeChild(tbody); // use the current event object in scope
		var nodeOn = false;

		for (var b=0;b<output.el.childNodes.length;b++) {
			if (nodeOn) {
				output.el.childNodes.item(b).childNodes.item(0).className = "on";
				nodeOn = false;
			} else {
				output.el.childNodes.item(b).childNodes.item(0).className = "";
				nodeOn = true;
			}
		}
		output.on=nodeOn;
//		cookies.create( output.el.innerHTML );
	},
	startup:function(){
		this.init();
		var obj = this.el.parentNode;
/*************
		Cookies for remembering form
		-------------
		obj.innerHTML = "";
		var str = "<table id=\"output\">";
		str += (cookies.read() == null) ? "" : cookies.read().toString().replace("<TBODY></TBODY>","").replace(/_/g,"") ;
		str += "</table>";
		obj.innerHTML = str;
		this.el = null;
		this.init();
		for (var b=0;b<output.el.childNodes.length;b++) {
			if (output.on) {
				output.on = false;
			} else {
				output.on = true;
			}
		}
		// We need to loop through again to attach the event handler.
		for (var b=0;b<output.el.childNodes.length;b++){
			output.el.childNodes.item(b).childNodes.item(0).onclick=output.remove;
		}
***************/
	}
};

var events = {
	objs:new Array(),
	init:function(){
		var el = document.getElementById('pipe');
		for (var a=0;a<el.childNodes.length;a++) {
			if (el.childNodes.item(a).nodeName.toLowerCase() == "input") {
				if (el.childNodes.item(a).getAttribute("type") == "text") {
					el.childNodes.item(a).onkeyup=events._keyup;
					this.objs.push(el.childNodes.item(a));
				}
			}				
		}
	},
	validate:function(form){
		if (output.form == null) {
			output.form = form;
		}
		
		for (var a=0;a<form.elements.length;a++) {
			if (!/^[0-9]/.test(form.elements[a].value) && form.elements[a].type != "submit") {
				alert("Please make sure all the fields are numeric");
				output.init();
				return false;
			}
		}

		if (form.elements['R'].value <= 19 || form.elements['R'].value >= 91) {
			alert("Please make sure the angle is greater than or equal to 20 and less than or equal to 90");
			form.elements['R'].value = "";
			output.init();
			return false;
		}
		document.getElementById("indicate").innerHTML = "<img src=\"images/indicator.gif\" /> Calculating";
		call.reqUrl = "call.php";
		call.reqForm = form;
		call.doCall();
		return false;
	},
	_keyup:function(evt){
		var evnt = (evt) ? evt : window.event ;
		
		if (evnt.keyCode > 32) {
			if (!/^[0-9]/.test(this.value) && this.value != this.title) {
				alert(this.value + " is not numeric");
				this.value = "";
				this.focus();
			}
		}
		if (evnt.keyCode == 13) {
			switch (this.name) {
				case "R":
					if (this.value <= 19 || this.value >= 91) {
						alert("Please make sure the angle is greater than or equal to 20 and less than or equal to 90");
					} else {
						output.form.elements['A'].focus();
					}
					break;
				case "A":
					output.form.elements['B'].focus();
					break;
				case "B":
					output.form.elements['C'].focus();
					break;
				case "C":
					events.validate(output.form);
					break;
			}
		}
	},
	_focus:function(){ },
	_blur:function(){ }
};

window.onload = function() {
	events.init();
	output.startup();
}