/* 
	 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
*/    

// safari doesn't like the for( var i in HtmlFormElement ) syntax
function checkboxCheck(formName) {
		//		return true;
		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 = "<strong>Please pick a color before adding to the cart.</strong>";
								//				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 = "<strong>Please pick a color before adding to the cart.</strong>";
								//				alert ( 'Please pick a color before adding to the cart.' );
								return false;
						}
				} else {
						var status = document.getElementById("checkboxError" );
						status.style.display = 'inline';
						status.innerHTML = "<strong>Select at least one option, then click Add to Cart</strong>";
						
						
						//			alert( "Select at least one option, then click Add to Cart");
						return false;
				}
		}
		return true;
}


// deprecated
function radioCheck(formName) {
	if ( formName == "option_form" ) {
		// some other form got passed in let server do the work
		if ( ! document.option_form.prod_opt_id ) {
			// no radios to select..shouldn't be a submit button..but just in case
			return true;
		} else if ( document.option_form.prod_opt_id.type ) { // One product to add to cart
			if ( document.option_form.prod_opt_id.type == 'radio' ) { // We have one radio and one or more inactive/unavailable products
				if ( document.option_form.prod_opt_id.checked ) { // one cover/bimini w/ color
					if ( ! hasColor( document.option_form.prod_opt_id.value ) ) {
						alert ( 'Please pick a color before adding to the cart.' ); 
						return false;
					}
					return true;
				} else {
					alert("Select an option, then click Add to Cart");
					return false;
				}
			} else { // one product only
				if ( ! hasColor( document.option_form.prod_opt_id.value ) ) { // one cover/bimini w/color
					alert ( 'Please pick a color before adding to the cart.' );
					return false;
				} else {
					return true;
				}
			}
		} else {
			// More then one radio, treat as NodeList
			for(i=0; i< document.option_form.prod_opt_id.length; i++ ) {
				if ( document.option_form.prod_opt_id[i].checked ) {
					// radio selected check if it needs a color 
					if ( ! hasColor( document.option_form.prod_opt_id[i].value ) ) {
						alert ( 'Please pick a color before adding to the cart.' );
						return false;
					} else {
						try { // has a color
							// has more than one color option
							if ( eval("document.option_form.prod_opt_id_" + document.option_form.prod_opt_id[i].value + "_option").length > 1 ) {
								var index = eval( "document.option_form.prod_opt_id_" + document.option_form.prod_opt_id[i].value + "_option" ).selectedIndex;
								var x = eval( "document.option_form.prod_opt_id_" + document.option_form.prod_opt_id[i].value + "_option" );
								return confirm ( "Add " + x[index].text + " color option to your cart?" ); 
							} else {
								return true;
							}
						} catch ( err ) { // no color..add to cart
							return true;
						}
					}
				}
			}
			// no radio selected
			alert("Select an option, then click Add to Cart");
			return false;
		}
	}
	return true;
}


function hasColor( prodOptId ) {
	/* Only here if an option is selected */
	var name = eval( "document.option_form.prod_opt_id_" + prodOptId + "_option" );
	if (name) {
		var index = eval( "document.option_form.prod_opt_id_" + prodOptId + "_option" ).selectedIndex;
		
		if ( name[index].value == 'choose' ) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}
