/*
 * Live Oak Interactive, 2007
 * This code is specifically to manage the Gift of Green forms.
 */

var GOG = {
	gog_check : function(){
		// Iterate throught the Gift of Green forms and verify that all required fields are there

		var gog_count = 0;
		for(i=0; i<document.getElementById('orderform').elements.length; i++){
			// Determine if the current form element is one of our gog elements
			//document.orderform.elements[i].name = product_options[444]
			names_index = document.getElementById('orderform').elements[i].name.replace('product_options', '').replace('[', '').replace(']', '');

			try{
				field_name = names[names_index].class_name;
				if(field_name.indexOf('gog_') !== -1){
					gog_count++;
					// It's a gog field
					if(document.getElementById('orderform').elements[i].value == '' && gog_count > 2){
						alert_text = field_name.replace('gog_', '');
						alert(alert_text.substr(0,1).toUpperCase() + alert_text.substr(1,alert_text.length) + ' is a required field.');
						return false;
					} // end if(document.orderform.elements[i].value == '')
					if(document.getElementById('orderform').elements[i].value.length > 150){
						alert_text = field_name.replace('gog_', '');
						alert(alert_text.substr(0,1).toUpperCase() + alert_text.substr(1,alert_text.length) + ' is limited to 150 characters.');
						return false;
					}
					if(field_name == 'gog_note'){
						var gog_note = document.getElementById('orderform').elements[i];
						gog_note.value = gog_note.value.replace(/(\r\n|\r|\n)/g, " ");
					}
				} // end if(field_name.indexOf('gog_') !== -1)
			}
			catch(e){
				// pass
				//alert('Received error: ' + e);
			}

		} // end for(i=0; i<document.orderform.elements.length; i++)

		return true;
	}, // end function gog_check()


	max_length_ta : function(field, maxChars){
		//alert(field.innerHTML);
		if(field.value.length >= maxChars){
			field.value = field.value.substring(0, maxChars);
		} // end if(field.innerHTML.length >= maxChars)
		return true;
	},

	max_length : function(field, maxChars) {
		if (field.value.length >= maxChars){
			field.value = field.value.substring(0, maxChars);
		} // end if (field.value.length >= maxChars)
		return true;
	}, // end max_length : function(field,maxChars)

	gog_set_form_field_properties : function () {
		if(document.getElementById('orderform')){
			var input_elements=document.getElementsByTagName("input");

			for(var i=0;i<input_elements.length;i++){
				if(input_elements[i].name.indexOf('product_options') !== -1){
					// It's a product extra

					names_index = input_elements[i].name.replace('product_options', '').replace('[', '').replace(']', '');

					try{
						// The names variable comes from X-Cart and will be loaded above.
						field_name = names[names_index].class_name;
						//alert(field_name);
						if(field_name.indexOf('gog_') !== -1){
							// It's a gog field

							if(field_name == 'gog_recipient'){
								input_elements[i].onkeypress = function () {
									return GOG.max_length(this,"80");
								};
							} // end if(field_name == 'gog_recipient')

							if(field_name == 'gog_address'){
								input_elements[i].onkeypress = function () {
									return GOG.max_length(this,"120");
								};
							} // end if(field_name == 'gog_address')

							if(field_name == 'gog_city'){
								input_elements[i].onkeypress = function () {
									return GOG.max_length(this,"120");
								};
							} // end if(field_name == 'gog_city')

							if(field_name == 'gog_zip'){
								input_elements[i].onkeypress = function () {
									return GOG.max_length(this,"5");
								};
							} // end if(field_name == 'gog_zip')

						} // end if(field_name.indexOf('gog_') !== -1)
					}
					catch(e){
						// pass
						//alert('Received error: ' + e);
					}
				} // end if(input_elements[i].name.indexOf('product_options'))
			} // end for(var i=0;i<input_elements.length;i++)


			// Now do the text areas

			var text_area_elements=document.getElementsByTagName("textarea");

			for(var i=0;i<text_area_elements.length;i++){
				if(text_area_elements[i].name.indexOf('product_options') !== -1){
					names_index = text_area_elements[i].name.replace('product_options', '').replace('[', '').replace(']', '');

					try{
						// The names variable comes from X-Cart and will be loaded above.
						field_name = names[names_index].class_name;
						//alert(field_name);
						if(field_name.indexOf('gog_') !== -1){
							// It's a gog field

							if(field_name == 'gog_note'){
								text_area_elements[i].onkeypress = function () {
									return GOG.max_length_ta(this,"149");
								};
							} // end if(field_name == 'gog_note')

						} // end if(field_name.indexOf('gog_') !== -1)
					}
					catch(e){
						// pass
					}

				} // end if(input_elements[i].name.indexOf('product_options') !== -1)
			} // end for(var i=0;i<text_area_elements.length;i++)




			return true;
		} else {
			return false;
		} // end if(document.getElementById('orderform'))
	} // end gog_set_form_field_properties : function ()

} // end var GOG

GOG.gog_set_form_field_properties();
