/* v 1.0.13 */

$j.ajaxSetup({
  url: "http://www.searchandpromote.com/",
  global: false,
  async: false,
  cache: false
});

//error msg queue
var msgQ = new Array();
var text_INT = new Array();

text_INT['help']		= 'Form Help';
text_INT['specify']		= 'Specify a ';
text_INT['checking']	= 'Validating';
text_INT['valid']		= 'valid';
text_INT['email']		= 'business email';
text_INT['phone']		= 'phone';
text_INT['domain']		= 'domain';
text_INT['choose']		= 'Choose One';
text_INT['competitive']	= 'Unacceptable competitive';

function _ret(d)
{
	$j.cookie('sfid',d[0]);
	data.sfid = d[0];
}

function updateSelections(PL_data,formselect,has_other,maxlen)
{
	var formHtml = "\n"+'<option value="">--select one--</option>';
	for (i in PL_data)
	{
		if (typeof PL_data[i].text != 'undefined')  
		{
			if (PL_data[i].text.length > maxlen) PL_data[i].text = PL_data[i].text.substr(0,maxlen);
			formHtml += "\n"+'<option value="'+PL_data[i].value+'">'+PL_data[i].text+'</option>';
		}
	}
	if (typeof has_other != 'undefined' && has_other != false) formHtml += "\n"+'<option value="Other">Other'+"</option>\n";
	$j('.web_form select[name='+formselect+']').html(formHtml);
}

//validation function
new function() {
	$j.fn.validate = {

		init: function(o) 
		{
			var goSubmit = true;
			if(o.type == 'text' || o.type == 'textarea') { goSubmit = this.textfield(o) }
			if(o.type == 'select-one' || o.type == 'select-multiple') { goSubmit = this.selectfield(o) };			
			if($j("#" + o.id).hasClass('valid-email')) { goSubmit = this.email(o) };
			if($j("#" + o.id).hasClass('valid-phone')) { goSubmit = this.phone(o) };
			if($j("#" + o.id).hasClass('valid-website')) { goSubmit = this.website(o) };

			return goSubmit;
        },

		textfield: function(o) 
		{
			var checkIt		= $j("#" + o.id).hasClass("required-field");
			var dontCheckIt = $j("#" + o.id).hasClass("hideme");			

			if(checkIt && !dontCheckIt)
			{
				if(o.value == "" || o.value.length < 2) 
				{ 
					doError(o,text_INT['specify'],$j("#" + o.id).attr("real_name"));
					return false;
				}
				else doSuccess(o);
			}
			return true;
		},
		selectfield: function(o) {
			var checkIt = $j("#" + o.id).hasClass("required-field");
			var dontCheckIt = $j("#" + o.id).hasClass("hideme");
			if(checkIt && !dontCheckIt) 
			{
				if($j(o).val().length)
				{
					doSuccess(o);
					return true;
				} 
				else 
				{
					doError(o,text_INT['specify'], $j("#" + o.id).attr("real_name"));
					return false;
				}
			} 
			else 
			{
				doSuccess(o);
				return true;
			}
		},

		email: function(o) 
		{
			var emailvalid = true;
			var emailRegExp1 = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
			var emailRegExp2 = /@(gmail\.com|hotmail\.com|yahoo\.com|msn\.com|sfdcglobal\.net|earthlink\.net|aol\.com|comcast\.net|sbcglobal\.net|yahoo\.co\.uk|hotmail\.co\.uk|yahoo\.co\.in|bellsouth\.net|cox\.net|earthlink\.net|verizon\.net|rediffmail\.com|yahoo\.ca|charter\.net|btinternet\.com|ntlworld\.com|yahoo\.fr|gmx\.de|gmx\.net|hotmail\.de|hotmail\.fr|yahoo\.de)/gi;
			var emailRegExp3 = /(webtrends|coremetrics|googleanalytics|clicktracks|fireclick|clickfox)\.com/gi;
			if (!o.value.match(emailRegExp1)) {
				doError(o,text_INT['specify'],text_INT['valid']+ ' '+text_INT['email']);
				emailvalid = false;
			} else
			if (o.value.match(emailRegExp2)) {
				doError(o,text_INT['specify'],text_INT['valid']+ ' '+text_INT['email']);
				emailvalid = false;
			} else
			if (o.value.match(emailRegExp3)) {
				doError(o,'',text_INT['competitive']+' '+text_INT['email']);
				emailvalid = false;
			}

			if (emailvalid && !$j.myCustomVars.emailvalid[o.value])
			{
				emailvalid = true;
			}

			if (emailvalid) { doSuccess(o); }
			return emailvalid;
		},
		website:  function(o)
		{
			sitevalid = true;

			if (!sitevalid) doError(o,text_INT['specify'],text_INT['domain']);

			return(sitevalid);
		},
		phone: function(o) {
			var phoneValid = true;
			var phoneRegex = /^([0-9(e|x|t)#\+\)\(\.\-\s])+$/;
			if (!o.value.match(phoneRegex)) {
				doError(o,text_INT['specify'],text_INT['valid']+' '+text_INT['phone']);
				phoneValid = false;
			}
			if (phoneValid) { doSuccess(o); }
			return phoneValid;
		}
	};

	function doSuccess(o) 
	{
		$j('#'+o.id).next().html("&nbsp;");
		$j("#" + o.id).removeClass("error-field");
	}


	function doError(o,s,m) 
	{
		var oPos = $j(o).position();
		var msg = '<li class="'+o.id+'">'+s+' <strong>'+m+'</strong></li>';

		if(o.type == "text" || o.type == "textarea") 
		{
			if(o.value=="")
				o.value = o.defaultValue;
		}

		$j('#'+o.id).next().html(msg);
		$j("#" + o.id).addClass("error-field");
		return false;
	}
	
};




$j(document).ready(function(){
	$j.myCustomVars = {};
	$j.myCustomVars.sitevalid			= new Array();
	$j.myCustomVars.emailvalid			= new Array();
	
	updateSelections(PL_data['countries'],'country',false,25);

	$j("form.web_form select,form.web_form input").blur(function() { $j(this).validate.init(this); });
	
	$j('form.web_form,form.lead-form-01').bind('submit',function(event) {
		event.preventDefault();

		var has_errors = 0;
		var dataString = '';

		$j('.formfields').hide();
		$j('.formprocess').show().html("<p class='spinner'>Please wait a moment while we save your request.</p>");
		
		
		//fail if input validate false
		$j('form input[type=text]').each( function() {
			has_errors += (!$j(this).validate.init(this)) ? 1 : 0;
		});
		
		//fail if select validate false
		$j('form select').each( function() {
			has_errors += (!$j(this).validate.init(this)) ? 1 : 0;
		});
		
		if (has_errors == 0)  
		{
/*
			$j('form').append('<img src="http://www.omniture.com/magic.html?'+dataString+'" />').ready(function()
			{
				$j('#contact_guest').dialog('close');
				$j.cookie('purl_form',1);
			});;
*/

			//$j('form').load('http://www.omniture.com/magic.html'+dataString);
			if($j(this).attr('name') != 'masterwebform')
			{
				dataString = $j('form.web_form').serialize();
				dataString = dataString+'&_ret=jsonp';

				$j.ajax({  
				  type: "POST",  
				  url: "http://www.omniture.com/magic.html",  
				  dataType: 'jsonp',
				  data: dataString,  
				  complete: function() {  
					$j('#contact_guest').dialog('close');
					$j.cookie('purl_form',1);
					$j('#contact_thanks').dialog({closeText: 'x'});
				  }  
				});  
			}
			else
			{
				dataString = $j('form.lead-form-01').serialize();
				dataString = dataString+'&_ret=jsonp';

				$j.ajax({  
				  type: "POST",  
				  url: $j(this).attr('action'),
				  dataType: 'xml',
				  data: dataString,  
				  complete: function() {  
					$j('#contact_guest').dialog('close');
					$j.cookie('purl_form',1);
					$j('#contact_thanks').dialog({closeText: 'x'});
				  }  
				});  
			}
		}
		else
		{
			$j('.formfields').show();
			$j('.formprocess').hide();
		}
	});
});

