/*
 Current Version : 1.0.0 by Darren 01/05/2006
 Added checkBoxCheck to mesh with the new add to cart options
 switched for/in loop over form elements to a for loop for safari
 Version : .03
 Darren's small but soon to grow javascript library
 functions relevant to the Product pages in the MegaMall
 June 7, 2004
 version .02 code refactoring
 version .03 fixed issue when there is only one radio button
 fixed error in hasColor when select was not defined
 verson 1.0.1 by Darren 02-14-2006
 use a div tag to show add to cart errors instead of a pop-up box..much friendlier
 version 1.1.0 by darren 2011-05-24
 Added the ability to color check an hidden input box for prettier color selectors
 */

// safari doesn't like the for( var i in HtmlFormElement ) syntax
function checkboxCheck(formName) {
		if ( formName == "option_form" ) {
				var options = new Array();
				var oneOption = 0;
				var theForm = document.option_form;
				// no longer have a nice radio group..we need to find all the checkboxes
				//		for (var i in theForm.elements) { // safari does not like this one bit! so better safe then safari heh!
				for (var i = 0; i < theForm.length; i ++ ) {
						try { // there are plenty of elements in the form that doen't have a .type member..skip those
								if ( theForm[i].type == 'checkbox' && theForm[i].checked ) {
										// need to keep track of the opt_id
										options.push(theForm[i].name);
								} else if ( theForm[i].type == 'hidden' && /qty_(\d+)/.test(theForm[i].name) ) {
										oneOption = (theForm[i].name).replace(/\w+_(\d+)/, "$1"); // get the prod_opt_id for later color checking
								}
						} catch( err ) {
								// not an object
								continue;
						}
				}
				// only one option .. or no options but then no add to cart button
				if ( oneOption ) {
						if ( !hasColor(oneOption) ) { // color check for one option
								var status = document.getElementById("checkboxError" );
								status.style.display = 'inline';
								status.innerHTML = '<div class="product_error">Please pick a color before adding to the cart.</div>';
								//				alert ( 'Please pick a color before adding to the cart.' );
								return false;
						} else
								return true;
				}
				// we have some checkboxes, thus some active options
				if ( options.length ) {
						var rtn = 1; // check for errors,  now that more than one opt can be added
						//for (var i in options) {
						for (var i = 0; i < options.length; i ++ ) {
								var prodOptId = options[i].replace(/\w+_(\d+)/, "$1"); // get prod_opt_id and check for color selection
								if ( !hasColor( prodOptId ) ) {
										rtn = 0;
										break;
								}
						}
						if ( document.getElementById("checkboxError" ) && rtn ) { //  no errors
								document.getElementById("checkboxError" ).style.display = 'none';
								return true;
						} else { // one or more errors
								var status = document.getElementById("checkboxError" );
								status.style.display = 'inline';
								status.innerHTML = '<div class="product_error">Please pick a color before adding to the cart.</div>';
								//				alert ( 'Please pick a color before adding to the cart.' );
								return false;
						}
				} else {
						var status = document.getElementById("checkboxError" );
						status.style.display = 'inline';
						status.innerHTML = '<div class="product_error">Select at least one option, then click Add to Cart</div>';


						//			alert( "Select at least one option, then click Add to Cart");
						return false;
				}
		}
		return true;
}

/* we now have a select-one or a bunch of color boxes */
function hasColor( prodOptId ) {
		/* Only here if an option is selected */
		var name = eval( "document.option_form.prod_opt_id_" + prodOptId + "_option" );
		if (name) {
				var el = eval( "document.option_form.prod_opt_id_" + prodOptId + "_option" );
				if ( el.type && el.type == 'select-one' ) {
						var index = el.selectedIndex;
						if ( name[index].value == 'choose' ) {
								return false;
						} else {
								return true;
						}
				} else if ( el.type && el.type == 'hidden') {
						if (el.value == '' || ( el.value == 'choose' ) ) {
								return false;
						} else {
								return true;
						}
				}
		} else {
				return true;
		}
}


function select_color_box (prod_opt_id, color_id, color_name ){
		try {
				$( 'qty_' + prod_opt_id + '_checkbox').checked=true;
		} catch (err) {}

		var others  = eval('divs_' + prod_opt_id);
		for( var i=0;i<others.length;i++) { /* go through all the valid div ids for this prod_opt */
				if ( $( others[i] ).id == 'color_box_' + prod_opt_id + '_' + color_id) {
						$( others[i] ).removeClass('color_default_border').removeClass( 'color_hover_border').addClass('color_selected_border' );
						$('prod_opt_id_' + prod_opt_id + '_option').value=color_id;
						$('color_name_title_' + prod_opt_id ).innerHTML= color_name;
				} else {
						if ( $( others[i] ) ) {
								$( others[i] ).removeClass('color_selected_border' ).addClass('color_default_border');
						}

				}
		}
}

/* checks that the color name, if selected is redisplayed after hovers */
function _check_name ( prod_opt_id ) {
		if ( $( 'prod_opt_id_' + prod_opt_id + '_option' ) ) {
				if ( $( 'prod_opt_id_' + prod_opt_id + '_option' ).value == 'choose' ) {
						$('color_name_title_' + prod_opt_id ).innerHTML = '';
				} else {
						var color_swap_array = eval('color_swap_' +  prod_opt_id);
						$('color_name_title_' + prod_opt_id ).innerHTML = color_swap_array[$( 'prod_opt_id_' + prod_opt_id + '_option' ).value];
						select_color_box(prod_opt_id, $( 'prod_opt_id_' + prod_opt_id + '_option' ).value,color_swap_array[$( 'prod_opt_id_' + prod_opt_id + '_option' ).value]  );
				}
		}
		return true;
}

function hover_color_name(el_name, color_name, color_box_el) {
		if ( $( el_name ) && $(color_box_el) ) {
				$(el_name).innerHTML = color_name;
				$(color_box_el).removeClass('color_default_border').addClass( 'color_hover_border');
				return true;
		}
		return false;
}

function reset_color_name_html(el_name, prod_opt_id, color_box_el ) {
		if ( $( el_name ) && $(color_box_el) ) {
				if ( ( $( 'prod_opt_id_' + prod_opt_id + '_option' ).value == '' ) || !$( 'prod_opt_id_' + prod_opt_id + '_option' ).value ) {
						$(el_name).innerHTML = '';
						$(color_box_el).removeClass('color_hover_border').removeClass('color_seleted_border').addClass( 'color_default_border');
				} else {
						_check_name( prod_opt_id );
				}
				$(color_box_el).removeClass('color_hover_border').addClass( 'color_default_border');
				return true;
		}
		return false;
}

function clear_color_border ( prod_opt_id ) {
		if ( prod_opt_id  && $( 'prod_opt_id_' + prod_opt_id + '_option' ) ) {
				$( 'prod_opt_id_' + prod_opt_id + '_option' ).value = '';
				if (eval('divs_' + prod_opt_id) ) {
						var others  = eval('divs_' + prod_opt_id);
						for( var i=0;i<others.length;i++) { /* go through all the valid div ids for this prod_opt */
								if ( $( others[i] ) ) {
										$( others[i] ).removeClass('color_selected_border' ).addClass('color_default_border');
								}
						}
				}
		}
}
