<!-- 
// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function cardval(s) {
// remove non-numerics
var v = "0123456789";
var w = "";
for (i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}

// validate number
j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;
k = Math.floor(j);
m = Math.ceil(j) - k;
c = 0;
for (i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}

function cf_isEmailAddr(str) {
	return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}

function navFormHandler(form){
	var URL = document.form.site.options[document.form.site.selectedIndex].value;
	window.location.href = URL;
}

function autocompleteoff() {
if (!document.getElementById) return false;
var f = document.getElementById('sub_combo');
var u = f.elements[0];
f.setAttribute("autocomplete", "off");
u.focus();
}

function checkForm() {
	var x;
	var i=0;
	var forbiddenRegExp = /[^\w'\-\.\$, @]/; //forbid any characters except alphanum ' - . , @
	for (x=0; x<document.sub_combo.elements.length; x++) {
		var xvalue = document.sub_combo[x].value;
		var xname = document.sub_combo[x].name;
		if (xvalue.length>35) {
				alert('\''+ xname.replace(/_/g," ") +'\' has too many characters (35 maximum). Please abbreviate as necessary and resubmit. Thanks!');
				document.sub_combo[x].className='error';
				document.sub_combo[x].focus();
				return false;
		}
// ========== MINIFORM (Borovsky) additional tests ===============
		if (xname.toUpperCase()==xvalue||xname=="Address"&&xvalue=="ADDRESS 1") {
			alert('Please provide your actual '+ xname +'.');
			document.sub_combo[x].className='error';
			document.sub_combo[x].focus();
			return false;
		}
// ========== END MINIFORM (Borovsky) additional tests =++========

		if (forbiddenRegExp.test(xvalue)) {
			alert('\''+ xname.replace(/_/g," ") +'\' contains illegal characters. Please use ONLY letters, numbers, or these characters: \' - , . @');
			document.sub_combo[x].className='error';
			document.sub_combo[x].focus();
			return false;
		}
		if (xname.substr(xname.length-3)!="opt") { //test last 3 letters
			if ((xname=='Email'||xname=='Phone') && !xvalue) {
				if (xname=='Phone') {
					i++;
				} else if (xname=='Email' && i==1) {
					alert('We need either a phone number or email where we can reach you in case of a question about your order. Thanks!');
					document.sub_combo[x].className='error';
					document.sub_combo[x].focus();
					return false;
				}
			} else if (xname.substr(xname.length-4)=='term' && document.sub_combo[x].selectedIndex==0) {
				alert('Please choose a duration for your gift and resubmit.');
				document.sub_combo[x].className='error';
				document.sub_combo[x].focus();
				return false;
			} else if (!xvalue) {
				alert('\''+ xname.replace(/_/g," ") +'\' is empty; we need this info to complete your order. Thanks!');
				document.sub_combo[x].className='error';
				document.sub_combo[x].focus();
				return false;
			}
		}
		if (xname=='Creditcard' && !cardval(xvalue)) {
			alert('Invalid credit card number; please change and resubmit.');
			document.sub_combo[x].className='error';
			document.sub_combo[x].focus();
			return false;
		}
		if (xname=='Email' && xvalue!='' && !cf_isEmailAddr(xvalue)) {
			alert('Not a valid email address; please fix and resubmit.');
			document.sub_combo[x].className='error';
			document.sub_combo[x].focus();
			return false;
		}
		if (xname=='Email') { var Email_holder=xvalue; }
		if (xname=='Email_chk_opt') {
			if (xvalue!=Email_holder) {
				alert('Email and email check fields differ; please fix.');
				document.sub_combo[x].className='error';
				document.sub_combo[x].focus();
				return false;
			}
		}
		document.sub_combo[x].className='';
	}
// If all fields check OK, submit form
	return true;
}


// for SUBSCRIBE form
function visible(x) {document.getElementById(x).style.display="table-row";}
function invisible(x) {document.getElementById(x).style.display="none";}
function giftcount(x) {document.subform.giftcounter.value=x;}

var self_counter = 0;
var counter = 0;
var limit = 24;

function removeElement(childDiv){
	if (document.getElementById(childDiv)) {     
	  var child = document.getElementById(childDiv);
	  var parent = document.getElementById('giftsubs_holder');
	  parent.removeChild(child);
	  
	  counter--;
	}
}

function addgift(tdName){
	var coa_flag = "<?php echo $subsection; ?>";
	if (counter == limit)  {
		alert("This form can only handle " + counter + " gift subscriptions -- please call the Funny Times subscription department toll-free at 1.888.386.6984 for assistance, or submit this form then return to it to add your next batch of gift subscriptions.");
	} else {
		if (self_counter>1) {
			self_counter=1;
		}
		counter++;
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id','child'+counter);
		newdiv.setAttribute('style','width:480px');

		newdiv.innerHTML = 	"<br /><hr />";
		
		if (coa_flag == "change"){

			newdiv.innerHTML += 	"<div>Gift "+counter+" Subscriber No.: <input type=\"text\" name=\"Gift_"+counter+"_Subscriber_Number_opt\" id=\"Gift_"+counter+"_Subscriber_Number_opt\" size=\"35\">";
			newdiv.innerHTML += 	"</div>";

		} else {

			newdiv.innerHTML = 	"<div class=\"required\">* Gift #"+counter+": ";

			if ((counter+self_counter)>6){ /* 7 or more at a time cheaper*/
				newdiv.innerHTML += "<select name=\"Gift_"+counter+"_term\" id=\"Gift_"+counter+"_term\"><option value=\"\"> -how many years?- </option><option value=\"1 year $20\">1 year [12 issues] = $20</option><option value=\"2 year\">2 year [24 issues] = $42</option><option value=\"3 year\">3 year [36 issues] = $60</option></select>";
			} else if ((counter+self_counter)==6){ /* free 6th issue*/
				newdiv.innerHTML += "<select name=\"Gift_"+counter+"_term\" id=\"Gift_"+counter+"_term\"><option value=\"\"> -how many years?- </option><option value=\"1 year FREE\">1 year [12 issues] = FREE!</option><option value=\"2 year\">2 year [24 issues] = $42</option><option value=\"3 year\">3 year [36 issues] = $60</option></select>";
			} else { /* ...or regular price */
				newdiv.innerHTML += "<select name=\"Gift_"+counter+"_term\" id=\"Gift_"+counter+"_term\"><option value=\"\"> -how many years?- </option><option value=\"1 year\">1 year [12 issues] = $25</option><option value=\"2 year\">2 year [24 issues] = $42</option><option value=\"3 year\">3 year [36 issues] = $60</option></select>";
			}
			newdiv.innerHTML += 	"</div>";

			newdiv.innerHTML += 	"<div>Is this subscription a renewal? <input type=\"checkbox\" name=\"Gift_"+counter+"_renewal\" id=\"Gift_"+counter+"_renewal\" value=\"Yes\">Yes";
			newdiv.innerHTML += 	"</div>";

			newdiv.innerHTML += 	"<div>";
			newdiv.innerHTML += 	"Start Gift: ";
			var month_name=new Array(12);
			month_name[0]="January"
			month_name[1]="February"
			month_name[2]="March"
			month_name[3]="April"
			month_name[4]="May"
			month_name[5]="June"
			month_name[6]="July"
			month_name[7]="August"
			month_name[8]="September"
			month_name[9]="October"
			month_name[10]="November"
			month_name[11]="December"
			var i=2;
			var now = new Date();
			var thismo = "";
			var nextmo = "";
			var nextyr = "";
			var option_months = "";
			while (i<13){ 
				thismo = month_name[now.getMonth()+i];
				nextmo = month_name[now.getMonth()+i+1];
				if (thismo==undefined) {thismo = month_name[now.getMonth()+i-12];nextyr = now.getFullYear()+1;}
				if (nextmo==undefined) {nextmo = month_name[now.getMonth()+i-11]}
				option_months += "<option value=\""+nextmo+" issue\">in "+thismo+" "+nextyr+"</option>";
				i++;
			}



			newdiv.innerHTML += 	"<select name=\"Gift_"+counter+"_start\" id=\"Gift_"+counter+"_start\"><option value=\"next issue\">with next issue</option><option value=\"after current sub expires\">after current subscription expires</option>"+option_months+"</div>";
		}

		newdiv.innerHTML += 	"<div class=\"required\">* First Name: <input type=\"text\" required=\"true\" name=\"Gift_"+counter+"_First_name\" id=\"Gift_"+counter+"_First_name\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div class=\"required\">* Last Name: <input type=\"text\" required=\"true\" name=\"Gift_"+counter+"_Last_name\" id=\"Gift_"+counter+"_Last_name\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div>Email: <input type=\"text\" name=\"Gift_"+counter+"_Email_opt\" id=\"Gift_"+counter+"_Email_opt\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div class=\"required\">* Address1: <input type=\"text\" required=\"true\" name=\"Gift_"+counter+"_Address\" id=\"Gift_"+counter+"_Address\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div>Address2: <input type=\"text\" name=\"Gift_"+counter+"_Address2_opt\" id=\"Gift_"+counter+"_Address2_opt\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div class=\"required\">* City: <input type=\"text\" required=\"true\" name=\"Gift_"+counter+"_City\" id=\"Gift_"+counter+"_City\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div class=\"required\">* State: <input type=\"text\" required=\"true\" name=\"Gift_"+counter+"_State\" id=\"Gift_"+counter+"_State\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div class=\"required\">* ZIP: <input type=\"text\" required=\"true\" name=\"Gift_"+counter+"_Zip\" id=\"Gift_"+counter+"_Zip\" size=\"35\">";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div>Country: <input type=\"text\" name=\"Gift_"+counter+"_Country_opt\" id=\"Gift_"+counter+"_Country_opt\" size=\"35\">";
		newdiv.innerHTML += 	"<br />[Each subscription sent outside the US adds $20/year]";
		newdiv.innerHTML += 	"</div>";

		newdiv.innerHTML += 	"<div>"; /* add gift button */
		newdiv.innerHTML += 	"<input type=\"hidden\" name=\"giftNumPass\" id=\"giftNumPass\" value=\""+counter+"\">"; /* PASS gift count as hidden input */

		if ((counter+self_counter)==5) { /* conditional for 5th gift new message */
			newdiv.innerHTML += 	"<input name=\"bt_addGift_"+counter+"\" id=\"bt_addGift_"+counter+"\" type=\"button\" value=\"Click here - FREE gift awaits\" onClick=\"addgift(\'giftsubs_holder\');invisible(\'bt_addGift_"+counter+"\');invisible(\'bt_removeGift_"+counter+"\');\">";
		} else {
			newdiv.innerHTML += 	"<input name=\"bt_addGift_"+counter+"\" id=\"bt_addGift_"+counter+"\" type=\"button\" value=\"add another friend\" onClick=\"addgift(\'giftsubs_holder\');invisible(\'bt_addGift_"+counter+"\');invisible(\'bt_removeGift_"+counter+"\');\">";
		}
		if (counter==1) { /* if first gift, revisible initial, hardcoded addgift button */
			newdiv.innerHTML += 	" <input name=\"bt_removeGift_"+counter+"\" id=\"bt_removeGift_"+counter+"\" type=\"button\" value=\"oops - remove this gift\" onClick=\"removeElement(\'child"+counter+"\');visible(\'bt_first_gift\');\">";
		} else { /* revisible last on-the-fly addgift button */
			newdiv.innerHTML += 	" <input name=\"bt_removeGift_"+counter+"\" id=\"bt_removeGift_"+counter+"\" type=\"button\" value=\"oops - remove this gift\" onClick=\"removeElement(\'child"+counter+"\');visible(\'bt_addGift_"+(counter-1)+"\');visible(\'bt_removeGift_"+(counter-1)+"\');\">";
		}
		newdiv.innerHTML += 	"</div>";
		
		if ((counter+self_counter)==3) { /* conditional for SIXPACK reminder */
			newdiv.innerHTML += 	"<br /><hr /><div>FREE Gift Offer: Give the gift of laughter <br />to two more friends - five subscriptions total - <br />and a sixth one-year subscription is on us.</div>";

		} else if ((counter+self_counter)==4) { /* conditional for SIXPACK reminder */
			newdiv.innerHTML += 	"<br /><hr /><div>Buy one more gift and get one FREE!</div>";

		} else if ((counter+self_counter)==6) { /* post-SIXPACK bulk discount */
			newdiv.innerHTML += 	"<br /><hr /><div>WHY STOP when you're on a roll? <br />You can order as many more 1-year subscriptions <br />as you like for only $20 each. And thanks <br />for spreading the gift of laughter so generously!</div>";
		}
		document.getElementById(tdName).appendChild(newdiv);
	}
}

// Borovsky MINIFORM field value flipper (blanks on focus, restores default on defocus)
//This credit must stay intact
//Script by http://www.java-Scripts.net and http://wsabstract.com
 function fieldClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }
 function fieldRestore(theText) {
     if (theText.value == "") {
         theText.value = theText.defaultValue
     }
 }

//-->
