
function checkContact(applicationID, req_typeID)
{
	var c_first_name, c_last_name, c_company, c_title, c_phone, c_email, c_industry, c_application, c_req_type, c_lead_source, c_description, c_IP_address;
	with(window.document.contact)
	{
		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_application = document.getElementById(applicationID); c_application.value = trim(c_application.value);
		c_req_type = document.getElementById(req_typeID); c_req_type.value = trim(c_req_type.value);
		c_industry = industry; c_industry.value = trim(c_industry.value);
		c_lead_source = lead_source; c_lead_source.value = trim(c_lead_source);
		c_description = description; c_description.value = trim(c_description);
		c_IP_address = IP_address; c_IP_address = trim(c_IP_address);
	}
	
	if(c_first_name.value == '')
	{
		alert('Please enter your 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 your 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_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_req_type.value == 'none')
	{
		alert('Please select a request type');
		c_req_type.focus();
		return false;
	}
	else if((c_req_type.value == 'Sales Inquiry') && (c_industry.value == 'none'))
	{
		alert('Please select an industry');
		c_industry.focus();
		return false;
	}
	else if((c_req_type.value == 'Sales Inquiry') && (c_application.value == 'none'))
	{
		alert('Please select an application');
		c_application.focus();
		return false;
	}
	else if((c_req_type.value == 'Sales Inquiry') && (c_lead_source.value == 'none'))
	{
		alert('Please tell us how you heard about us');
		c_lead_source.focus();
		return false;
	}
	else
	{
		document.getElementById('pinwheel').style.display='inline';
		
		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 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);
}
