// To guarantee form is only submitted once
var formSubmitted = false;

function prefillForm(skip,focusField,idprefix)
{
        // Some forms skip the form altogether if the same form has already been filled out
        if (skip) return;
		
		var firstName = "#" + (idprefix == null ? "" : idprefix) + "firstName";
		var lastName = "#" + (idprefix == null ? "" : idprefix) + "lastName";
		var email = "#" + (idprefix == null ? "" : idprefix) + "email";
		var phone = "#" + (idprefix == null ? "" : idprefix) + "phone";
		var companyName = "#" + (idprefix == null ? "" : idprefix) + "companyName";
		var city = "#" + (idprefix == null ? "" : idprefix) + "city";
		var state = "#" + (idprefix == null ? "" : idprefix) + "state";
		var country = "#" + (idprefix == null ? "" : idprefix) + "country";
		var totalDesktops = "#" + (idprefix == null ? "" : idprefix) + "totalDesktops";
		
		// Common code may call this function for a form that doesn't really exist
		if ($(firstName).length <= 0) return;
        
        // These always there
        $(firstName)[0].value = form$firstName;
        $(lastName)[0].value = form$lastName;
        $(email)[0].value = form$email;
        $(phone)[0].value = form$phone;
        
        // These may or may not be
        if ($(companyName).length>0) $(companyName)[0].value = form$companyName;
        if ($(city).length>0) $(city)[0].value = form$city;
        // Must handle state and country differently
        if ($(state).length>0) {
                sel = $(state)[0];
                for (i=0; i<sel.options.length; i++) {
                        if (sel.options[i].text.toLowerCase() == form$state.toLowerCase()) {
                            sel.selectedIndex = i;
                                break;
                        }
                }
        }
        if ($(country).length>0) {
                sel = $(country)[0];
                for (i=0; i<sel.options.length; i++) {
                        if (sel.options[i].text.toLowerCase() == form$country.toLowerCase()) {
                            sel.selectedIndex = i;
                                break;
                        }
                }
        }
        if ($(totalDesktops).length>0) $(totalDesktops)[0].value = form$totalDesktops;
        
        if (focusField != null) {
                $(focusField).focus();
        }
}

// This version uses the supplied form name to go thru the DOM vs. using IDs - it's required to allow
// form population when there are 2 forms on the page with the same named fields
function prefillForm2(skip,focusField,fn)
{
        // Some forms skip the form altogether if the same form has already been filled out
        if (skip) return;
        
        // These always there
        document.forms[fn].firstName.value = form$firstName;
        document.forms[fn].lastName.value = form$lastName;
        document.forms[fn].email.value = form$email;
        document.forms[fn].phone.value = form$phone;
        
        // These may or may not be
        if (document.forms[fn].companyName != null) document.forms[fn].companyName.value = form$companyName;
        if (document.forms[fn].city != null) document.forms[fn].city.value = form$city;
        // Must handle state and country differently
        if (document.forms[fn].state != null) {
                sel = document.forms[fn].state;
                for (i=0; i<sel.options.length; i++) {
                        if (sel.options[i].text.toLowerCase() == form$state.toLowerCase()) {
                            sel.selectedIndex = i;
                                break;
                        }
                }
        }
        if (document.forms[fn].country != null) {
                sel = document.forms[fn].country;
                for (i=0; i<sel.options.length; i++) {
                        if (sel.options[i].text.toLowerCase() == form$country.toLowerCase()) {
                            sel.selectedIndex = i;
                                break;
                        }
                }
        }
        if (document.forms[fn].totalDesktops != null) document.forms[fn].totalDesktops.value = form$totalDesktops;
        
        if (focusField != null) {
                document.forms[fn][focusField].focus();
        }
}

// Form validation - VPA/Email
function validateForm1()
{
   
    // Field name & description
    var required = new Array("vpa|VPA number","email|E-mail");
        
    var s = "";
    var t;
    
    // Keep track of which was the first missing so focus can be placed there
    //var firstMissing = "";
    var firstMissing = null;

    // For all these required fields ...    
    for (var i=0; i<required.length; i++) {
        t = required[i].split('|');
        if (document.support_login[t[0]].value.length==0) {
            s += '   ' + t[1] + '\n';
            //if (firstMissing.length==0) firstMissing = t[0];
            if (firstMissing==null) firstMissing = document.support_login[t[0]];
        }
    }
   
    // Any any missing, alert the user and cancel the form submission
    if (s.length != 0) {
        alert('One or more required fields were not specified:\n' + s +
              'Please supply the missing information and resubmit the form.');
        if (firstMissing != null) {
            firstMissing.focus();
        }
        //if (firstMissing.length != 0) {
        //    document.support_login[firstMissing].focus();
        //}
        return false;
    }
    
    // Here's a MUCH more exhaustive e-mail address check courtesy of coveryourasp.com that uses
    // regular expression pattern matching. Portions (c) James Shaw - james@CoverYourASP.com
    var em = document.support_login.email.value;
    if (em.search( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/ ) == -1) {
        alert('Please verify your e-mail address for correct syntax.');
        document.support_login.email.focus();
        return false;
    }

    return true;
}

// Form validation - VPA/Email/Name/Phone/Company
function validateForm2()
{
   
    // Field name & description
    var required = new Array("vpa|VPA number","name|Name",
    "companyName|Company","email|E-mail","phone|Phone");
        
    var s = "";
    var t;
    
    // Keep track of which was the first missing so focus can be placed there
    //var firstMissing = "";
    var firstMissing = null;

    // For all these required fields ...    
    for (var i=0; i<required.length; i++) {
        t = required[i].split('|');
        if (document.support_login[t[0]].value.length==0) {
            s += '   ' + t[1] + '\n';
            //if (firstMissing.length==0) firstMissing = t[0];
            if (firstMissing==null) firstMissing = document.support_login[t[0]];
        }
    }

    // Any any missing, alert the user and cancel the form submission
    if (s.length != 0) {
        alert('One or more required fields were not specified:\n' + s +
              'Please supply the missing information and resubmit the form.');
        if (firstMissing != null) {
            firstMissing.focus();
        }
        return false;
    }
    
    // Here's a MUCH more exhaustive e-mail address check courtesy of coveryourasp.com that uses
    // regular expression pattern matching. Portions (c) James Shaw - james@CoverYourASP.com
    var em = document.support_login.email.value;
    if (em.search( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/ ) == -1) {
        alert('Please verify your e-mail address for correct syntax.');
        document.support_login.email.focus();
        return false;
    }

    return true;
}

// Form validation - Email/Name/ESM product & version/Problem (this is for the request for support form)
function validateForm3()
{
   
    // Field name & description
    var required = new Array("name|Name","email|E-mail","problem|Problem Description");
        
    var s = "";
    var t;
    
    // Keep track of which was the first missing so focus can be placed there
    //var firstMissing = "";
    var firstMissing = null;

    // For all these required fields ...    
    for (var i=0; i<required.length; i++) {
        t = required[i].split('|');
        if (document.support_login[t[0]].value.length==0) {
            s += '   ' + t[1] + '\n';
            //if (firstMissing.length==0) firstMissing = t[0];
            if (firstMissing==null) firstMissing = document.support_login[t[0]];
        }
    }
        
        // Check the two required dropdowns
        var sel = document.support_login["esmProd"];
        if (sel.options[sel.selectedIndex].text == "") {
                s += '   Express Product\n';
        if (firstMissing==null) firstMissing = sel;
        }
        sel = document.support_login["esmVer"];
        if (sel.options[sel.selectedIndex].text == "") {
                s += '   Product Version\n';
        if (firstMissing==null) firstMissing = sel;
        }

    // Any any missing, alert the user and cancel the form submission
    if (s.length != 0) {
        alert('One or more required fields were not specified:\n' + s +
              'Please supply the missing information and resubmit the form.');
        if (firstMissing != null) {
            firstMissing.focus();
        }
        return false;
    }
    
    // Here's a MUCH more exhaustive e-mail address check courtesy of coveryourasp.com that uses
    // regular expression pattern matching. Portions (c) James Shaw - james@CoverYourASP.com
    var em = document.support_login.email.value;
    if (em.search( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/ ) == -1) {
        alert('Please verify your e-mail address for correct syntax.');
        document.support_login.email.focus();
        return false;
    }

    return true;
}

// Form validation - non-trial page eval form (first, last, email, phone, company, country, # of seats).
// Form name is specified via fn
function validateForm(fn)
{
    // Only allow the form to be submitted ONCE.
    if (formSubmitted) return false;
    
    // Field name & description
    var required = new Array("firstName|First name","lastName|Last name","email|Email",
        "phone|Phone","companyName|Company");
        
    var s = "";
    var t;
	var f;
    
    // Keep track of which was the first missing so focus can be placed there
    //var firstMissing = "";
    var firstMissing = null;

    // For all these required fields ...    
    for (var i=0; i<required.length; i++) {
                try {
                        t = required[i].split('|');
						f = document.forms[fn][t[0]].value;	
                        if (trim(f).length==0) {
                                s += '   ' + t[1] + '\n';
                                //if (firstMissing.length==0) firstMissing = t[0];
                                if (firstMissing==null) firstMissing = document.forms[fn][t[0]];
                        }
                } catch (err) {
					//alert(err.description);
                }
    }
    
    if (document.forms[fn].country.selectedIndex<=0) {
        s += "   Country\n";
        //if (firstMissing.length==0) firstMissing = "country";
        if (firstMissing==null) firstMissing = document.forms[fn].country;
    }
        
	// Check number of desktops
	var nd = document.forms[fn].totalDesktops.value;
	var nd2 = "";
	for (var i=0; i<nd.length; i++) {
			if (nd.charAt(i) != ",") {              // remove commas - parseInt() treats them as decimal points!
					nd2 += nd.charAt(i);
			}
	}
	if (nd2.length == 0 || isNaN(parseInt(nd2)) || parseInt(nd2) == 0) {
			s += "   Total desktops\n";
	} else {
		document.forms[fn].totalDesktops.value = parseInt(nd2);	// Put fixed up number back into form
	}
   
    // Any any missing, alert the user and cancel the form submission
    if (s.length != 0) {
        alert('One or more required fields were not specified:\n' + s +
              'Please supply the missing information and resubmit the form.');
        if (firstMissing != null) {
            firstMissing.focus();
        }
        //if (firstMissing.length != 0) {
        //    document.expressEval[firstMissing].focus();
        //}
        return false;
    }
    
    // Here's a MUCH more exhaustive email address check courtesy of coveryourasp.com that uses
    // regular expression pattern matching. Portions (c) James Shaw - james@CoverYourASP.com
    var em = document.forms[fn].email.value;
    if (em.search( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/ ) == -1) {
        alert('Please verify your e-mail address for correct syntax.');
        document.forms[fn].email.focus();
        return false;
    }
    
    // When the form has been validated, indicate that it is to be submitted
    formSubmitted = true;
    return true;
}

// This function trims leading and trailing spaces
function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}



