/* JQuery goodness */

$(function() {
	$('a').focus(function() { this.blur(); });		// Blur on focus.
});

$.fn.check = function(mode) {
	var mode = mode || 'on'; // if mode is undefined, use 'on' as default
	return this.each(function() {
		switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
		}
	});
};

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', prettyform, false );

function prettyform(){
  // Hide forms
  $( 'form.prettyform' ).hide().end();
  
  // Processing
  $( 'form.prettyform' ).find( 'li/label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end();
  
  // Show forms
  $( 'form.prettyform' ).show().end();
}

sfHover = function() {
	var sfEls = document.getElementById("sucker").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function check(){
	for(var i=0; i<document.getElementById('checkform').elements.length; i++){
		if(document.getElementById('checkform').elements[i].type.match(/checkbox/i)){
			if(document.getElementById('toggle').alt.match(/^Select/i)){ document.getElementById('checkform').elements[i].checked = 1 }
			else{ document.getElementById('checkform').elements[i].checked = 0 }
		}
	}
	document.getElementById('toggle').alt = (document.getElementById('toggle').alt == 'Select All') ? 'Unselect All' : 'Select All';
} 

function selectall(selectRef){
	for (var i=0;i<document.getElementById(selectRef).options.length;i++) {
		document.getElementById(selectRef).options[i].selected = true;
	}
}

function selectnone(selectRef){
	for (var i=0;i<document.getElementById(selectRef).options.length;i++) {
		document.getElementById(selectRef).options[i].selected = false;
	}
}

function submitform(theform){
    document.getElementById(theform).submit();
}

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	   num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
			  num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function specifyOrder(strOrdermethod)
{
	//strOrdermethod = objOrdermethod.value;
	//alert("field: [" + fieldsetid +"] radio: [" + radioid + "]");
	
	switch(strOrdermethod){
			case 'confirmation':
				$('#order_confirmation').removeClass('hidden');
				$('#order_external').addClass('hidden');
				$('#order_none').addClass('hidden');
				break;
			case 'external':
				$('#order_external').removeClass('hidden');
				$('#order_confirmation').addClass('hidden');
				$('#order_none').addClass('hidden');
				break;
			case 'none':
				$('#order_none').removeClass('hidden');
				$('#order_confirmation').addClass('hidden');
				$('#order_external').addClass('hidden');
				break;
	}
}

function specifyCountry()
{
	strCountryiso = $('#countryiso').val();
	
	switch(strCountryiso){
		case 'NZ':
		case 'AU':
			//alert('GST 12.5%');
			$('#salestax1').check();
			$('#taxrate').val('12.5');
			$('#taxname').text('GST');
			$('#statefield').addClass('hidden');
			$('#zipcodefield').addClass('hidden');
			break;
		case 'GB':
			//alert('VAT 17.5%');
			$('#salestax1').check();
			$('#taxrate').val('17.5');
			$('#taxname').text('VAT');
			$('#statefield').addClass('hidden');
			$('#zipcodefield').addClass('hidden');
			break;
		case 'US':
			//alert('Sales Tax 7.25%');
			$('#salestax1').check();
			$('#taxrate').val('7.25');
			$('#taxname').text('Sales Tax');
			$('#statefield').removeClass('hidden');
			$('#zipcodefield').removeClass('hidden');
			break;
		case 'CA':
			$('#statefield').removeClass('hidden');
			$('#zipcodefield').removeClass('hidden');
			$('#taxname').text('Sales Tax');
			break;
		case 'ZA':
			$('#taxname').text('Sales Tax');
			$('#statefield').addClass('hidden');
			$('#zipcodefield').addClass('hidden');
			break;
	}
}

function countryAddress()
{
	strCountryiso = $('#countryiso').val();
	
	switch(strCountryiso){
		case 'CA':
		case 'US':
			$('#statefield').removeClass('hidden');
			$('#zipcodefield').removeClass('hidden');
			break;
		default:
			$('#statefield').addClass('hidden');
			$('#zipcodefield').addClass('hidden');
			break;
	}
}

function popitup(url) {
	var newwindow;
	newwindow=window.open(url,'popupWindow','height=500,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,location=no,status=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}