var tries=0;

function workstationdetails()
{
    var output = "";
    output += "<table cellspacing=\"10px\">\n"
    output += "<td valign=\"top\" width=\"50%\">\n";
    output += "<input value=\"workstation1\" name=\"subscription\" type=\"radio\">\n";
    output += "1-3 Workstations $2,000.00<br>\n";
    output += "<input value=\"workstation2\" name=\"subscription\" type=\"radio\">\n";
    output += "4-7 Workstations $3,000.00<br>\n";
    output += "<p>Optional:  If you know the IP addresses of the workstations ";
    output += "involved, list them here.<br>\n";
    output += "<textarea id=\"ipinfo\" name=\"workstationips\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n";
    output += "</td>\n<td valign=\"top\" width=\"50%\">\n";
    output += "<span style=\"color:red;\">";
    output += "No modem or proxy access allowed.  "
    output += "Static IPs are required for each workstation.\n";
    output += "</span>\n</td>\n</table>\n";
    document.getElementById("workstationdetails").innerHTML=output;
    document.getElementById("unlimiteddetails").innerHTML='';
    /* now turn off any checked stuff in the other set */
    var subs=document.getElementsByName("subscription");
    for (var item=0; item < subs.length; item++)
    {
	subs[item].checked=false;
    }
}

function unlimiteddetails()
{
    var output = "";
    output += "Please complete the questionnaire below.  ";
    output += "We will email price to contact provided above. <p>\n";
    output += "<ol>\n<li><span style=\"color:red\">*</span>";
    output += "What is the size of your institution?  Please ";
    output += "provide total FTE count, including faculty, researchers, ";
    output += "students, and staff.<br>\n";
    output += "<textarea id=\"totalfteid\" name=\"totalfte\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n<div id=\"totalfteerror\"></div>";
    output += "</li><p>\n";
    output += "<li><span style=\"color:red\">*</span>";
    output += "The TLG is a specialized resource.  In our experience ";
    output += "departments more likely to use it come from departments ";
    output += "such as Classics, Philosophy, History, Linguistics, ";
    output += "English and Religious Studies.  How many anticipated users ";
    output += "do you have in your institution? (approximate number)<br>\n";
    output += "<textarea name=\"anticipatedusers\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n<div id=\"anticipateduserserror\"></div>";
    output += "</li><p>\n";
    output += "<li><span style=\"color:red\">*</span>";
    output += "Do you provide access via proxy?<br>\n";
    output += "<textarea name=\"proxyaccess\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n<div id=\"proxyaccesserror\"></div>";
    output += "</li><p>\n";
    output += "<li><span style=\"color:red\">*</span>";
    output += "Do you provide (or wish to provide) access to ";
    output += "visitors, walk-ins, or alumni?<br>\n";
    output += "<textarea name=\"otheraccess\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n<div id=\"otheraccesserror\"></div>";
    output += "</li><p>\n";
    output += "<li><span style=\"color:red\">*</span>";
    output += "Do you offer access from one site (campus) or more?<br>\n";
    output += "<textarea name=\"multiaccess\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n<div id=\"multiaccesserror\"></div>";
    output += "</li><p>\n";
    output += "<li><span style=\"color:red\">*</span>";
    output += "Do you have any off-campus affiliated programs for which ";
    output += "you intend to provide access?<br>\n";
    output += "<textarea name=\"offsiteaccess\" rows=\"3\" maxrows=\"10\" ";
    output += "cols=\"50\"></textarea>\n<div id=\"offsiteaccesserror\"></div>";
    output += "</li><p>\n";
    document.getElementById("unlimiteddetails").innerHTML=output;
    document.getElementById("workstationdetails").innerHTML='';
    /* since that's a fake radio button not with the "subscription" one,
    turn the workstation main radio button off manually otherwise both
    look checked */
    document.getElementById("placeholder").checked=false;
}

function validateForm ( form )
{
    var retval = true;  // be optimistic!!
    var silent = false;
    var missingq = false;
    var otherq = false;
    var errname;
    try {
	for ( var e=0; e < form.elements.length; e++ )
	{
	    var el = form.elements[e];
	    if (el.name != '')
	    {
		errname = el.name + "error";
	    }
	    if (errname != '')
	    {
		// ban html coding
	        if (el.type != 'radio' && el.value != '')
		{
		    var ndx = el.value.search(/(<|htm|http)/);
		    if (ndx >= 0)
		    {
		        retval=false;
			silent=true;
		    }
		}

		if (el.name=='title'||el.name=='state'||
		    el.name=='zipcode'||el.name=='fax'||
		    el.name=='department'||el.name=='invoice'||
		    el.name=='questions'||el.name=='submit'||
		    el.name=='workstationips'||
		    el.name=='reset'||el.id=='placeholder')
		{
		    // these are optional fields
		    // but reject junk in non empty fields
		    if ((el.name=='fax'||el.name=='workstationips') &&
                        el.value != '')
		    {
			var ndx = el.value.search(/[^0-9-.\s()+]/);
			if (ndx >= 0)
			{
			    retval=false;
			    silent=true;
			}
		    }

		}
		else if (el.type == 'radio')
		{
		    var group = form [el.name];
		    var checked = false;
		    if (!group.length)
			checked = el.checked;
		    else
			for ( var r = 0; r < group.length; r ++ )
			    if ( ( checked = group [ r ].checked ) )
				break;
		    if ( !checked )
		    {
			var output = "<span style=\"color:red\">Please choose an option!</span>";
			document.getElementById(errname).innerHTML=output;
			otherq = true;
			retval = false;
		    }
		    /* need to make sure unlimited has opened up
		    questionnaire and answered the questions...it's
		    possible to wind up with unlimited clicked but not
		    showing the q., in which case this simple validation
		    fails since it doesn't examine specific names
		    otherwise in the radio check */
		    else if (el.name=="subscription" &&
		             el.value=="unlimited" &&
			     el.checked==="true")
		    {
			/* since it's all or nothing display, only need
			to check for one of the q's names, if it's
			defined, then we need do nothing further as this
			will catch missing ones.  if it's undefined,
			then put out a message about clicking the
			unlimited again and answering the q's */
			if (document.getElementById("totalfteid") == null)
			{
			    retval = false;
			    missingq= true;
			}
		    }
		    else
		    {
			document.getElementById(errname).innerHTML='';
		    }

		}
		else if (el.value == '')
		{
		    var output = "<span style=\"color:red\">";
		    output += "Please fill in required field!</span>";
		    document.getElementById(errname).innerHTML=output;
		    retval = false;
		    otherq = true;
		}
		else
		{
		    document.getElementById(errname).innerHTML='';
		    if (el.name=='email')
		    {
			var ndx1 = el.value.search(/^([\w.-])+@([\w])+\./);
			var ndx2 = el.value.search(/\.([a-z])+$/);
			if (ndx1 < 0 || ndx2 < 0)
			{
			    retval=false;
			    silent=true;
			}
		    }
		    /* can't restrict, really  -- "no phone" and
		    "xxxxx or xxxxx" are okay, so screw that
		    if (el.name=='phone')
		    {
			var ndx = el.value.search(/[^0-9-.\s()+]/);
			if (ndx >= 0)
			{
			    retval=false;
			    silent=true;
			}
                    }
		    */
		    if (el.name=='firstname' || el.name=='lastname' ||
			el.name=='state' || el.name=='country')
		    {
			var ndx = el.value.search(/\d/);
			if (ndx > 0)
			{
			    retval=false;
			    silent=true;
			}
		    }
		}
	    }
	}
    }
    catch (e)
    {
	/*
        alert("An exception occurred in the script. Error name: " +
	      e.name + ". Error message: " + e.message);
	alert("errname is " + errname);
	*/
        retval = false;
    }

    document.getElementById('generalerror').innerHTML='';
    tries++;
    if (tries >= 3)
    {
//alert("too many failures");
        document.getElementById('generalerror').innerHTML=
                  "<input type=\"hidden\" name=\"discard\" value=\"Y\">\n";
        return true;
    }

    if (retval===false)
    {
	if (missingq===true)
	{
	    var output = "";
	    output = "<span style=\"color:red\">" +
		"Please fill out the questionnaire under Unlimited " +
		"Subscription (click on button again to show questions)";
	    if (otherq===true)
	    {
	       output += "<br>and please also fill out other fields as " +
	                 "indicated above";
	    }
	    output += "!</span>";
	    document.getElementById('generalerror').innerHTML=output;
	}
	else
	{
            // if a required field is empty, trump the silent marker,
            // it could have both bogus and missing so check both
            // indicators.
            if (silent===false||otherq===true)
            {
		document.getElementById('generalerror').innerHTML=
		    "<span style=\"color:red\">" +
		    "Please go back and fill in required field(s)!</span>";
            }
	}
	return false;
    }
    else
    {
	return true;
    }
}

