// $Date:
// $Id:
// $Revision:

// set picker scrolling if .pickerList exists and
// an element has the ON class we scroll to it so
// it is visible
Event.observe(window, 'load', function() {
	$$('.pickerList').each(function(pl){
		l=pl.cumulativeOffset().left;
		$(pl).select('.on').each( function(theA){
			o=theA.cumulativeOffset().first();
			pl.scrollLeft=o-l;
			});
		});
	});


function _xml_id(regionNumber, componentNumber, elementNumber) {

	// note: PHP version of this function is in class.component.php

	var id =
		 'id'+
		'RN' +(regionNumber<10?'0':'')    +regionNumber+
		'CN' +(componentNumber<10?'0':'') +componentNumber+
		'EN' +(elementNumber<10?'0':'')   +elementNumber;

	return(id);
}
function trim(str){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


function check_hub(label, topwID, botwID, arngID, symbID, ctlsID, charLimit) {

	// topw/botw are text inputs for wording. arng is a hidden element
	// (so it's always present; recall all elements go into php as
	// arrays.) symb is hidden for symbol value and ctls is the control
	// (for focus() call) for symb.
	$(topwID).value=trim($(topwID).value);
	$(botwID).value=trim($(botwID).value);
	// topw not empty
	if ( !$(topwID).value || $(topwID).value.length < 1 ) {
		alert("You must enter something for "+label+".");
		$(topwID).scrollTo();
		$(topwID).focus();
		return(false);
	}

	// botw EMPTY if they checked 'arrange'
	if ( !$(botwID).value && 1!=$(arngID).value ) {
		alert(
			"You must enter something in both fields for "+label+
			". Or, type all the wording in the top and mark the \"Arrange wording\" checkbox.");
		$(botwID).scrollTo();
		$(botwID).focus();
		return(false);
	}

	// max total wording length
	if ( charLimit < $(topwID).value.length + $(botwID).value.length ) {
		alert(
			"You may enter no more than "+charLimit+" total characters for "+label+
			". (You have "+($(topwID).value.length + $(botwID).value.length)+" characters.)");
		$(topwID).scrollTo();
		$(topwID).focus();
		return(false);
	}

	// symbol selection
	if ( ! $(symbID).value ) {
		alert("Please make a selection for the separating symbols for "+label+".");
		$(ctlsID).scrollTo();
		$(ctlsID).focus();
		return(false);
	}

	return(true);
}
function hubArrangeWording(cb, hiddenElm, topWInput, bottomWInput){

	// set hidden input based on  Checkbox
	$(hiddenElm).value=cb.checked?1:0;

	//combine on top line
	if(cb.checked){
		$(topWInput).value=$F(topWInput)+' '+$F(bottomWInput);
		$(bottomWInput).value='';
		$(bottomWInput).hide();
		$(bottomWInput).next().hide();
		$(topWInput).next().hide();
	}
	else{
		$(bottomWInput).show();
		$(bottomWInput).next().show();
		$(topWInput).next().show();
	}
}

function updateWC(top, bottom, max) {

	sum = $(top).value.length+$(bottom).value.length;
	elm = $(top).up().select('span.wcDisplay').first();
	remain = max-sum;
	if ( remain < 1 ) {
		elm.addClassName('error');
		elm.update('<b>'+(-remain)+' too many characters<\/b>');
	}
	else {
		elm.removeClassName('error');
		elm.update(remain+' more characters maximum.');
	}

	// enforce uppercase since typed something
	$(top).value    = $(top).value.toUpperCase();
	$(bottom).value = $(bottom).value.toUpperCase();
}

function check_notempty(label, elementID) {

	if ( ! $(elementID).value || $(elementID).value.length < 1 ) {
		// "specify" because this could be an input (like text) or a
		// radio or picker, etc
		alert("You must specify something for "+label+".");
		$(elementID).scrollTo();
		
		if($(elementID).visible() && $(elementID).type!=='hidden'){
			// try to set focus if it can accept focus
			// MSIE 6 errors on calling focus on a input type"hidden"
			$(elementID).focus();
		}
		
		return(false);
	}

	return(true);
}

function check_nametag_linelength(whichLine, inputElement, maxLength) {

	if ( $F(inputElement).length > maxLength ) {
		alert("Line "+whichLine+" can only be up to "+maxLength+" characters including spaces.");
		$(inputElement).value = $(inputElement).value.substr(0,maxLength);
		$(inputElement).focus();
	}
}

function check_plaquewording_linelength(selectElement, inputElement) {

	if ( $F(selectElement) == 'large' ) {
		if ( $F(inputElement).length > 22) {
			alert("Large text lines can only be up to 22 characters including spaces");
			$(inputElement).value = $(inputElement).value.substr(0,22);
			$(inputElement).focus();
		}
	}
	else {
		if ( $F(inputElement).length > 32 ) {
			alert("Small and medium text lines can only be up to 32 characters including spaces");
			$(inputElement).value = $(inputElement).value.substr(0,32);
			$(inputElement).focus();
		}
	}
}

function check_plaquewording_onchangesize(selectElement, regionNumber, componentNumber, numberOfLines) {

	if ( $F(selectElement) == 'large' ) {

		var counter = 0;
		for ( var i=0; i<numberOfLines; i++ ) {
			var elementNumber = ( i*2 ) +1;
			var elementID     = _xml_id(regionNumber, componentNumber, elementNumber);
			if ( 'large' == $F(elementID) ) {
				counter++;
			}
		}

		if ( counter > 3 ) {
			alert('Each plaque may have no more than 3 lines of large text.');
			$(selectElement).selectedIndex = 2; // small
			$(selectElement).focus();
		}
	}
}

function check_presentation_choices(label, radioElementID, radioElementName, radioElementValue, selectElementID) {

	// locate all of the input elements whose names begin with the name
	// we're passed. Square-brackets have meaning in the CSS-style
	// matching so radioElementName must not have the squares on it.
	var oneIsChecked = false;
	var valueIs;
	$$('input[name^="'+radioElementName+'"]').each(function(e){
		if(e.checked){
			oneIsChecked=true;
			valueIs=e.value;
		}
	});

	if ( ! oneIsChecked ) {
		alert("You must choose something for "+label+".");
		$(radioElementID).scrollTo();
		$(radioElementID).focus();
		return(false);
	}

	// radioElementValue may be passed as Null to indicate there does
	// not exist an additional select that must also be set.
	if ( radioElementValue && valueIs == radioElementValue ) {
		// then the select must also be set to a value
		if ( ! $(selectElementID) ) { // huh? what?!
			return(false);
		}
		if ( ! $F(selectElementID) ) {
			alert("You must choose details for "+label+".");
			$(selectElementID).scrollTo();
			$(selectElementID).focus();
			return(false);
		}
	}

	return(true);
}

function check_quantity(elementID, minimum, multiplesOf, isOptional) {

	if ( 0 === $F(elementID) && isOptional ) {
		return(true);
	}

	if ( $F(elementID) < minimum ) {

		if ( minimum > 1 ) {
			alert("Please specify a quantity of at least "+minimum+".");
		}
		else {
			alert("Please specify a quantity.");
		}

		$(elementID).scrollTo();
		$(elementID).focus();

		return(false);
	}

	if ( $F(elementID) % multiplesOf !== 0 ) {
		alert("Please order a quantity which is a multiple of "+multiplesOf+".");
		$(elementID).scrollTo();
		$(elementID).focus();
		return(false);
	}

	return(true);
}

function check_sum_quantities(minimum, multiplesOf) {

	// assuming <input name="f_quantity[] ... />

	sum=0;
	$$('input[name^=f_quantity]').each(function(theI){
		v=parseInt($F(theI),10);
		if(!isNaN(v)){
			sum+=v;
		}
		});

	if ( sum < minimum ) {
		alert("Please specify a total quantity of at least "+minimum+".");
		return(false);
	}

	if ( sum % multiplesOf !== 0 ) {
		alert("Please order a quantity which is a multiple of "+multiplesOf+".");
		return(false);
	}

	return(true);
}

function check_string(label, elementID, minimum, maximum, confirmEmptyID) {

	// optional: We can require a confirmation checkbox be set to accept
	//           an empty value.
	var confirmEmpty = false;
	if ( ''!=confirmEmptyID && $(confirmEmptyID) ) {
		confirmEmpty = true;
	}

	// enforce minimum
	if ( minimum > 0 ) {
		if ( ! $(elementID).value || $(elementID).value.length < minimum ) {
			if ( minimum == 1 ) {
				alert("You must enter something for "+label+".");
			}
			else {
				alert("You must enter at least "+minimum+" characters for "+label+".");
			}
			$(elementID).scrollTo();
			$(elementID).focus();
			return(false);
		}
	}
	else if (
		   confirmEmpty                                           // confirmation is desired
		&& ( !$(elementID).value || $(elementID).value.length<1 ) // and string is empty
		&& true!=$(confirmEmptyID).checked                        // and not confirmed
		)
	{
		alert("You must enter something for "+label+" or check 'No custom message'.");

		$(elementID).scrollTo();
		$(elementID).focus();
		return(false);
	}

	// enforce maximum
	if ( maximum > 0 ) {
		if ( $(elementID).value.length > maximum ) {
			alert("You have entered too many characters for "+label+". There are "+$(elementID).value.length+" characters and the limit is "+maximum+".");
			$(elementID).scrollTo();
			$(elementID).focus();
			return(false);
		}
	}

	return(true);
}

function copy_nametag_previous(regionNumber, componentNumber, numberOfLines) {

	var fromElementID;
	var toElementID;

	for ( var elementNumber=1; elementNumber<=numberOfLines; elementNumber++ ) {
		fromElementID = _xml_id( (regionNumber-1), componentNumber, elementNumber );
		toElementID   = _xml_id( regionNumber,     componentNumber, elementNumber );
		$(toElementID).value = $(fromElementID).value;
	}
}

function plaquewording_copy_previous(regionNumber, componentNumber, numberOfLines) {

	var fromElementID;
	var toElementID;

	for ( var i=0; i<numberOfLines; i++ ) {

		// dupe size SELECT . . .
		fromElementID = _xml_id((regionNumber-1), componentNumber, ((i*2)+1));
		toElementID = _xml_id(regionNumber, componentNumber, (i*2)+1);
		$(toElementID).selectedIndex = $(fromElementID).selectedIndex;

		// dupe size INPUT values . . .
		fromElementID = _xml_id((regionNumber-1), componentNumber, ((i*2)+2));
		toElementID = _xml_id(regionNumber, componentNumber, (i*2)+2);
		$(toElementID).value = $(fromElementID).value;
	}
}

function changlingPickerSet(a, inputId, itemId){
	$(inputId).value=itemId;
	$(a).up().up().select('a').each(function(theE){theE.removeClassName('on');});
	$(a).addClassName('on');
	$(inputId+'_text').innerHTML='SELECTED: <b>'+$(a).innerHTML.stripTags()+'</b>';
}

function changlingSelectSet($component_number, hiddenInputId, selectId) {
	$(hiddenInputId).value = $F(selectId);
	refreshProductImage($component_number, $F(selectId));
}

function refreshProductImage(componentNumber, componentVersionID) {

	// We are called to alter the SRC attriute on some IMG. We only know
	// how to wrangle and alter "feeder" imagery.

	// Does the current image source look like the image came from the
	// imagery "feeder"?
	var currentSource = $('imgProductFullsize').src;
	if ( ! currentSource.match(/\/images\/feeder\.phtml/) ) {
		// there's nothing we can do!
		return(true);
	}

	// everything is already encoded in the current image source
	var data =
		  'current_src=' +encodeURIComponent(currentSource)+
		  '&component_number=' +encodeURIComponent(componentNumber)+
		  '&component_version_id=' +encodeURIComponent(componentVersionID);

	// There's an AJAX component that can decode the current source and
	// add in this component choice. AJAX URL must be root referenced
	// due to path_info usase.
	new Ajax.Request(
		'/catalog/details_imagesrc_ajax.phtml',
		/* options */ {
			method:     'post',
			parameters: data,
			onComplete: function(transport) {
				if ( ! transport.responseText.match('^ERROR') ) {
					$('imgProductFullsize').src = transport.responseText;
				}
			},
			onFailure:  bbxAjaxFail
		}
	);



}


