function checkConfig(billing_same, applicationID, sharevault_nameID, sharevault_urlID, IP_addressID)
{
	var c_first_name, c_last_name, c_company, c_title, c_phone, c_email, c_street, c_city, c_state, c_province, c_postal_code, c_country, c_sharevault_name, c_url, c_industry, c_application, c_lead_source, c_description, c_IP_address, c_comments, c_days, c_pages, c_mb, c_users, c_product, c_product_name, c_approval_checkbox, full_address, today = new Date(), expiration = new Date();
	// Maximum no of characters in a ShareVault name
	var maxCharsInShareVaultName = 32;


	c_first_name = window.document.config.first_name; c_first_name.value = trim(c_first_name.value);
	c_last_name = window.document.config.last_name; c_last_name.value = trim(c_last_name.value);
	c_company = window.document.config.company; c_company.value = trim(c_company.value);
	c_title = window.document.config.title; c_title.value = trim(c_title.value);
	c_phone = window.document.config.phone; c_phone.value = trim(c_phone.value);
	c_email = window.document.config.email; c_email.value = trim(c_email.value);
	c_street = window.document.config.street; c_street.value = trim(c_street.value);
	c_city = window.document.config.city; c_city.value = trim(c_city.value);
	c_state = window.document.config.state; c_state.value = trim(c_state.value);
	c_province = window.document.config.province; c_province.value = trim(c_province.value);
	c_postal_code = window.document.config.postal_code; c_postal_code.value = trim(c_postal_code.value);
	c_country = window.document.config.country; c_country.value = trim(c_country.value);
	c_sharevault_name = document.getElementById(sharevault_nameID); c_sharevault_name.value = trim(c_sharevault_name.value);
	c_url = document.getElementById(sharevault_urlID); c_url.value = trim(c_url.value);
	c_industry = window.document.config.industry; c_industry.value = trim(c_industry.value);
	c_application = document.getElementById(applicationID); c_application.value = trim(c_application.value);
	c_lead_source = window.document.config.lead_source; c_lead_source.value = trim(c_lead_source.value);
	c_description = document.getElementById("description"); c_description.value = trim(c_description.value);  // I don't know why window.document.config.description doesn't work, but it doesn't
	c_comments = window.document.config.comments; c_comments.value = trim(c_comments.value);
	c_product = window.document.config.product;
	c_product_name = window.document.config.product_name;
	c_days = window.document.config.days;
	c_pages = window.document.config.pages;
	c_mb = window.document.config.mb;
	c_users = window.document.config.users;
	c_IP_address = window.document.config.IP_address; c_IP_address.value = trim(c_IP_address.value);
	c_approval_checkbox = window.document.config.approval_checkbox;
	
	if(billing_same) n = 1; else n = 2;
	for (i = 1; i <= n; i++)
	{
	  if (i == 2)
	  {
		c_first_name = window.document.config.ap_first_name; c_first_name.value = trim(c_first_name.value);
		c_last_name = window.document.config.ap_last_name; c_last_name.value = trim(c_last_name.value);
		c_company = window.document.config.ap_company; c_company.value = trim(c_company.value);
		c_title = window.document.config.ap_title; c_title.value = trim(c_title.value);
		c_phone = window.document.config.ap_phone; c_phone.value = trim(c_phone.value);
		c_email = window.document.config.ap_email; c_email.value = trim(c_email.value);
		c_street = window.document.config.ap_street; c_street.value = trim(c_street.value);
		c_city = window.document.config.ap_city; c_city.value = trim(c_city.value);
		c_state = window.document.config.ap_state; c_state.value = trim(c_state.value);
		c_province = window.document.config.ap_province; c_province.value = trim(c_province.value);
		c_postal_code = window.document.config.ap_postal_code; c_postal_code.value = trim(c_postal_code.value);
		c_country = window.document.config.ap_country; c_country.value = trim(c_country.value);
	  }
	  if(c_first_name.value == '')
	  {
		  alert('Please enter the first name');
		  c_first_name.focus();
		  return false;
	  }
	  else if(isEmail(c_first_name.value))
	  {
		  alert('ERROR: You have entered an email address in the first name field');
		  c_email.focus();
		  return false;
	  }
	  
	  if(c_last_name.value == '')
	  {
		  alert('Please enter the last name');
		  c_last_name.focus();
		  return false;
	  }
	  else if(isEmail(c_last_name.value))
	  {
		  alert('ERROR: You have entered an email address in the last name field');
		  c_email.focus();
		  return false;
	  }
	  
  	  else if(c_title.value == '')
	  {
		  alert('Please enter the job title');
		  c_title.focus();
		  return false;
	  }
	  
	  else if(c_company.value == '')
	  {
		  alert('Please enter the company name');
		  c_company.focus();
		  return false;
	  }
	
	  else if(c_phone.value == '')
	  {
		  alert('Please enter the phone nubmer');
		  c_phone.focus();
		  return false;
	  }
	  else if(!checkInternationalPhone(c_phone.value))
	  {
		  alert('Please enter a valid phone number');
		  c_phone.focus();
		  return false;
	  }
	  
	  else if(c_email.value == '')
	  {
		  alert('Please enter the email address');
		  c_email.focus();
		  return false;
	  }
	  else if(!isEmail(c_email.value))
	  {
		  alert('Please enter a valid email address');
		  c_email.focus();
		  return false;
	  }
	  else if(c_country.value == '')
	  {
		  alert('Please select the country');
		  c_country.focus();
		  return false;
	  }
	  else if(c_street.value == '')
	  {
		  alert('Please enter the street address');
		  c_street.focus();
		  return false;
	  }
	  else if(c_street.value.replace(/\r/g,'').split('\n').length > 2)
	  {
		  alert('Please re-enter the street address: The street address cannot exceed 2 lines of text.');
		  c_street.focus();
		  return false;
	  }
	  else if(c_city.value == '')
	  {
		  alert('Please enter the city');
		  c_city.focus();
		  return false;
	  }
	  else if ((c_country.value == "Canada") && (c_province.value == ''))
	  {
		  alert('Please select the province');
		  c_province.focus();
		  return false;		
	  }
	  else if ((c_country.value == "United States") && (c_state.value == ''))
	  {
		  alert('Please select the state');
		  changeCountry('');
		  c_state.focus();
		  return false;		
	  }	
	  else if(c_postal_code.value == '')
	  {
		  if(c_country.value == "United States")
			  alert('Please enter the zip code');
		  else
			  alert('Please enter the postal code');
		  c_postal_code.focus();
		  return false;
	  }
	  else if((c_country.value == "United States") && (!isInteger(c_postal_code.value) || c_postal_code.value.length != 5))
	  {
		  alert('Please enter a valid zip code');
		  c_postal_code.focus();
		  return false;
	  }

	}
	
	
	if(c_sharevault_name.value == '')
	{
		alert('Please enter a ShareVault name');
		c_sharevault_name.focus();
		return false;
	}
	else if(c_sharevault_name.value.length > 32)
	{
		alert('Please limit the ShareVault name to a maximum 32 characters');
		c_sharevault_name.focus();
		return false;
	}
	else if(!isValidShareVaultName(c_sharevault_name.value))
	{
		alert('Please enter a ShareVault consisting of only letters, numbers, spaces and underscore characters');
		c_sharevault_name.focus();
		return false;
	}
	else if(c_url.value == '')
	{
		alert('Please enter a URL');
		c_url.focus();
		return false;
	}
	else if(!isValidURL(c_url.value))
	{
		alert('Please enter a valid URL');
		c_url.focus();
		return false;
	}
	if(c_industry.value == 'none')
	{
		alert('Please select your industry');
		c_industry.focus();
		return false;
	}
	if(c_application.value == 'none')
	{
		alert('Please select your application');
		c_application.focus();
		return false;
	}
	if(c_lead_source.value == 'none')
	{
		alert('Please tell us how you heard about us');
		c_application.focus();
		return false;
	}
	if(!c_approval_checkbox.checked)
	{
		alert('Please check the checkbox to indicate that you agree to the terms of the Master Subscription Agreement');
		c_application.focus();
		return false;
	}
	else
	{				
		document.getElementById('pinwheel').style.display='inline';
		
		full_address = c_street.value.split("\n")[0] + ", ";
		if (c_street.value.split("\n").length > 1) full_address += c_street.value.split("\n")[1] + ", ";
		full_address += c_city.value + ", ";
		
		if (c_country.value == "United States") full_address += c_state.value + " ";
		else if (c_country.value == "Canada") full_address += c_province.value + " ";
		
		full_address += c_postal_code.value + ", " + c_country.value;
		
		c_description.value = "/r/nProduct: " + c_product;
						
		c_description.value += "\r\nCapacity: " + c_pages.value + " pages, " + c_mb.value + " MB, " + c_users.value + " users";
		
		expiration.setDate(today.getDate() + c_days.value);
		c_description.value += "\r\nDuration: " + c_days.value + " days (expires " + expiration.toLocaleDateString() + ")";
		
		c_description.value += "\r\n\nComments:\r\n" + c_comments.value;
		
		c_description.value += "\r\n\nSubmitted from IP address: " + c_IP_address.value + " (http://private.dnsstuff.com/tools/ipall.ch?ip=" + c_IP_address.value;
		c_description.value += "\r\nBest-guess website: http://www." + c_email.value.split("@")[1] + "\r\n";
		
		set_cookie("first_name", c_first_name.value);
		set_cookie("last_name", c_last_name.value);
		set_cookie("company", c_company.value);
		set_cookie("title", c_title.value);
		set_cookie("phone", c_phone.value);
		set_cookie("email", c_email.value);
		set_cookie("industry", c_industry.value);
		set_cookie("application", c_application.value);
		set_cookie("lead_source", c_lead_source.value);
		
		return false;
	}
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
	// return String(str).replace(/^\s+|\s+$/g,'');
	// return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	// return String(str || '').replace(/^\s+|\s+$/g,'');
}

/*
Check if a string is in valid email format. 
Returns true if valid, false otherwise.
*/
function isEmail(str)
{
	var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn|ye|yt|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	return regex.test(str);
}


/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function hasUpper(str)
{   var i;
    for (i = 0; i < str.length; i++)
    {   
        // Check that current character is upper case.
        var c = str.charAt(i);
        if (((c >= "A") && (c <= "Z"))) return true;
    }
    // No upper case characters.
    return false;
}

function hasLower(str)
{   var i;
    for (i = 0; i < str.length; i++)
    {   
        // Check that current character is lower case.
        var c = str.charAt(i);
        if (((c >= "a") && (c <= "z"))) return true;
    }
    // No lower case characters.
    return false;
}

function hasDigit(str)
{   var i;
    for (i = 0; i < str.length; i++)
    {   
        // Check that current character is a digit.
		var c = str.charAt(i);
        if (((c >= "0") && (c <= "9"))) return true;
    }
    // No digits.
    return false;
}

function isValidShareVaultName(str)
{   var i, j = 0;

    for (i = 0; i < str.length; i++)
    {   
        // Check that current character is valid.
		var c = str.charAt(i);
        if (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || ((c >= "0") && (c <= "9")) || (c == " ") || (c == "_")) j++;
    }

    if (i == j) return true; else return false;
}

function isValidURL(str)
{   var i, j = 0;
    for (i = 0; i < str.length; i++)
    {   
        // Check that current character is valid.
		var c = str.charAt(i);
        if (((c >= "A") && (c <= "Z")) || ((c >= "a") && (c <= "z")) || ((c >= "0") && (c <= "9")) || (c == "+") || (c == "-") || (c == "-") || (c == "_")) j++;
    }

    if (i == j) return true; else return false;
}


// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com


function encode64(input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
   }
   
   return output.toString();
}

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) {
	var StringMaker = function () {
		this.str = "";
		this.length = 0;
		this.append = function (s) {
			this.str += s;
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.str = s + this.str;
			this.length += s.length;
		}
		this.toString = function () {
			return this.str;
		}
	}
} else {
	var StringMaker = function () {
		this.parts = [];
		this.length = 0;
		this.append = function (s) {
			this.parts.push(s);
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.parts.unshift(s);
			this.length += s.length;
		}
		this.toString = function () {
			return this.parts.join('');
		}
	}
}
