// JavaScript Document
var tsfGlobal_timeout;
var tsfStat = true;
var tsfFullpat = '';
$j(document).ready(function(){tsfLoader();});
function tsfLoader(){
	
	
	$j('link').each(function(){
		if($j(this).attr('rel') == 'index')tsfFullpat = $j(this).attr('href');						 
	});
	
	if(document.getElementById('tsfFrontEnabler')){
		clearTimeout(tsfGlobal_timeout);
		if($j('#tsfFrontEnabler').attr('title') == 'free_site_analysis'){
			tsfLoadFront();			
		}else if($j('#tsfFrontEnabler').attr('title') == 'contact'){
			tsf_contact_page();	
		}
	
		$j('.tsf_validator').each(function(){
			var tsf_v = $j(this);
			tsf_v.focus(function(){
				if($j(this).val() == $j(this).attr('title')){
					$j(this).val('');		
				}
			});
			tsf_v.blur(function(){
				if($j(this).val() == ''){
					$j(this).val($j(this).attr('title'));
				}else if($j(this).val() == $j(this).attr('title')){
					$j(this).val($j(this).attr('title'));
				}
			});
			
		});
		(document.getElementById('tsfdialog'))?'':$j('body').prepend('<div id="tsfdialog"></div>');
		
	}else{
		tsfGlobal_timeout = setTimeout("tsfLoader()", 500);
	}
}


function tsfEmptyStat(o, thebgError){
	if(o.value == '' || o.value == o.title){
		$j(o).parent().css('background', 'url('+thebgError+')');
		$j(o).parent().css('background-repeat', 'no-repeat');
		$j(o).css('color', '#f89695');
		tsfStat = false;
	}
}

function tsfURLStat(o, thebgError){
	var theURLs = o.value;
	var v = new RegExp();
	if(theURLs.length>0 && theURLs !=o.title){
		
		if(theURLs.substring(0,7) == 'http://')theURLs = theURLs.substring(7);
		//v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		v.compile("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		if (!v.test(theURLs)) {
			$j(o).parent().css('background', 'url('+thebgError+')');	
		$j(o).parent().css('background-repeat', 'no-repeat');
			$j(o).css('color', '#f89695');
			tsfStat = false;
		}
	}
}

function tsfEmailStat(o, thebgError){
	
	var email = o.value;
	var passed;
	var blnRetval, intAtSign, intDot, intComma, intSpace, intLastDot, intDomain, intStrLen;
	intAtSign=email.indexOf("@");
	intDot=email.indexOf(".",intAtSign);
	intComma=email.indexOf(",");
	intSpace=email.indexOf(" ");
	intLastDot=email.lastIndexOf(".");
	intDomain=intDot-intAtSign;
	intStrLen=email.length;
	// *** CHECK FOR BLANK EMAIL VALUE
	
	if (intAtSign == -1){
		$j(o).parent().css('background', 'url('+thebgError+')');
		$j(o).parent().css('background-repeat', 'no-repeat');	
		$j(o).css('color', '#f89695');
		tsfStat = false;
	}else if (intComma != -1){
		$j(o).parent().css('background', 'url('+thebgError+')');
		$j(o).parent().css('background-repeat', 'no-repeat');	
		$j(o).css('color', '#f89695');
		tsfStat = false;
	}else if (intSpace != -1){
		$j(o).parent().css('background', 'url('+thebgError+')');
		$j(o).parent().css('background-repeat', 'no-repeat');	
		$j(o).css('color', '#f89695');
		tsfStat = false;
	}else if ((intDot <= 2) || (intDomain <= 1)  || (intStrLen-(intLastDot+1) < 2)){
		$j(o).parent().css('background', 'url('+thebgError+')');	
		$j(o).parent().css('background-repeat', 'no-repeat');
		$j(o).css('color', '#f89695');
		tsfStat = false;
	}
};

function tsfNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))return false;
	return true;
}

function tsfLoadFront(){
	var theBg = tsfFullpat+'/wp-content/themes/everspark-2011/images/formtext-bg.jpg';
	var thebgError = tsfFullpat+'/wp-content/themes/everspark-2011/images/formtext-bg_red.jpg';
	
	$j('#tsf_analysis').submit(function(){
		$j('.input_style_holder').children('input').css('color', '#666666');
		$j('.input_style_holder').css('background', 'url('+theBg+')');	
		tsfStat = true;
		
		tsfEmptyStat(document.tsf_analysis.fname, thebgError);
		tsfEmptyStat(document.tsf_analysis.company, thebgError);
		tsfEmptyStat(document.tsf_analysis.phone, thebgError);
		tsfEmptyStat(document.tsf_analysis.email, thebgError);
		tsfEmptyStat(document.tsf_analysis.websiteURL, thebgError);
		tsfEmailStat(document.tsf_analysis.email, thebgError);
		tsfURLStat(document.tsf_analysis.websiteURL, thebgError);
		
		if(tsfStat == true){
			$j('.tsf_analysis_loader').css('visibility','visible');			
			urlu=document.getElementById('tsfFrontEnabler').value+'?action=submitAnalysis';							
			$j.ajax({
			  type: 'POST',
			  url: urlu,
			  data: $j(this).serialize(),
			  success: function(data){
				$j('.tsfanalysis_form').css('opacity', 0);
				$j('.tsfanalysis_form').html(data);
				$j('.tsfanalysis_form').animate({
					opacity:1
				}, "slow");
			  }
			});
		}else{
		}
		
		return false;
	
	});
}
	
function tsfresubmit(){
	$j('.tsfanalysis_form').css('opacity', 0);
	$j.get(document.getElementById('tsfFrontEnabler').value, '', function(data){
		$j('.tsfanalysis_form').html(data);
		$j('.tsfanalysis_form').animate({
			opacity:1
		}, "slow");
		tsfLoadFront();
	});
}

function tsf_contact_page(){
	var theBg = tsfFullpat+'/wp-content/themes/everspark-2011/images/txtboxBg.jpg';
	var thebgError = tsfFullpat+'/wp-content/themes/everspark-2011/images/txtboxBg_red.jpg';
	var theBgTxtArea = tsfFullpat+'/wp-content/themes/everspark-2011/images/txtareaBg.jpg';
	var thebgTxtAreaError = tsfFullpat+'/wp-content/themes/everspark-2011/images/txtareaBg_red.jpg';
	//
	$j('#tsf_contactform').submit(function(){
		$j('.txtbox').children('input').css('color', '#666666');
		$j('.txtbox').css('background', 'url('+theBg+')');	
		$j('.txtbox').css('background-repeat', 'no-repeat');
		tsfStat = true;
		
		tsfEmptyStat(document.tsf_contactform.name, thebgError);
		tsfEmptyStat(document.tsf_contactform.phone, thebgError);
		tsfURLStat(document.tsf_contactform.website, thebgError);
		tsfEmailStat(document.tsf_contactform.email, thebgError);
		
		if(tsfStat == true){
			$j('.tsf_analysis_loader').css('visibility','visible');			
			urlu=document.getElementById('tsfFrontEnabler').value+'?action=submitContact';							
			$j.ajax({
			  type: 'POST',
			  url: urlu,
			  data: $j(this).serialize(),
			  success: function(data){
				
				$j('.tsfcontact_form').css('opacity', 0);
				$j('.tsfcontact_form').html(data);
				$j('.tsfcontact_form').animate({
					opacity:1
				}, "slow");
			  }
			});
		}else{
		}
		
		return false;
	
	});
}
	
function tsfcontactresubmit(){
	$j('.tsfanalysis_form').css('opacity', 0);
	$j.get(document.getElementById('tsfFrontEnabler').value, '', function(data){
		$j('.tsfcontact_form').html(data);
		$j('.tsfcontact_form').animate({
			opacity:1
		}, "slow");
		tsf_contact_page();
	});
}
	
function tsfdialog(text){
	$j.fx.speeds._default =1500;
	$j("#tsfdialog").html(text);
	$j("#tsfdialog:ui-dialog").dialog( "destroy" );
	$j("#tsfdialog").attr('title', 'Notification');
/*
	$j( "#tsfdialog" ).dialog({
		resizable: false,
		width:350,
		modal: true,
		buttons: {
			"Okay" : function() {
				$j( this ).dialog( "close" );
			}
		}
	});
*/
	$j( "#tsfdialog" ).dialog({
		resizable: false,
		width:800,
		height:600,
		show: "fade",
		hide: "fade",
		modal: true
	});
}
