
function checkQuote(applicationID, product)
{
	var c_first_name, c_last_name, c_company, c_title, c_phone, c_email, c_industry, c_application, c_lead_source, c_plan, c_months, c_pages, c_users, c_mb, c_funds, c_description, c_scanning, c_ocr, c_dvd, c_IP_address, c_HTTP_referer, tmp_string;

	with(window.document.sharevault_quote)
	{	
		c_first_name	= first_name;	c_first_name.value =	trim(c_first_name.value);
		c_last_name		= last_name;	c_last_name.value =		trim(c_last_name.value);
		c_company		= company;		c_company.value =		trim(c_company.value);
		c_title			= title;		c_title.value =			trim(c_title.value);
		c_phone			= phone;		c_phone.value =			trim(c_phone.value);
		c_email			= email;		c_email.value =			trim(c_email.value);
		c_industry		= industry;		c_industry.value =		trim(c_industry.value);
		c_application = document.getElementById(applicationID);
		c_application.value = trim(c_application.value);
		c_lead_source	= lead_source;	c_lead_source.value =	trim(c_lead_source.value);
		if(product == "ShareVault" || product == "ShareVault Express") {
			c_plan			= plan;			c_plan.value =			trim(c_plan.value);
			c_months		= months;		c_months.value =		trim(c_months.value);
			c_pages			= pages;		c_pages.value =			trim(c_pages.value);
			c_users			= users;		c_users.value =			trim(c_users.value);
			c_mb			= mb;			c_mb.value =			trim(c_mb.value);
		}
		if(product == "ShareVault") {
			c_scanning		= scanning;		c_scanning.value =		trim(c_scanning.value);
			c_ocr			= ocr;			c_ocr.value =			trim(c_ocr.value);
			c_dvd			= dvd;			c_dvd.value =			trim(c_dvd.value);
		}
		if(product == "ShareVault LP Portal") {
			c_funds			= funds;		c_funds.value =		trim(c_funds.value);
		}
		c_description	= description;
		c_IP_address = IP_address;
		c_HTTP_referer = HTTP_referer;
	}
	
	if(c_first_name.value == '')
	{
		alert('Please enter your first name');
		c_first_name.focus();
		return false;
	}
	if(c_last_name.value == '')
	{
		alert('Please enter your last name');
		c_last_name.focus();
		return false;
	}
	else if(c_company.value == '')
	{
		alert('Please enter your company name');
		c_company.focus();
		return false;
	}
	else if(c_title.value == '')
	{
		alert('Please enter your title');
		c_title.focus();
		return false;
	}
	else if(c_phone.value == '')
	{
		alert('Please enter your 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 your 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_industry.value == 'none')
	{
		alert('Please select your industry');
		c_industry.focus();
		return false;
	}
	else if(c_application.value == 'none')
	{
		alert('Please select your application');
		c_application.focus();
		return false;
	}
	else if(c_lead_source.value == 'none')
	{
		alert('Please tell us how you heard about us');
		c_lead_source.focus();
		return false;
	}
	else if(product == "ShareVault" || product == "ShareVault Express")
	{
		if((c_plan[0].checked) && (!isInteger(c_months.value) || !((parseInt(c_months.value) <= 12) && (parseInt(c_months.value) >= 1))))
		{
			alert('The project duration must be between 1 and 12 months');
			c_months.focus();
			return false;
		}
		else if(!isInteger(c_months.value) || !((parseInt(c_pages.value) <= 1000000000) && (parseInt(c_pages.value) >= 1000)))
		{
			alert('The number of pages must be a number greater than or equal to 1000');
			c_pages.focus();
			return false;
		}
		else if(!isInteger(c_months.value) || !((parseInt(c_users.value) <= 10000) && (parseInt(c_users.value) >= 10)))
		{
			alert('The number of users must be a number greater than or equal to 10');
			c_users.focus();
			return false;
		}
		else if((c_mb.value != '') && (!isInteger(c_months.value) || !((parseInt(c_mb.value) <= 100000) && (parseInt(c_mb.value) >= 10))))
		{
			alert('The storage in MB must be either blank or a number greater than 10');
			c_users.focus();
			return false;
		}
	}
	else if((product == "ShareVault LP Portal") && (!isInteger(c_funds.value) || (c_funds.value < 1)))
	{
		alert('The number of funds must be a number greater than or equal to 1');
		c_funds.focus();
		return false;
	}
	else
	{
		document.getElementById('pinwheel').style.display='inline';
		tmp_string = c_description.value;
		c_description.value = "Product: " + product + "\r\n";
		if (product == "ShareVault" || product == "ShareVault Express") {
			c_description.value += "Subscription Plan: ";
			if (c_plan[0].checked)
				c_description.value += "Project, duration: " + c_months.value + " months\r\n";
			else
				c_description.value += "Ongoing (month-to-month)\r\n";
			c_description.value += "Capacity: " + c_users.value + " users, "+ c_pages.value + " pages, " + c_mb.value + " mb\r\n";
		}
		if(product == "ShareVault") {
			c_description.value += "Optional Services: ";
	
			if (!c_scanning.checked && !c_ocr.checked && !c_dvd.checked)
				c_description.value += "None, ";
			else {
				if (c_scanning.checked)
					c_description.value += "Document Scanning, ";
				if (c_ocr.checked)
					c_description.value += "OCR, ";
				if (c_dvd.checked)
					c_description.value += "DVD Archive, ";
				}
			c_description.value += "!";  // Just a hack to make it easy to find the comma that needs to be deleted
			c_description.value = c_description.value.replace(", !", "\r\n\n");
		}
		if (product == "ShareVault LP Portal") {
			c_description.value += "Number of Funds: " + c_funds.value + "\r\n\n";
		}
		c_description.value += "Comments:\r\n" + trim(tmp_string);
		
		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\nHTTP referer: " + c_HTTP_referer.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 true;
	}
}

function set_cookie(name, value)
{
	var exdate=new Date();
	// 10 year expiration
	exdate.setDate(exdate.getDate()+365*10);
	document.cookie = name + " = " + escape(value) + "; expires = " + exdate.toUTCString() + "; path = /; domain = sharevault.com;";
	document.cookie = name + " = " + escape(value) + "; expires = " + exdate.toUTCString() + "; path = /; domain = pandesa.com;";
}

/*
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);
}

