// JavaScript Document

function doSubmit(){
	var d = document.forms[0];
	var isGood = true;
	var e;
	
	/* validate each required input (input[type=text]) */
	for (i=0; i<d.elements.length; i++){
		e = d.elements[i];
		if (e.type == 'text'){
			if (e.value == '') {
				//one exception; they don't have to fill out the second address line
				if (e.name != 'company_addr2'){
					isGood = false;
					break;
				}
			}
		}
	}
	
	if (!isGood){
		alert('Please fill in all the required information.');
		e.focus();
		return;
	}
	
	/* make sure the minimum quantity has been ordered -- a combined total of 12 hickmans and/or ports */
	isGood = ((Number(d.port_quantity.value) + Number(d.hickman_quantity.value)) >= 12);
	if (!isGood){
		alert('You must order a total of at least 12 Ports and/or Hickmans.');
		d.port_quantity.focus();
		return;
	}
	
	//make sure the email is formatted in a valid way
	if (!isEmailValid(d.hosp_email.value)){
		alert(d.hosp_email.value + " is not a valid email address. Please enter a valid one.");
		d.hosp_email.focus();
		return;
	}
	if (!isEmailValid(d.company_email.value)){
		alert(d.company_email.value + " is not a valid email address. Please enter a valid one.");
		d.company_email.focus();
		return;
	}
	
	//otherwise, submit the form
	d.submit();
}


function isEmailValid(email){
	var isGood = true;
	
	//check for presence of @ symbol
	isGood = (email.indexOf('@') > -1);
	if (!isGood){ return false; }
	
	
	//check for valid domain types: com, edu, org, net, gov, aero, biz, coop, tv, info, museum, name, pro
	var extensionList = "com,edu,org,net,gov,aero,biz,coop,tv,info,museum,name,pro";
	var arExtensions = extensionList.split(',');
	//set value of isGood to false for teh for-next loop
	isGood = false;
	var ext;
	for (i=0; i<arExtensions.length; i++){
		ext = '.' + arExtensions[i];	
		isGood = (email.indexOf(ext) > -1);
		if (isGood){ break; }
	}
	return isGood;
}

function populateForm(){
	var sUrl = location.href;
	var d = document.forms[0];
	var pos, sParams, isDebug;
	
	//check to see if there are any parameters in the URL
	pos = sUrl.lastIndexOf("?");
	if (pos >= 0) {
		sParams = sUrl.substr(pos+1);
		//now, we should have a string that looks like param1=value1&param2=value2, but we're looking for debug=1
		pos = sParams.indexOf('debug');
		if (pos >= 0){
			//populate fields with test values
			d.hospital.value = "TEST Hospital";
			d.hosp_contact.value = "Larry Larry";
			d.hosp_city.value = "Nashville";
			d.hosp_state.value = "TN";
			d.hosp_zip.value = "37203";
			d.hosp_phone.value = "615.555.5555";
			d.hosp_email.value = "seclemens@live.com";
			d.port_quantity.value = 99;
			d.hickman_quantity.value = 7;
			d.company_name.value = "TEST Company";
			d.company_attn.value = "Larry Larry, Jr.";
			d.company_addr1.value = "123 Channing Street.";
			d.company_addr2.value = "";
			d.company_city.value = "Nashville";
			d.company_state.value = "TN";
			d.company_zip.value = "37212";
			d.company_phone.value = "615.555.5556";
			d.company_email.value = "seclemens@live.com";
			d.initials.value = "LLJ";
			d.comments.value = "THIS IS A TEST.";
			
			
		}
		
	}
}