//CHEER INK JAVASCRIPT FUNCTIONS


//set the display state of a supplied (blocklevel) element
function switchViz(eid){
    var el = document.getElementById(eid);
    if (el.style.display == "none" || el.style.display == '') {
        el.style.display = "block";
    }
    else {
        el.style.display = "none";
    }
}

//clear forms
function clearForms(){
    var tds = document.getElementsByTagName('input');
    for (var i = 0; i < tds.length; i++) {
        if (tds[i].type.toLowerCase() == 'text') {
            if (tds[i].name != 'title') {
                tds[i].value = '';
            }
            else {
				tds[i].value = 'Type the title of your blog post here';
            }
        }
    }    
    //clear text areas
    var tas = document.getElementsByTagName('textarea');
    for (var i = 0; i < tas.length; i++) {
		tas[i].value = '';
		tas[i].innerHTML = '';
    }
}

//change product image
function changeProductImage(ipath, iname){
	document.getElementById('pimg').innerHTML='<img src="'+ipath+'" width="365" height="406" alt="'+iname+'" />';
}
function nextItem(){
	$.post( baseurl+"advanceitem.php", {dir:1},
	function(data){
		window.location = data;
	}, "text"
	);
	
}
function previousItem(){
	$.post( baseurl+"advanceitem.php", {dir:-1},
	function(data){
		window.location = data;
	}, "text"
	);
}
//add item to cart
function addItem(pid){
	var qty=document.getElementById('qty').value;	
	qty=Math.round(qty);
	var sizes=document.getElementById('opt_size');
	var colors=document.getElementById('opt_color');
	var size=sizes.options[sizes.selectedIndex].value;
	var color=colors.options[colors.selectedIndex].value;
	$.post(baseurl+"additem.php", {pid:pid, qty:qty, color:color, size:size},
		function(data){
			window.location = baseurl+"view_cart/";
		}, "text"
	);
}
//update quantity
//input params: 
//(qfield_id)= the id of the quantity field for the specific cart entry.
//(cartindex)= the index for the entry in the cart session var
function updateQTY(qfield_id, cartindex){
	var qty=document.getElementById(qfield_id).value;
	qty=Math.round(qty);
	document.getElementById(qfield_id).value=qty;
	$.post(baseurl+"updatecart.php", {cartindex:cartindex, qty:qty},
		function(data){
			//alert(data);
			window.location = baseurl+"view_cart/";
		}, "text"
	);
}

//update values and form elements for international shipping addresses
function changeShipOptions(){
		//if billing addy and shipping addy are different then use shipping field
		var cobox;		
		if (document.getElementById("noship").checked) {
			cobox = document.getElementById('country');
		} else {
			cobox = document.getElementById('s_country');
		}
		var i = cobox.selectedIndex;
		var ctry = cobox.options[i].value;		
		
		var sbox=document.getElementById('shipopts');
		if (ctry != "US") {			
			sbox.innerHTML="<select name=\"ShipMethod\" id=\"shipMethod\" onchange=\"updateShipMethod()\"><option value=\"12.95\">International ($12.95)</option></select>";
		} else {
			sbox.innerHTML="<select name=\"ShipMethod\" id=\"shipMethod\" onchange=\"updateShipMethod()\"><option value=\"0\">Free Shipping</option><option value=\"15\">2-Day Shipping ($15)</option><option value=\"22\">Overnight Shipping ($22)</option></select><br /><br />";
		}
		
		updateShipMethod();
}

//calculate shipping charges


function updateShipMethod(){
	//get current total	
	var mtotal=document.getElementById('merchprice').value;
	
	//get selected ship method	
	var sbox=document.getElementById('shipMethod');
	var si=sbox.selectedIndex;
	var srate=sbox.options[si].value;
	if(isNaN(srate)){
		srate=0;
	}
	//update smethod hidden field
	var smeth=sbox.options[si].text;

	document.getElementById("smethod").value=smeth;
	//
	var newtotal=(parseFloat(mtotal) + parseFloat(srate));
	//update estimated total 
	document.getElementById("finalprice").innerHTML=newtotal;
	//update hidden price field?
	document.getElementById("tprice").value=newtotal;	
}

function hideShipping(){
	document.getElementById('shipadd').style.display='none';
	//update the shipping options dropdown;
	changeShipOptions();
}
function showShipping(){
	document.getElementById('shipadd').style.display='inline';
	//update the shipping options dropdown;
	changeShipOptions();
}

