
var ic = 0;
var cart_items = new Array();
		
var parsed = 0;
var products = new Array();
		
				
function qty(mode, code) 
{
		
	if (mode == 'minus') {
		c = document.getElementById(code+'_qty').value;
		c--;
		if (c < 0) { c=0; }
		document.getElementById(code+'_qty').value = c;
				
		//subtotal();				
	}
	else if (mode == 'add') {
		c = document.getElementById(code+'_qty').value;
		c++;
		if (c > 99) { c=99; }
		document.getElementById(code+'_qty').value = c;			
				
		//subtotal();
	} 
	else {
		alert('Error: unknown mode '+mode+' send to function qty()');
	}
} // END function qty


		
function subtotal() 
{
	t=0;
			
	for (i=0, j=cart_items.length; i<j; i++) {			
		nqty = cart_items[i]['qty'];
		
		// get price
		for (m=0, n=products.length; m<n; m++) {
				
			if ( products[m]['CODE'] == cart_items[i]['code'] ) {
				val = products[m]['PRICE'];
				break;
			}
		}
				
		t = t + (nqty *val);
	}
			
	t = t * 100;
	t = Math.round(t) / 100;	
	t = moneyise(t);

	document.getElementById('subtotal_text').innerHTML = 'US$'+t;			
				
} // END function subtotal
		


function moneyise(amt) 
{
	s = new String(amt);
			
	if (s.indexOf('.') < 0) { return (s+'.00'); }
			
	var len=s.length;
	if (s.charAt(len-2) == '.') { return(s+'0'); }
			
	amt = amt * 100;
	amt = Math.round(amt) / 100;	
			
	return (amt);
} // END function moneyise
		

		
function preporder() 
{
	// check shipping dest
	if (document.getElementById('shipping_dest').value == '') {
		alert ('Please select your shipping destination!');
	} 	
	else {
		
		if (cart_items.length <= 0) {
			alert('Please fill your cart with items to purchase');
		}
		else {
				
			// save cart items into flat format
			data = save_cart_flat();
					
			document.getElementById('order_data').value = data;
			
			document.shop.submit();
		}
	}
			
} // END function preporder
				
	
	
function add_to_cart( code ) 
{
		
	// check if product list has been parsed into an array
	if (parsed == 0) {
		parse_prodlist();
		parsed = 1;
	}
		
	if (document.getElementById(code+'_qty').value <= 0) {
		alert('Add or removes items to and from your cart by clicking the plus and minus buttons');
		return;
	}
			
	cart = document.getElementById('carttbl');
			
	lastrow = cart.rows.length;
			
	// row
	row = cart.insertRow(lastrow);
	rid = 'cartrow_'+ic;
	ic++;
	row.id = rid;
			
			
	// cell 1
	cell1 = row.insertCell(0);
	el = document.createElement('A');
	el.innerHTML = 'Remove';
	el.href = 'javascript:remove_from_cart("'+rid+'");';
	cell1.appendChild( el );
			
			
	// cell 2
	desc = get_desc( code );
	desc = document.getElementById(code+'_qty').value + ' x ' + desc;
	attr = get_attribs( code );
	if ( attr != '' ) {
		desc = desc + ' (' + attr + ')';
	}
			
	cell2 = row.insertCell(1);
	textNode = document.createTextNode( desc );
	cell2.appendChild( textNode );
			
						
	// cell 3
	price = get_price( code );
	price = price * document.getElementById(code+'_qty').value;
						
	cell3 = row.insertCell(2);
	cell3.align = 'right';
	textNode = document.createTextNode( '$'+moneyise(price) );
	cell3.appendChild( textNode );	
			
			
	// push onto array
	new_item = new Array();
	new_item['qty'] = document.getElementById(code+'_qty').value;
	new_item['code'] = code;
	new_item['id'] = rid;
	new_item['attrib'] = attr;

	cart_items.push( new_item );	
						
	// update sub total
	subtotal();						
} // END function add_to_cart
		
		

function save_cart_flat() 
{
		
	data = '';
			
	for (i=0, j=cart_items.length; i<j; i++) {
			
		data = data + 
			'CODE==' + cart_items[i]['code'] + '[::]' +
			'ATTRIB==' + cart_items[i]['attrib'] + '[::]' +
			'QTY==' + cart_items[i]['qty'] + '[;;]';
	}
			
	return data;
} // END function save_cart_flat
		
		

function get_price( code ) 
{
				
	for (i=0, j=products.length; i<j; i++) {
				
		if ( products[i]['CODE'] == code ) {
			return parseFloat( products[i]['PRICE'] );
		}
	}			
} // END function get_price
		
		

function get_desc( code ) 
{
		
	for (i=0, j=products.length; i<j; i++) {
				
		if ( products[i]['CODE'] == code ) {
			return products[i]['DESC'];	
		}
	}			
} // END function get_desc
		
		

function get_attribs( code ) 
{
		
	for (i=0, j=products.length; i<j; i++) {
				
		if ( products[i]['CODE'] == code ) {
				
			if ( !products[i]['ATTRIB'] ) {
				return '';
			}
					
			if ( products[i]['ATTRIB'].constructor == Array ||
				products[i]['ATTRIB'].constructor == Object )
			{
				// is array	
				attr = '';
						
				for (m=0, n=products[i]['ATTRIB'].length; m<n; m++) {
						
					if (attr == '') {
						attr = attr 
							+ document.getElementById( products[i]['ATTRIB'][m] ).value;							
					} 
					else {
						attr = attr + ', '
							+ document.getElementById( products[i]['ATTRIB'][m] ).value;
					}
				}	
						
				return attr;			
			}
			else {
				// single string
				return document.getElementById( products[i]['ATTRIB'] ).value;
			}						
		}
	}			
} // END function get_attribs
		

		
function remove_from_cart( id ) 
{
		
	cart = document.getElementById('carttbl');
	new_items = new Array();
	total_rows = cart.rows.length;
			
	for (i=0, j=total_rows; i<j; i++) {
			
		if( cart.rows[i].getAttribute('id') == id ) {
			cart.deleteRow( i );

					
			// remove from array
			for(i=0, j=cart_items.length; i<j; i++) {
					
				if ( cart_items[i]['id'] != id ) {
					new_items.push( cart_items[i] );
				}
			}
					
			cart_items = new_items;
					
			break;
		}
	}
			
	// update sub total
	subtotal();
} // END function remove_from_cart
		
		

function view_cart() 
{
		
	c = '';
			
	for(i=0, j=cart_items.length; i<j; i++) {
			
		c = c +'i='+i+':items='+cart_items[i]['id'] 
			+ ':qty='+ cart_items[i]['qty'] + '\n';
	}
			
	alert(c);
} // END function view_cart
		
		

function parse_prodlist() 
{				
						
	prods = new Array();
	prods = document.getElementById('prodlist').value.split(';');
			
	for(i=0, j=prods.length; i<j; i++) {
			
		prod_data = new Array();
					
		tags = new Array();
		tags = prods[i].split(':');
				
		for(t=0, k=tags.length; t<k; t++) {
				
			if ( tags[t] == '' ) { continue; }
					
			el = new Array();
			el = tags[t].split('=');
					
			// get field vals and trim()
			tag_name = el[0].replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
			tag_value = el[1].replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');

			is_mul = tag_value.indexOf(',');
					
			if (tag_name == 'ATTRIB' && is_mul != -1) {
				
				// comma delimination
				mul = new Array();
				mul = tag_value.split(',');
						
				prod_data[tag_name] = mul;
			} 
			else {
				prod_data[tag_name] = tag_value;
			}
		}
				
		products.push( prod_data );
	}
			
	//print_r( products );
	//alert(m);
	
} // END function parse_prodlist
	


m = '';
			
function print_r(theObj)
{
	
	
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    m = m +'*';
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
	    m = m +'*['+p+'] => '+typeof(theObj)+'\n';

        print_r(theObj[p]);

      } else {
	m = m +'*['+p+'] => '+theObj[p]+'\n';

      }
    }
    
  }
 
} // END funcion print_r





function checkorder() 
{
	what = document.orderForm;

	if (what.email.value != what.conf_email.value) {
		alert('The two email addresses you typed do not match, '
			+'please confirm the email address given');
	} else {
		// check we have data for all form fields
		var msg = '';
		f = new Array(
			'fname',
			'lname',
			'email',
			'address1',
			'city',
			'state',
			'zipcode'
		);
				
		var format = /\w+/g;
				
		for (i=0, j=f.length; i<j; i++) {
			n = document.getElementById(f[i]).value;

			if (!n.match(format)) {
				msg += f[i]+' is invalid!\n';
			}
		}
		

		if (msg == '') {
			// submit form
			document.orderForm.submit();
		} else {
			alert(msg);
		}

	}
} // END function checkorder
		
	
		
		
		
