function submit_handler (form) {
	var status = true;
	if ( form == 'f1' ) {
		form =  document.forms[form];
	}
	status = validate_data(form);
	if (status) {
		status = check_project_reference(form);
	} 
	if (status) {
		status = checkSelections(form);
	} 
	if (status) {
		form.submit();
	} 
	return status;
}

function edit_submit_handler (form) {
	var status = true;
	status = validate_data(form);
	if (status) {
		status = checkSelections(form);
	} 
	if (status) {
		form.submit();
	} 
	return status;
}

function reuse_handler (form) {
	var status = true;
	status = validate_reuse(form);
	if (status) {
		status = check_project_reference(form);
	}
	if (status) {
		form.submit();
	}
}

function predefined_handler (form) {
	var status = true;
	status = validate_predefined(form);
	if (status) {
		status = check_project_reference(form);
	}
	if (status) {
		form.submit();
	}
}

function quantity_validation (qty_elem) {
	var qty_tmp = qty_elem.value;
	// handles preceeding zeros when editing. (otherwise interpreted as a hex value with parseInt)
	qty_elem.value = Math.floor(qty_elem.value);
	// error handling if rounding failed
	if (isNaN(qty_elem.value)) {
		qty_elem.value = qty_tmp;
		qty_elem.select();
		return false;
	}
	return true;
}


function validate_data (form) {
	var text = '';
	var pressType = false;
	if (form.rdbPressType) {
		for (var index = 0; index <form.rdbPressType.length; index++){
			if (form.rdbPressType[index].checked) {
				pressType = true;
				break;
			}
		}
	} else {
		pressType = true;
	}
	if ( form.rdbPressType && form.rdbPressType.type == 'hidden'){
		pressType = true;
	}
	if ( ! pressType) {
		text += "Please select the press type.\n";
	}
	var ptype = false;

	if (form.rdbProjectType && form.rdbProjectType.length) {
		for ( var index = 0; index < form.rdbProjectType.length; index += 1 ) {
			if ( form.rdbProjectType[index].checked ) {
				ptype = true;
				break;
			} 
		} 
	}
	else { ptype = 'predefined'; }
	if ( ! ptype ) {
		text += "Please select the type of project.\n";
	}
	var mode = false;
	if (form.rdbMore) {
		for ( var index = 0; index < form.rdbMode.length; index += 1 ) {
			if ( form.rdbMode[index].checked ) {
				mode = true;
				break;
			} 
		} 
	} else mode = 'predefined';
	if ( ! mode ) {
		text += "Please select a project creation method from Basic Specifications or Detailed Specifications.\n";
		//text += "Please select a project creation method from Pre-defined, Basic Specifications or Detailed Specifications.\n";
	}
	var additional_quantities = [];
	if (form.txtQuantity1 && form.txtQuantity1.value) {
		if ( parseInt(form.txtQuantity1.value) != form.txtQuantity1.value || !quantity_validation(form.txtQuantity1)) {
			text += "The field 'Quantity 1' may only contain whole numbers greater than 1.\n";
		}
	} 
	if (form.txtQuantity2 && form.txtQuantity2.value) {
		if ( parseInt(form.txtQuantity2.value) != form.txtQuantity2.value || !quantity_validation(form.txtQuantity2)) {
			text += "The field 'Quantity 2' may only contain whole numbers greater than 1.\n";
		}
	}
	if (form.txtQuantity3 && form.txtQuantity3.value) {
		if ( parseInt(form.txtQuantity3.value) != form.txtQuantity3.value || !quantity_validation(form.txtQuantity3)) {
			text += "The field 'Quantity 3' may only contain whole numbers greater than 1.\n";
		}
	}
	if (form.txtQuantity1 && ! form.txtQuantity1.value) {
		text += "Please enter a value for Quantity 1.\n";
	}
	if (form.txtProjectReference && (form.txtProjectReference.value == "" || form.txtProjectReference.value == "ex: ABC Company - Brochure")) {
		text += "Please give your project a reference name.\n";
	}
	
	if (text) {
		text = "Please correct the following errors before proceeding:\n\n" + text;
		alert(text);
		return false;
	}
	
	return true;
}

function validate_predefined (form) {
	var text = '';
	if (form.txtQuantity1.value) {
		if ( parseInt(form.txtQuantity1.value) != form.txtQuantity1.value) {
			text += "The field 'Quantity 1' may only contain whole numbers greater than 1.\n";
		} 
	} 
	if (form.txtQuantity2.value) {
		if ( parseInt(form.txtQuantity2.value) != form.txtQuantity2.value) {
			text += "The field 'Quantity 2' may only contain whole numbers greater than 1.\n";
		} 
	} 
	if (form.txtQuantity3.value) {
		if ( parseInt(form.txtQuantity3.value) != form.txtQuantity3.value) {
			text += "The field 'Quantity 3' may only contain whole numbers greater than 1.\n";
		} 
	} 
	if (!form.txtQuantity1.value) {
		text += "Please enter a quantity 1 for your project.\n";
	} 
	if ( form.txtProjectReference.value == "" || form.txtProjectReference.value == "ex: ABC Company - Brochure") {
		text += "Please give your project a reference name.\n";
	} 
	if (text) {
		text = "Please correct the following errors before proceeding:\n\n" + text;
		alert(text);
		return false;
	} 
	return true;
}

function validate_reuse (form) {
	var text = '';
	if ( form.txtProjectReference.value == "" || form.txtProjectReference.value == "ex: ABC Company - Brochure") {
		text += "Please give your project a reference name.\n";
	} 
	if (text) {
		text = "Please correct the following errors before proceeding:\n\n" + text;
		alert(text);
		return false;
	} 
	return true;
}

function checkSelections (form){
	if (form.rdbProjectType) {
	var projectType = get_rdb_value(form,'rdbProjectType');
		if ( projectType == 'ScratchPads' ) {
			if ( form.txtQuantity1.value > 5000 || form.txtQuantity2.value > 5000 || form.txtQuantity3.value > 5000 ) {
				alert("The maximum quantity of scratch pads per project is 5000. Please reduce all quantities to 5000 or less.");
				return false;
			} 
		} 
	}
	//var projectService = get_rdb_value(form,'rdbShipping');
	//if ( projectService == 'CourierLTLFreight' ) {
	//	if ( ! confirm ("Please click OK to calculate your project without a shipping price and an administrator will contact you to arrange the most cost effective way to ship your project(s).\n\nOr click Cancel to select a different shipping method.")) {
		// if the click Cancel on the pop-up, then return false to cancel the submit
	//	return false;
	//	} 
	//} 
	//commented out the above as we're revamping shipping on project create page - Duke
	return true;
}

function check_project_reference(form){
	if ( ! form.txtProjectReference ) {
		return true;
	}
	var reference = form.txtProjectReference.value;
	for (var i=0; i < projectReferences.length; i++ ) {
		if ( reference == projectReferences[i] ) {
			alert("The project reference you have entered is already in use by your active browser session, please enter a new reference for your project");
			return false;
		} 
	} 
	return true;
}

function get_rdb_value( form, rdbName ) {
    for ( var x = 0; x < form.elements[rdbName].length; x ++ ) {
        if ( form.elements[rdbName][x].checked == true ) {
            return form.elements[rdbName][x].value;
        } 
    } 
    return '';
} 

// Yet another custom little function to do form dependency stuff. This one
// displays or hides the "other program" question.
addEvent(window, 'load', design_format);
function design_format (e) {
    var form = document.forms['f1'];
    if (!form || !form['format']) return null;

    // Display the other program question based on the chosen option.
    form['format'].onchange = other_program;
    // Run it now to hide or show the question as needed.
    other_program.apply(form['format']);
    return true;
}

// Display or hide the other program question based on the format selection.
function other_program (e) {
    var question = document.getElementById('other_program');

    if (this.options[this.selectedIndex].value == 'Other')
        question.style.display = '';
    else
        question.style.display = 'none';
}
