/**
 * @author syc
 */

function it905app() {
	var ajaxobj;
	
	this.command = function(action, args, obj) {
		switch (action) {
			case 'test':
				alert('test');
				break;
			
			case 'navigate':
				this.show_message('Loading...');
				this.remote_request('content.php?content='+args);
				break;
				
			case 'hidemessage':
				this.hide_message();
				break;
			
			case 'handlecc':
				var cc = this.get_byid('cc');
				this.command('ccdiff', '', cc);
				break;
				
			case 'ccdiff':
				var ccsecurity = this.get_byid('cc_security');
				if (obj.checked) {
					ccsecurity.style.display = '';
				} else {
					ccsecurity.style.display = 'none';
				}
				break;
			
			case 'handleship':
				var shipobj = this.get_byid('shipping_chk');
				this.command('shipdiff', '', shipobj);
				break;
				
			case 'shipdiff':
				// obj == shipping_chk
				var shipform = this.get_byid('shipping');
				if (obj.checked) {
					shipform.style.display = '';
				} else {
					shipform.style.display = 'none';
				}
				break;

			case 'replacechange':
				var prodimg = this.get_byid(args);
				var prodoptions = this.get_byid(obj);
				
				if ( prodimg && prodimg.id == 'prodimg8' ) {
					prodimg.src = 'images/'+prodoptions.value+'_replace.jpg';
				}
				break;
				
			case 'validateorder':
				// perform validation of fields here
				var orderform = this.get_byid('orderform');
				orderform.submit();
				break;
			
			case 'getprice':
				this.show_message('Updating...');
				this.remote_request('orderpricing.php?'+args+'&qty='+obj.value);
				break;
				
			case 'updateprice':
				this.show_message('Updated!');
				
				var qtyfield = this.get_byid('qty'+obj);
				var pricefield = this.get_byid('price'+obj);
				var totalfield = this.get_byid('total'+obj);
				
				pricefield.value = args;
				totalfield.value = (args*qtyfield.selectedIndex).toFixed(2);
				
				this.calcgrandtotal();
				
				break;
			
			case 'showmessage':
				this.show_message(args);
				break;
			
			default:
				alert('Unknown action: '+action);
				break;
		}
		return false;		
	}
	
	this.calcgrandtotal = function() {
		var grandobj = this.get_byid('grandtotal');
		
		var grandtotal = 0;
		var i = 1;
		while (true) {
			itemtotal = this.get_byid('total'+i);
			if ( !itemtotal ) break;
			if ( itemtotal && !itemtotal.value == '' )
				grandtotal += eval(itemtotal.value);
			i += 1;
		}
		grandobj.innerHTML = '$'+grandtotal.toFixed(2);
	}
	
	this.init = function(jsname) {
		this.jsname = jsname;
		this.ajaxinit();
	}
	
	this.ajaxinit = function() {
		this.ajaxobj = this.getxmlhttpobj();
		if (this.ajaxobj)
			this.ajaxobj.onreadystatechange=this.xmlhttpobjchanged;
	}
	
	this.show_message = function(msg, type, delay) {
		var message = this.get_byid('message');
		var messagetext = this.get_byid('messagetext');
		messagetext.innerHTML = msg;
		message.style['visibility'] = 'visible';
		
		// if no delay given, default to 3 seconds
		if ( !delay )
			delay = 3.0;
		
		// delay in milliseconds
		delay = delay * 1000;
	
		// start timer
		if ( this.message_timer_id ) {
			clearTimeout(this.message_timer_id);
			this.message_timer_id = 0;
		}
		this.message_timer_id = setTimeout('it905.command("hidemessage")', delay);
		
		return false;
	}
	
	this.hide_message = function() {
		var message = this.get_byid('message');
		message.style['visibility'] = 'hidden';
	}
	
	this.get_byid = function(id) {
		var obj = document.getElementById(id);
		return obj;
	}
	
	this.set_content = function(html) {
		var content = this.get_byid('content');
		
		if ( content ) {
			content.innerHTML = html;
		}
	}
	
	this.remote_request = function(url) {
		if (this.ajaxobj) {
			try {
				this.ajaxobj.open('GET', url, true);
				this.ajaxobj.send(null);
			} catch (e) {
				alert('AJAX Error: '+e.toString());
			}
		} else {
			alert('unable');
		}
	}
	
	this.remote_response = function(response) {
		var contenttype = this.ajaxobj.getResponseHeader('Content-Type');
		
		if ( contenttype == 'text/javascript' ) {
			eval(response);
		} else if (contenttype == 'text/html' ) {
			this.set_content(response);
			this.show_message('FINISHED!');
		}
		//this.hide_message();
		this.ajaxinit();
	}
	
	this.getxmlhttpobj = function() {
		var objXMLHttp=null;
		if (window.XMLHttpRequest)
			objXMLHttp=new XMLHttpRequest();
		else if (window.ActiveXObject)
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		
		return objXMLHttp;
	}
	
	this.xmlhttpobjchanged = function() {
		switch (it905.ajaxobj.readyState) {
			case 4:
				it905.remote_response(it905.ajaxobj.responseText);
				break;
		}
	}
}
