/* Form Functions
 * $Id: forms.js,v 1.9 2005/11/29 15:17:21 justin Exp $
 */

var regex_fax = /[0-9\s\-\(\)\.]{7,}/;
var regex_email = /(([A-Za-z0-9_\-\.]+)@([A-Za-z0-9\+_\-])+\.([A-Za-z0-9\+_\-\.])+)/;
var uploading = false;
var uploadClicked = false;

// toggle upload / stop upload in Java applet
function toggleUpload(btn) {
	var msg = "You have not selected any files to upload.  You must select AT LEAST ONE FILE before proceeding.\nYou can select images, Word documents, PDFs, and many more formats (click the 'Help' link above for a more complete list).\n\n**NOTE** If we are designing your ad for you, you should supply us with assets we might need, such as your company's logo.\n\nTo select files, click on the 'Add...' button and choose files or folders from your computer (hold the Ctrl key to select more than one file).  You can also drag and drop files onto the white box above to add them to the list.\n\nTo remove files from your list, select them (hold Ctrl to select more than one) and click the 'Remove' button.";

	// prevent double-clicking
	if (uploadClicked) return;
	uploadClicked = true;

	if (!uploading) {
		// check if files have been added to the list
		if (document.JUpload.jsIsReady() && document.JUpload.jsGetFileNumber() > 0) {
			document.JUpload.jsClickUpload();
			uploading = true;
			btn.value = "- Stop Uploading -";
		} else {
			alert(msg);
		}

	} else {
		document.JUpload.jsClickStop();
		uploading = false;
		btn.value = "Upload -->";
	}

	uploadClicked = false;
}

// toggle upload / stop upload in Java applet
function toggleUpload2(btn) {
	var msg = "You have not selected any files to upload.  You must select AT LEAST ONE FILE before proceeding.\nYou can select images, Word documents, PDFs, and many more formats (click the 'Help' link above for a more complete list).\n\n**NOTE** If we are designing your ad for you, you should supply us with assets we might need, such as your company's logo.\n\nTo select files, click on the 'Add...' button and choose files or folders from your computer (hold the Ctrl key to select more than one file).  You can also drag and drop files onto the white box above to add them to the list.\n\nTo remove files from your list, select them (hold Ctrl to select more than one) and click the 'Remove' button.";

	// check if files have been added to the list
	if (document.JUpload.isInitialized() && !document.JUpload.isTransferring() && document.JUpload.getFileCount() > 0) {
		document.JUpload.startTransfer();
	} else {
		alert(msg);
	}
}

// set the values for the selected publication and issue
function setPubIssue(fm,delim) {
	var str = getFormValue(fm.pub);

	str = str.split(delim);

	fm.publication.value = str[0];
	fm.issue.value = str[1];
}

// check form
function checkForm(fm) {
	var msg = "You must fix the following error(s) before you can continue:";
	var good = true;
	var fld = false;

	// reset bad field markings
	for (var i=0; i<fm.length; i++) markField(fm.elements[i], false);
	markField(document.getElementById('sendFax'), false);
	markField(document.getElementById('sendEmail'), false);

	// do we have an advertiser name?
	if (getFormValue(fm.advertiser) == "") {
		// warn user
		good = false;
		msg += "\n- Advertiser name is missing";
		markField(fm.advertiser, true);
		if (!fld) fld = fm.advertiser;
	}

	// do we have a publication?
	if (getFormValue(fm.pub) == "") {
		// warn user
		good = false;
		msg += "\n- Publication/Issue is not selected";
		markField(fm.pub, true);
		if (!fld) fld = fm.pub;
	}

	// do we have a contact first name?
	if (getFormValue(fm.contactFName) == "") {
		// warn user
		good = false;
		msg += "\n- Contact person First Name is missing";
		markField(fm.contactFName, true);
		if (!fld) fld = fm.contactFName;
	}

	// do we have a contact last name?
	if (getFormValue(fm.contactLName) == "") {
		// warn user
		good = false;
		msg += "\n- Contact person Last Name is missing";
		markField(fm.contactLName, true);
		if (!fld) fld = fm.contactLName;
	}

	// do we have a contact phone?
	if (getFormValue(fm.contactPhone) == "") {
		// warn user
		good = false;
		msg += "\n- Contact Phone Number is missing";
		markField(fm.contactPhone, true);
		if (!fld) fld = fm.contactPhone;
	}

	// do we have a contact email or contact email verify?
	if (getFormValue(fm.contactEmail) != "" || getFormValue(fm.contactEmail2) != "") {
		// is the contact email valid?
		if (fm.contactEmail.value.match(regex_email) == null) {
			// warn user
			good = false;
			msg += "\n- Contact Email is incomplete or invalid";
			markField(fm.contactEmail, true);
			if (!fld) fld = fm.contactEmail;
		} else {
			// do they match?
			if (getFormValue(fm.contactEmail) != getFormValue(fm.contactEmail2)) {
				// warn user
				good = false;
				msg += "\n- Contact Emails do not match";
				markField(fm.contactEmail2, true);
				if (!fld) fld = fm.contactEmail2;
			}
		}
	}

	// do we have a proof email or proof email verify?
	if (getFormValue(fm.proofEmail) != "" || getFormValue(fm.proofEmail2) != "") {
		// is the proof email valid?
		if (fm.proofEmail.value.match(regex_email) == null) {
			// warn user
			good = false;
			msg += "\n- Proofing Email is incomplete or invalid";
			markField(fm.proofEmail, true);
			if (!fld) fld = fm.proofEmail;
		} else {
			// do they match?
			if (getFormValue(fm.proofEmail) != getFormValue(fm.proofEmail2)) {
				// warn user
				good = false;
				msg += "\n- Proofing Emails do not match";
				markField(fm.proofEmail2, true);
				if (!fld) fld = fm.proofEmail2;
			}
		}
	}

// debugging for Safari
fm.sendFax.value = fm.sendFax2.checked ? 1 : 0;
fm.sendEmail.value = fm.sendEmail2.checked ? 1 : 0;

	// check for proofing options
	if (getFormValue(fm.sendFax) != "1" && getFormValue(fm.sendEmail) != "1") {
		good = false;
		msg += "\n- You have not selected a method to receive proofs";
		markField(document.getElementById('sendFax'), true);
		markField(document.getElementById('sendEmail'), true);
		if (!fld) fld = fm.sendFax2;
	}

	// check for proof fax
	if (getFormValue(fm.sendFax) == "1" && fm.contactFax.value.match(regex_fax) == null && fm.proofFax.value.match(regex_fax) == null) {
		good = false;
		msg += "\n- You have selected to receive proofs via Fax but the Fax number is missing, incomplete or invalid";
		markField(fm.contactFax, true);
		if (!fld) fld = fm.contactFax;
	}

	// check for proof email
	if (getFormValue(fm.sendEmail) == "1" && fm.contactEmail.value.match(regex_email) == null && fm.proofEmail.value.match(regex_email) == null) {
		good = false;
		msg += "\n- You have selected to receive proofs via Email but the Email is missing, incomplete or invalid";
		markField(fm.proofEmail, true);
		if (fm.proofEmail2.value.match(regex_email) == null) markField(fm.proofEmail2, true);
		if (!fld) fld = fm.proofEmail;
	}

	// check for terms agree
	if (!fm.agreeTerms.checked) {
		good = false;
		msg += "\n- You must read and agree to the Terms of Service";
		markField(document.getElementById('agreeTerms'), true);
		if (!fld) fld = fm.agreeTerms;
	}


	// show error
	if (!good) {
		alert(msg);
		fld.focus();
	}

	return good;
}


// mark/unmark a field
function markField(fld, on) {
	if (fld.className == "button" || fld.type == "radio" || fld.type == "checkbox") return;
	fld.className = on ? "missing" : "field";
}


// clear the value of a select box
function clearSelect(ctl) {
	for (var i=0; i<ctl.options.length; i++) ctl.options[i].selected = false;
}


// set the value of a form field
function setFormValue(fld,val) {
		switch (fld.type) {
			case "radio":
				setRadio(fld,val);
				break;

			case "select-one":
			case "select-multiple":
				setOption(fld,val);
				break;

			case "checkbox":
				fld.checked = val;
				break;

			case "textarea":
			case "text":
			case "password":
				fld.value = val;
				break;
		}
}

// get the value of a form field
function getFormValue(fld) {
		switch (fld.type) {
			case "radio":
				return getRadio(fld);
				break;

			case "select-one":
			case "select-multiple":
				return getOption(fld);
				break;

			case "checkbox":
				return fld.checked;
				break;

			case "hidden":
			case "textarea":
			case "text":
			case "password":
			default:
				return fld.value;
				break;
		}
}

// set the selection of a select box or radio button group
function setOption(ctl,val) {
	for (var i=0; i<ctl.options.length; i++) ctl.options[i].selected = ctl.options[i].value == val;
}

// get the selected value of a select box
function getOption(ctl) {
	for (var i=0; i<ctl.options.length; i++) if (ctl.options[i].selected)  return ctl.options[i].value;
}

// set the selection of a radio button set
function setRadio(ctl,val) {
	for (var i=0; i<ctl.length; i++) ctl[i].checked = ctl[i].value == val;
}

// return the selected value of a radio button set
function getRadio(ctl) {
	for (var i=0; i<ctl.length; i++) if (ctl[i].checked) return ctl[i].value;
}

