function validate(form,isUser) 
{
	var validOK = true;
	var formType = form.type.value;
	
	if (formType != "cnf")
		var formsubPhone = form.subPhone.value;

	switch(formType){
		case "aw":
			break;
		case "ComeBack":
			break;
		case "onedaypass":
			break;
		case "register":
			break;
		case "conf":
			break;
		case "generic":
			break;
		case "cnf":
			break;
		case "cnf_trial":
			break;
		case "fc":
			break;
		case "two_months":
			break;
		default:
			//sendT();
	}
	
	with (form){
		if (!isUser){

			switch(formType){
				case "aw":
					if (!CheckAcademicCode(form)){ return false;}
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "fc":
					if (!CheckAcademicCode(form)){ return false;}
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "ComeBack":
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!CheckCreditCardInfo(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "onedaypass":
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!CheckEdition(form))	{return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "register":
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "conf":
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!CheckCreditCardInfo(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "generic": //Housing at a tipping point - for now. (ANT 09/11/2006)
					if (!CheckContactInfo(form)) {return false;}
					if (form.logged_in.value != '1')
					{
						if (!CheckPassword(form)) {return false;}
					}
					if (!CheckCreditCardInfo(form)) {return false;}
					if (!CheckShipping(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				case "cnf": //consumer flow
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!CheckCreditCardInfo(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					//if (!chkAgree.checked==true){alert(strAlert + 'Please read and agree to all of the Terms of Use.'); return false ;}
					break;	
				case "cnf_trial":
					if (!CheckCreditCardInfo(form)) {return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
				//	if (!chkAgree.checked==true){alert(strAlert + 'Please read and agree to all of the Terms of Use.'); return false ;}
					break;	
				case "two_months":
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					//if (!CheckEdition(form))	{return false;}
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					break;
				default:
					if (!CheckContactInfo(form)) {return false;}
					if (!CheckPassword(form)) {return false;}
					if (!CheckEdition(form))	{return false;}

					//JAP 27-Sep-2010, modified so that the credit card check happens properly.
                    if(formsubPhone=="False"){ //Orignially set to true (not the string version though).
						if (!CheckCreditCardInfo(form)) {return false;}
					}
					
				//	if (!saveSurvey()){return false;}					
					if (!chkService.checked==true){alert(strAlert + 'Please read and agree to the Terms of Use.'); return false ;}
					//if (!chkAgree.checked==true){alert(strAlert + 'Please read and agree to all of the Terms of Use.'); return false ;}
					break;
			}
					
			//Uncommented out 30-Sep-2010 JAP, users can register, the bug preventing users is actually in mcheck.js.
			DisableButton(); //to avoid double-clicks, resulting in duplicate purchases

			return validOK ;

		}
	}
}

//**********************************************
//Make sure at least one Dismal edition is checked
//**********************************************

function CheckEdition(what)
{
    var checked=new Array();
    for(i=0;i<what.elements.length;i++)
		{
		  if ((what.elements[i].type=="checkbox") && (what.elements[i].name == "pid") && (what.elements[i].checked))
		  { checked[checked.length]=what.elements[i].value; }
		}

    if(checked.length){return true;} else	{alert('Please choose an edition(s).');return false;}
  }



//**********************************************
//Check for valid academic code if registering Addison Wesley
//**********************************************
function CheckAcademicCode(what)
{
	with(what)
	{
		if(code.value == ""){code.focus(); alert("Please enter a valid activation code");	return false;}
		return true;
	}
}

//**********************************************
//Check contact information
//**********************************************
function CheckContactInfo(what)
{
	with(what)
	{
			if (!checkName(firstname.value,'Please enter a first name',"firstname"))  { return false ; }

			if (!checkName(lastname.value,'Please enter a last name',"lastname"))  { return false ; }

//			if(country.options[country.selectedIndex].value == ""){country.focus(); alert("Please select a country"); return false;}

			if (!check_Email(email)) { return false ;}
			
			//if (!check_Email(confirm_email)) { return false ;}

			//if ( email.value != confirm_email.value ) 
			//	{ alert(strAlert + "Your email address does not match your confirmation email address. \nPlease re-enter your email address information."); confirm_email.value=""; email.value=""; email.focus(); return false ; }

			if (!checkName(company.value, "Please enter an organization.","company")) { return false ; }
			
			
			if (type.value != "fc" && type.value != "aw") { 
				// check the job title
				if (job_title.options[job_title.selectedIndex].value == "-1") { alert(strAlert + "Please select a job function.", "job_title"); job_title.focus(); return false; }
			}
			
			if (type.value != "fc" && type.value != "aw") {
				// check the industry
				if (industry.options[industry.selectedIndex].value == "-1") { alert(strAlert + "Please select an industry.", "industry"); industry.focus(); return false; }
			}
			
			//if (!checkName(bus_phone.value, "Please enter your Business/Daytime phone number.", "bus_phone")) { return false; }

//			if (!checkName(postal.value, "Please enter your postal code.", "postal")) { return false; }

			return true;
	}
}

//**********************************************
//Check password information
//**********************************************
function CheckPassword(what)
{

	with(what)
	{

		//if ( document.forms.frmMain.password_reminder ) 
			//{
				//if (!checkName(password.value,"Please enter a password","password")) { return false; }		
				//if (!checkName(confirm_password.value,"Please confirm your password.","confirm_password")) { return false; }	
				
				if(!check_Password(password)) { return false ;}

				//if(!check_Password(confirm_password)) { return false ;}

				// is password between 5 and 10 characters long?
				if (password.value.length < 5 ) 
				{ alert(strAlert + "Your password is less than 5 characters long. \nPlease re-enter your password information."); password.value=""; confirm_password.value=""; password.focus(); return false ; }
				//return false ;

				//Brought back the confirm password check, depending on the form type.  JAP 29-Sep-2010
				if (type.value == "aw" || type.value == "fc" || type.value == "student" || type.value == "cnf") {
					if ( password.value != confirm_password.value ) 
						{ alert(strAlert + "Your password does not match your confirmation password. \nPlease re-enter your password information."); confirm_password.value=""; password.value=""; password.focus(); return false ; }
				}
				return true;
			//}
		}
}

//**********************************************
//Check credit card information
//**********************************************
function CheckCreditCardInfo(what)
{
	with(what)
	{
			// check the name on credit card
				if (!checkName(card_name.value,'Please enter your name as it appears on your credit card.',"card_name"))  { return false ; }

				// check the credit card number
				if (!isCreditCard()) { return false ; }
				if (!checkCard()) { return false ; }
				if (!checkType(document.frmMain.cc_type.selectedIndex)) { return false ; }
				//if (!checkType(document.frmMain.cc_type.value)) { return false ; }
				if (!checkName(card_number.value,'Please enter the credit card number.',"card_number"))  { return false ; }

				// check the expiration date
				//if (!checkName(card_exp.value,'Please enter the credit card expiration date.',"card_exp"))  { return false ; }
				
				// check the address
				if (!checkName(card_address1.value,'Please enter the credit card billing street address.',"card_address1"))  { return false ; }
				if (!checkName(card_city.value,'Please enter the credit card billing city.',"card_city"))  { return false ; }
				if (!checkName(card_zip.value,'Please enter the credit card billing zip code.',"card_zip"))  { return false ; }
				
				if ((card_country.value == "US" || card_country.value == "CA") && card_state.value == "X") {alert(strAlert + 'The State/Province field is required for US and Canada billing addresses.'); card_state.focus(); return false ; }
			
			return true;
	}
}

function CheckShipping(what)
{
	with(what)
	{
		if (document.frmMain.chkUseBillAdd.checked==true)
		{
			document.frmMain.ship_address1.value = document.frmMain.card_address1.value;
			document.frmMain.ship_address2.value = document.frmMain.card_address2.value;
			document.frmMain.ship_city.value = document.frmMain.card_city.value;
			document.frmMain.ship_state.value = document.frmMain.card_state.value;
			document.frmMain.ship_zip.value = document.frmMain.card_zip.value;
			document.frmMain.ship_country.value = document.frmMain.card_country.value;
		}
		else
		{
			if (!checkName(ship_address1.value,'Please enter the shipping street address.',"ship_address1"))  { return false ; }
			if (!checkName(ship_city.value,'Please enter the shipping city.',"ship_city"))  { return false ; }
			if (!checkName(ship_zip.value,'Please enter the shipping zip code.',"ship_zip"))  { return false ; }
			if ((ship_country.value == "US" || ship_country.value == "CA") && ship_state.value == "X") {alert(strAlert + 'The State/Province field is required for US and Canada shipping addresses.'); return false ; }
		}

		return true;

	}
}

function CheckSweepstakes(what)
{
	with(what)
	{
		/***********************************************************************************************
			  if entering sweepstake
			***********************************************************************************************/
					if (document.frmMain.sweep.value == "1")
					{
						if (document.frmMain.chkUseBillAdd.checked==true)
						{
							document.frmMain.ship_address1.value = document.frmMain.card_address1.value;
							document.frmMain.ship_address2.value = document.frmMain.card_address2.value;
							document.frmMain.ship_city.value = document.frmMain.card_city.value;
							document.frmMain.ship_state.value = document.frmMain.card_state.value;
							document.frmMain.ship_zip.value = document.frmMain.card_zip.value;
							document.frmMain.ship_country.value = document.frmMain.card_country.value;
						}
						else
						{
							if (!checkName(ship_address1.value,'Please enter the shipping street address.',"ship_address1"))  { return false ; }
							if (!checkName(ship_city.value,'Please enter the shipping city.',"ship_city"))  { return false ; }
							if (!checkName(ship_zip.value,'Please enter the shipping zip code.',"ship_zip"))  { return false ; }
							if ((ship_country.value == "US" || ship_country.value == "CA") && ship_state.value == "X") {alert(strAlert + 'The State/Province field is required for US and Canada shipping addresses.'); return false ; }
						}
					}
			/**************************************************************************************************/

			/*if(document.frmMain.freq[0].checked == false)
			{
				if (!checkName(ship_address1.value,'Please enter the shipping street address.',"ship_address1"))  { return false ; }
				if (!checkName(ship_city.value,'Please enter the shipping city.',"ship_city"))  { return false ; }
				if (!checkName(ship_zip.value,'Please enter the shipping zip code.',"ship_zip"))  { return false ; }
				if ((ship_country.value == "US" || ship_country.value == "CA") && ship_state.value == "X") {alert(strAlert + 'The State/Province field is required for US and Canada shipping addresses.'); return false ; }
				if (address_type[0].checked == false && address_type[1].checked == false) {alert(strAlert + 'Please specify whether the shipping address supplied is a Residential or Business address.'); return false;}
			}*/
	}
}

function checkName(name,strMessage,strElement){
	if (name.length == 0){
		validOK = false
		alert(strAlert + strMessage)
		eval("document.frmMain." + strElement + ".focus()")
		return false ;
	}
	return true
}

function useBill()
{
	if (document.frmMain.use_bill.checked == true)
	{
		if(document.frmMain.freq[0].checked == false)
		{
			document.frmMain.ship_address1.value = document.frmMain.card_address1.value;
			document.frmMain.ship_address2.value = document.frmMain.card_address2.value;
			document.frmMain.ship_city.value = document.frmMain.card_city.value;
			document.frmMain.ship_state.value = document.frmMain.card_state.value;
			document.frmMain.ship_zip.value = document.frmMain.card_zip.value;
			document.frmMain.ship_country.value = document.frmMain.card_country.value;
		}
		else
		{
			alert('Dismal Scientist Gifts will only be sent to subscribers who purchase access for at least one year.');
			document.frmMain.use_bill.checked = false;
		}
	}
	else
	{
		document.frmMain.ship_address1.value = '';
		document.frmMain.ship_address2.value = '';
		document.frmMain.ship_city.value = '';
		document.frmMain.ship_state.value = 'X';
		document.frmMain.ship_zip.value = '';
		document.frmMain.ship_country.value = 'US';
	}
}

function checkFreq(strObj)
{
	if(document.frmMain.freq[0].checked == true)
	{
		alert('Gifts will only be sent to subscribers who purchase access for at least one year.');
		eval('document.frmMain.' + strObj + '.value=""');
		eval('document.frmMain.' + strObj + '.blur()');
	}
}

function sendT()
{
	if(document.frmMain.freq[0].checked == true)
	{
		//document.frmMain.tshirt.value = "NO";
	}
	else
	{
		//document.frmMain.tshirt.value = "YES";
	}
}

function checkCard()
{
	var now = new Date();
	var iMonth;

	if(parseInt(document.frmMain.card_expM.options[document.frmMain.card_expM.selectedIndex].value.substr(0,1)) == 0)
	{
		iMonth = parseInt(document.frmMain.card_expM.options[document.frmMain.card_expM.selectedIndex].value.substr(1,2));
	}
	else
	{
		iMonth = parseInt(document.frmMain.card_expM.options[document.frmMain.card_expM.selectedIndex].value.substr(0,2));
	}
	if(iMonth < parseInt(now.getMonth()+1) &&  document.frmMain.card_expY.options[document.frmMain.card_expY.selectedIndex].value == now.getFullYear().toString().substring(2,4))
	{
		alert ('Invalid expiration date.  Please select a valid expiration date for your credit card and re-submit.');
		document.frmMain.card_expM.focus();
		validOK = false;
		return false;
	}
	return true;
}

function isCreditCard(){
	var st = document.frmMain.card_number.value;
	if(st.length > 12)
	{
		sum = 0; mul = 1; l = st.length;
		for (i = 0; i < l; i++) 
		{
			digit = st.substring(l-i-1,l-i);
			tproduct = parseInt(digit ,10)*mul;
			if (tproduct >= 10)
				sum += (tproduct % 10) + 1;
			else
				sum += tproduct;
			if (mul == 1)
				mul++;
			else
				mul--;
		}
		if ((sum % 10) != 0)
		{
			alert ("You have entered an invalid credit card number.  Please verify the number and re-submit.");
			document.frmMain.card_number.focus();
			validOK = false;
			return false;
		}
		return true;
	}
	else
	{
		alert ("You have entered an invalid credit card number.  Please verify the number and re-submit.");
		document.frmMain.card_number.focus();
		validOK = false;
		return false;
	}
}

function checkType(iType){
	var isTypeOK = false;
	var cc = document.frmMain.card_number.value ;
	switch (iType)
	{
	case 0:
		if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4))
		{
			isTypeOK = true;
		}
		break;
	case 1:
		if ((cc.length == 16) && (cc.substring(0,2) == 51 || cc.substring(0,2) == 52 || cc.substring(0,2) == 53 || cc.substring(0,2) == 54 || cc.substring(0,2) == 55))
		{
			isTypeOK = true;
		}
		break;
	case 2:
		if ((cc.length == 15) && (cc.substring(0,2) == 34 || cc.substring(0,2) == 37))
		{
			isTypeOK = true;
		}
		break;
	case 3:
		if ((cc.length == 16) && (cc.substring(0,4) == 6011))
		{
			isTypeOK = true;
		}
		break;
	default:
		isTypeOK = false;
	}



	if (isTypeOK)
	{
		return true;
	}
	else
	{
		alert ("The credit card number you supplied is not a valid " + document.frmMain.cc_type.options[iType].text + " card number.  Please verify the type and number and re-submit.");
		document.frmMain.cc_type.focus();
		validOK = false;
		return false;
	}
}

function goLicense( iPage )
	{
		if ( iPage == 0 ) {
			var sURL = "/register/site_license.asp";
			var sSize = "status,height=350,width=400";
		} else {
			var sURL = "/register/tenUserDesc.asp";
			var sSize = "status,height=500,width=570";
		}

		if ( wndLicense == null )	
		{
			wndLicense = window.open(sURL,"sub",sSize)

			if ( !wndLicense.opener )
				wndLicense.Opener = window;

		}
		else if ( wndLicense.closed ) 
		{
			wndLicense = window.open(sURL,"sub",sSize)

			if ( !wndLicense.opener )
				wndLicense.Opener = window;		
		}
		else
		{
			wndLicense.location = sURL;
		}
		
		wndLicense.focus()
	}

	function goURL(sURL)
	{
		if( wndLocation == null || wndLocation.closed ){
			wndLocation = window.open(sURL);
		}else{
			wndLocation.location = sURL;
		}
		wndLocation.focus();
	}

function changePlan(iFreq)
	{
		var iPos, i;

		for(i=0; i<aryFreq.length; i++){
			if(iFreq == aryFreq[i]){
				iPos = i;
			}
		}

		with(document.forms.frmMain){
			billcycle.value = aryBillcycle[iPos];
			card_amount.value = aryPrice[iPos];
			plan_desc.value = aryDesc[iPos];
		}
		
		if (document.frmMain.track_guid.value != "" ) {
			if (iFreq == 150) {
				document.frmMain.btnSubmit.value = "Start My Subscription Now";
				//document.all.divMsg.innerHTML = "<font class='spListing'>START YOUR SUBSCRIPTION</font><br /><font class='smText'>Click on this button to 	submit your order information.</font>";
			} else {
				document.frmMain.btnSubmit.value = "Start My FREE Trial Now";
				//document.all.divMsg.innerHTML = "<font class='spListing'>START YOUR FREE TRIAL</font><br /><font class='smText'>Click on this button to 	submit your order information.</font>";
			}				
		}
	}

	function saveSurvey() {
		var oRdo = document.frmMain.rdoAns;
		var sAns;
		var sAnsDet;
		// make sure survey is completed.
		var blnChk = false;
		for (i=0;i<oRdo.length;i++)
		{
			if (oRdo[i].checked)
			{
				blnChk = true;
				if ((i==0 && document.frmMain.txtMedia.value=="") || (i==5 && document.frmMain.txtOther.value==""))
				{
					switch(i)
					{
						case 0:
						{
							alert("Please enter media source.");
							break;
						}
						case 5:
						{
							alert("Please enter other source.");
							break;
						}
					}
					return false;
				} 
			}
		}

		if (!blnChk) 
		{
			alert("Please complete the survey and re-submit.");
			return false;
		}

		// save info
		for (i=0;i<oRdo.length;i++) {
			if (oRdo[i].checked) {
				sAns = oRdo[i].value
				if ( (i==0) && (sAns.indexOf("q1a") > -1) ) {
					sAnsDet = document.frmMain.txtMedia.value;
				} else if ( (i==5) && (sAns.indexOf("q1a") > -1) ) {
					sAnsDet = document.frmMain.txtOther.value;
				} else {
					sAnsDet = ""
				}
			}
		}
		document.frmMain.ans.value = sAns;
		document.frmMain.ansDet.value = sAnsDet;		

		if (blnChk)
		{
			return true;
		}
	}

	function DisableButton()
	{
		//alert("here");
		document.frmMain.btnSubmit.disabled = "disabled";
	}

	function txtClear()
	{
		document.frmMain.txtMedia.value="";
		document.frmMain.txtOther.value="";
	}

	function popUp(url) {
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin"; }
