var _ledid = '';
var $j = jQuery.noConflict();

$j(document).ready( function () 
{
	$j.ajax({
        type: 'GET',
            url: path + '/rePV',
            data: '',
            async: true
	});
});

inputTextInit = Class.create();
inputTextInit.applyTo('input[type="text"]');
inputTextInit.prototype = {
	
	onclick: function(){
		this.value = '';
		this.onclick = null;
	}
	
}

function OpenSnippet( snippet, ledid )
{
	window.open( path +'/siteRedirect?snippet=' + snippet + '&lid=' + ledid, "Snippet" );
}

function DownloadSnippet( snippet, ledid )
{
	window.open( path +'/siteRedirect?snippet=' + snippet + '&lid=' + ledid, "popDown", "width=1,height=1,top=0,left=0" );
}

function SubmitPassSecond( snippet, ledid, action )
{
	var run = action + '(\'' + snippet + '\',\'' + ledid + '\' )';
	eval( run );
	return false;
}

function SubmitPassLead( snippet, action )
{
	
	var _email = $j('form input[name="Email"]');
	if( _email.length )
	{
		var _email2 = $j('form input[name="Email2"]');
		if( _email2.length )
		{
			if( EmailCheck(_email2) )
			{
				_email.val( _email2.val() );
			}
		}
	}
	//Validate form
	
	if( !MandatoryCheck(_email) ){
		alert(ss.i18n.sprintf(ss.i18n._t( 'VALIDATOR.FIELDREQUIRED' ), 'Email'));
		return false;
	}
	if(!EmailCheck(_email)){
		alert(ss.i18n.sprintf(ss.i18n._t( 'VALIDATOR.VALIDEMAIL' )));
		$j.ajax({
			type: 'POST',
			url: path + '/lbmAN/',
			data: 'email=' + _email.val(),
			async: true
		})
        return false;
	}
	var _terms = $j('form input[name="Terms"]');
	if(_terms.length && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(ss.i18n.sprintf(ss.i18n._t( 'VALIDATOR.FIELDREQUIRED' ), 'Terms & Conditions'));
			return false;
		}
	}

	$j.ajax({
	    type: 'GET',
		url: path + '/createLead/',
		data: 'email=' + _email.val() ,
		async: false,
		success: function(t)
		{
			_ledid = t;
		}
	});
	if ( _ledid == 0 )
	{
		return false;
	}
	if( snippet != "undefined" && snippet.length > 0 )
	{
		if( action == 'OpenSnippet' )
			OpenSnippet( snippet, _ledid );
		else if( action == 'DownloadSnippet' )
			DownloadSnippet( snippet, _ledid );
	}
	return true;		

}

function SubmitDownloadSnippet2Tier( snippet, ledid ){

	DownloadSnippet( snippet, ledid );
	return true;
}

function SubmitPassLead1()
{
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}
	///create lead
	//new Ajax.Request('/createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});
	$j.ajax({
	    type: 'GET',
		url: 'createledid/' +  _email.value,
		data: '',
		async: false,
		success: function(t)
		{
			_ledid=t.responseText;
		}
	});

	if (_ledid == 0) {
		return false;
	}
	return true;
}

/*mandatory checks*/
function MandatoryCheck(_elm)
{
	return trim(_elm.val()) != '';
}

/*checking email validity*/
function EmailCheck(_elm)
{
	reg = new RegExp(/^([\w\-]+)(\.[\w]+)*@([\w\-]+){2,}(\.[\w]{2,7})(\.[a-zA-Z]{2})?$/);
	return reg.exec(_elm.val()) != null;
}

/*checking terms status*/
function TermsCheck(_elm)
{
	return _elm.is(':checked');
}

handleLedID = function(t){
	_ledid=t.responseText;
}

/*trim all*/
function trim(_string){
	return _string.replace(/^\s+|\s+$/g,"");
}

function unsubscribe()
{
	var email = $j('#Form_UnsubForm_Email').val();
	if( email && typeof email != 'undefined' )
	{
		$j.ajax({
		    type: 'GET',
		    url: 'createsnsess/uss/' ,
		    data: 'Email=' + email,
		    async: false
		})
	}
}


