
function toggleButtons(state, formName) {
	if (formName) var inputs = document.forms[formName].getElementsByTagName('input');
	else var inputs = document.getElementsByTagName('input');
	for (i = 0; i < inputs.length; i++) {
		if (inputs[i].type == 'button' || inputs[i].type == 'submit') inputs[i].disabled = state;
	}
}

var formSuccess = function(t) {
	toggleButtons(0);
	kill();
	eval(t.responseText);
}

var formError = function(t) {
	toggleButtons(0);
	kill();
	alert('Error ' + t.status + ' -- ' + t.statusText);
}

function ajaxSubmit(frm) {
	toggleButtons(1, frm.id);
	startActivity();
	var request = new Ajax.Request(frm.action, {method:'post', postBody:Form.serialize(frm.id), onSuccess:formSuccess, onFailure:formError});
	return false;
}

function ajaxRequest(url, queryStr) {
	startActivity();
	var request = new Ajax.Request(url, {method:'post', postBody:queryStr, onSuccess:formSuccess, onFailure:formError});	
}

function showAndTell(box, boxClass, txtContainer, txt, scrollToElem) {

	var displayFunc = function(box, boxClass, txtContainer, txt) {
		if ($(box).style.display == 'none') {
			$(txtContainer).innerHTML = txt;
			$(box).className = boxClass;
			new Effect.Appear(box, {duration:0.3});
		} else {
			new Effect.Opacity(box, {from:1.0, to:0.1, duration:0.3, afterFinish:function(){
				$(txtContainer).innerHTML = txt;
				$(box).className = boxClass;
				new Effect.Opacity(box, {from:0.1, to:1.0, duration:0.2});
			} });
		}
	}

	if (scrollToElem) {
		new Effect.ScrollTo(box, {duration:0.1, afterFinish:displayFunc(box, boxClass, txtContainer, txt)});
	} else {
		displayFunc(box, boxClass, txtContainer, txt);
	}

}

function isNumeric(sText) {

	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	
	return IsNumber;
   
}

function formatNumber(pnumber,decimals){
	if (isNaN(pnumber)) { return 0};
	if (pnumber=='') { return 0};
	
	var snum = new String(pnumber);
	var sec = snum.split('.');
	var whole = parseFloat(sec[0]);
	var result = '';
	
	if(sec.length > 1){
		var dec = new String(sec[1]);
		dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
		dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
		var dot = dec.indexOf('.');
		if(dot == -1){
			dec += '.'; 
			dot = dec.indexOf('.');
		}
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	} else{
		var dot;
		var dec = new String(whole);
		dec += '.';
		dot = dec.indexOf('.');		
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	}	
	return result;
}

function updateSample(schoolUri, productId, width, height, storeId) {
	loadFrame1(width, height);
	$('picture').style.display = 'none';
	$('frame').style.display = 'none';
	$('ind').style.display = '';
	if ($('shirtColor')) {
		var shirtColor = $('shirtColor').value;
		$('shirtColor').disabled = true;
	}
	$('textColor').disabled = true;
	if ($('textColorAlt')) {
		var textColorAlt = $('textColorAlt').value;
		$('textColorAlt').disabled = true;
	}
	if ($('textColor')) $('textColor').disabled = true;
	$('design').disabled = true;
	$('frame').src = "/store/sample.php?uri="+schoolUri+"&shirt_color="+shirtColor+"&text_color="+$('textColor').value+"&text_color_alt="+textColorAlt+"&design="+$('design').value+"&product_id="+productId+"&store_id="+storeId;
}

function sampleUpdated(productId) {
	$('ind').style.display = 'none';
	$('frame').style.display='';
	if ($('shirtColor')) $('shirtColor').disabled = false;
	if ($('textColor')) $('textColor').disabled = false;
	if ($('textColorAlt')) $('textColorAlt').disabled = false;
	$('design').disabled = false;
}

function addItem(uri) {
	$('orderForm').action = "http://www.alumniclass.com/"+uri+"/tools/order-add-hsac";
	$('orderForm').target = "_self";
	document.forms[0].submit();
}

function classInput1() {
	var html = '<div style="background-color:#f2f2f2; padding:6px;">Enter year to print (example 88, or 76): <input type="text" class="text2" name="year" style="width:30px; margin-left:4px;" maxlength="2" /></div>';
	$('year').innerHTML = html;
	$('year').style.display = '';
}

function classInput2() {
	var html = '<div style="background-color:#f2f2f2; padding:6px;">Enter year to print (example 1988, or 1976): <input type="text" class="text2" name="year" style="width:30px; margin-left:4px;" maxlength="4" /></div>';
	$('year').innerHTML = html;
	$('year').style.display = '';
}

function classClear() {
	$('year').innerHTML = '';
	$('year').style.display = 'none';
}

function checkYear(elem, productId) {
	var designMatch;
	var design_id = elem.id.substring(1);
	if (cs1[productId]) {
		for (i = 0; i < cs1[productId].length; i++) {
			if (cs1[productId][i] == design_id) {
				classInput1();
				designMatch = true;
			}
		}
	}
	if (cs2[productId]) {
		for (i = 0; i < cs2[productId].length; i++) {
			if (cs2[productId][i] == design_id) {
				classInput2();
				designMatch = true;
			}
		}
	}
	if (!designMatch) classClear();
}

function checkDesign(elem) {
	var design_id = elem.id.substring(1);
	if (design_id == 1) $('textColorBox').style.display = 'none';
	else $('textColorBox').style.display = '';
}

var frameLoaded1 = 0;
function loadFrame1(width, height) {
	if (frameLoaded1 == 0) {
		var elem = $('frameContainer1');
		var html = '<iframe id="frame" src="blank.html" frameborder="0" scrolling="no" style="width:'+width+'px; height:'+height+'px; display:none; padding:0; margin:0;"></iframe>';
		new Insertion.Bottom(elem, html);
		frameLoaded1 = 1;
	}
}		

function checkVal (elem) {
	if (elem.value != '' && (!isNumeric(elem.value) || elem.value < 1)) {
		elem.value = '';
		alert('Invalid quantity!');
	} 
	updateTotal();
}

function updateTotal() {
	$('displayTotal').innerHTML = '0.00';
	$('orderLink').style.display = 'none';
	var elems = $('quantities').getElementsByTagName('input');
	total = 0;
	for (i = 0; i < elems.length; i++) {
		if (elems[i].value != '') {
			var size_id = elems[i].id.substring(1);
			total = total + (Number(sizes[size_id]) * Number(elems[i].value));
			$('displayTotal').innerHTML = formatNumber(total, 2);
			$('orderLink').style.display = '';
		}
	}

}

function bookmark(url, title){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

function selectSchool(schoolId, name, state) {
	$('school_id').value = schoolId;
	$('schoolLocator').style.display = 'none';
	$('schoolSelectedName').innerHTML = name+' ('+state+')';
	//$('schoolSelected').style.display = '';
	Effect.Appear('schoolSelected', {duration:0.3});
}

function cancelSchool() {
	$('school').value = '';
	$('schoolSelected').style.display = 'none';
	$('schoolLocator').style.display = '';
	$('school').focus();
}