var countries = new Array('us', 'ca', 'uk', 'other');

function setGASource() 
{
	var map = {};
	map = getUrlVars();
	var leadListSrc = document.getElementById('00N30000004qVxC');
	if ( leadListSrc != null ) {
		if (map['utm_source'] != 'undefined')
			leadListSrc.value = map['utm_source'];
	}
	
	var utmSource = document.getElementById('00N30000007gGvm');
	if ( utmSource != null ) {
		if (map['utm_source'] != 'undefined')
			utmSource.value = map['utm_source'];
	}
	
	var utmCamp = document.getElementById('00N30000007gGvr');
	if ( utmCamp != null ) {
		if (map['utm_campaign'] != 'undefined')
			utmCamp.value = map['utm_campaign'];
	}
	
	var utmMedium = document.getElementById('00N30000007gGvw');
	if ( utmMedium != null ) {
		if (map['utm_medium'] != 'undefined')
			utmMedium.value = map['utm_medium'];
	}
	
	var utmContent = document.getElementById('00N30000007gGw1');
	if ( utmContent != null ) {
		if (map['utm_content'] != 'undefined')
			utmContent.value = map['utm_content'];
	}
	
	var utmTerm = document.getElementById('00N30000007gGw6');
	if ( utmTerm != null ) {
		if (map['utm_term'] != 'undefined')
			utmTerm.value = map['utm_term'];
	}
}
	
function getUrlVars() 
{
    var map = {};

    var s = window.location.href;

    if (s.indexOf('&amp;') == -1) {
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
         map[key] = value;
	});
	} else {

	var hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&amp;');
	for(var i = 0; i < hashes.length; i++)
	  {
	   hash = hashes[i].split('=');
	   map[hash[0]] = hash[1];
	  }
	}	
    return map;
}

function validateForm(pForm)
{
	var reason = "";
	var field = "";
	
	//validate first name
	field = pForm.first_name;
	if (field.value.length == 0)
	{
		field.style.background = "Yellow";
		reason += "First name is a required field and must be filled in.\n";
	}
	else
	{
		field.style.background = "White";
	}
			
	//validate last name
	field = pForm.last_name;
	if (field.value.length == 0)
	{
		field.style.background = "Yellow";
		reason += "Last name is a required field and must be filled in.\n";
	}
	else
	{
		field.style.background = "White";
	}
			
	//validate company
	field = pForm.company;
	if (field.value.length == 0)
	{
		field.style.background = "Yellow";
		reason += "Company is a required field and must be filled in.\n";
	}
	else
	{
		field.style.background = "White";
	}
	
	//validate country
	field = pForm.country;
	if (field.value.length == 0)
	{
		field.style.background = "Yellow";
		reason += "Country is a required field and must be filled in.\n";
	}
	else
	{
		field.style.background = "White";
	}
			
	//validate phone
	field = pForm.phone;
	var stripped = field.value.replace(/[\(\)\.\-\ ]/g, '');
	if (field.value.length == 0)
	{	
		field.style.background = "Yellow";
		reason += "Phone is a required field and must be filled in.\n";
	}
	else if (isNaN(parseInt(stripped)))
	{
		field.style.background = "Yellow";
		reason += "Phone contains invalid characters.\n";
	}
	else
	{
		field.style.background = "White";
	}
			
	//validate email
	field = pForm.email;
	if (field.value.length == 0)
	{
		field.style.background = "Yellow";
		reason += "Email is a required field and must be filled in.\n";
	}
	else if ( field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1 )
	{
		field.style.background = "Yellow";
		reason += "Email is invalid.\n";
	}
	else
	{
		field.style.background = "White";
	}
			
	//validate MainBusinessActivity  --None--
	field = document.getElementById('00N30000004q1T3');
	if (field.value == '--None--')
	{
		field.style.background = "Yellow";
		reason += "Main Business Activity is a required field and a value must be selected.\n";
	}
	else
	{
		field.style.background = "White";
	}
			
			
	//validate
	if (reason.length > 0) 
	{	
		alert(reason);
		return false;
	}
	else {
		return true;
	}
		
}

function setStateProvinceOptions() {
	
	var selectedCountryIndex = 3;
	
	switch ($('#country').val()) {
		case "US":
			selectedCountryIndex = 0; 
		break;
		case "CA":
			selectedCountryIndex = 1;
		break;
		case "GB":
			selectedCountryIndex = 2;
		break;
		default:
		break;
	}
	
	for (var i = 0; i < countries.length; i++) {
		$('#state_province_' + countries[i]).css('display', i == selectedCountryIndex ? 'block' : 'none');
	}

	$('#state_province_label').attr('for', 'state_province_' + countries[selectedCountryIndex]);
	$('#state_province_container label.error').remove();
	$('#state_province_container select, #state_province_container input').removeClass('error');
	
	setStateProvinceCode(selectedCountryIndex);
	
}

function setStateProvinceCode(selectedCountryIndex) {
	
	if (selectedCountryIndex != 3) {
		$('#state_province_code').attr('value', $('#state_province_' + countries[selectedCountryIndex]).val());
	} else {
		$('#state_province_code').removeAttr('value');
	}
	
}

function setPhoneCode(phoneCodeNumbers,countryCode,phoneCodeNumber) {
	var st=document.getElementById(phoneCodeNumbers).value;
	var countryCode=document.getElementById(countryCode).value;
	var bar = st.split("|");
	for (var i = 0;i<bar.length;i++) {
		if (countryCode==bar[i].match(countryCode)) {
			var codeNum = bar[i].split("=");
			document.getElementById(phoneCodeNumber).value=codeNum[1];
			break;
		}
	}
}

function populateCampaignFields() {
	
	if ($.url.param('utm_campaign') != null) {
		$('#campaign_id').val($.url.param('utm_campaign'));
	}
	
	if ($.url.param('utm_medium') != null) {
		$('#campaign_type').val($.url.param('utm_medium'));
	}
	
}

function insertGATrackingFromUrchin(formSelector) {     

	var trackingSource = document.getElementById('GA_TrackingSource');
	var trackingMedium = document.getElementById('GA_TrackingMedium');
	var trackingTerm = document.getElementById('GA_TrackingTerm');
	var trackingContent = document.getElementById('GA_TrackingContent');
	var trackingCampaign = document.getElementById('GA_TrackingCampaign');

	if ($.url.attr('query') != null) {

		if ($.url.param('utm_source') != null) {
			if (trackingSource == null) {
				$(formSelector).append('<input type="hidden" name="Client.ClientPortalRelations.ClientPortalRelation.GaTrackingSource" id="GA_TrackingSource" value="' + $.url.param('utm_source') + '"/>');
			} else {    
				trackingSource.value = $.url.param('utm_source');
			}
		}

		if ($.url.param('utm_medium') != null) {
			if (trackingMedium == null) {
				$(formSelector).append('<input type="hidden" name="Client.ClientPortalRelations.ClientPortalRelation.GaTrackingMedium" id="GA_TrackingMedium" value="' + $.url.param('utm_medium') + '"/>');
			} else {    
				trackingMedium.value = $.url.param('utm_medium');
			}
		}

		if ($.url.param('utm_term') != null) {
			if (trackingTerm == null) {
				$(formSelector).append('<input type="hidden" name="Client.ClientPortalRelations.ClientPortalRelation.GaTrackingTerm" id="GA_TrackingTerm" value="' + $.url.param('utm_term') + '"/>');
			} else {    
				trackingTerm.value = $.url.param('utm_term');
			}
		}

		if ($.url.param('utm_content') != null) {
			if (trackingContent == null) {
				$(formSelector).append('<input type="hidden" name="Client.ClientPortalRelations.ClientPortalRelation.GaTrackingContent" id="GA_TrackingContent" value="' + $.url.param('utm_content') + '"/>');
			} else {    
				trackingContent.value = $.url.param('utm_content');
			}
		}

		if ($.url.param('utm_campaign') != null) {
			if (trackingCampaign == null) {
				$(formSelector).append('<input type="hidden" name="Client.ClientPortalRelations.ClientPortalRelation.GaTrackingCampaign" id="GA_TrackingCampaign" value="' + $.url.param('utm_campaign') + '"/>');
			} else {    
				trackingCampaign.value = $.url.param('utm_campaign');        
			}
		}
		
	}
	
}
