var red_page = '';

function setRedPage( _red_page )
{
	red_page = _red_page;
}

registerOnClick();

function registerOnClick()
{
	// Usage in firefox
	if (typeof document.addEventListener != "undefined")
	{
		window.addEventListener("click", captureOnClick, false)
	} // Usage in IE
	else if (typeof document.attachEvent != "undefined")
	{
		document.attachEvent("onclick", captureOnClick);
	}
}

function captureOnClick( event )
{
	var t = getTarget( event );
	if( t.id == 'email' )
	{
		email_f = document.getElementById('email');
		if( email_f != null && typeof email_f != 'undefined' )
		{
			email_f.onclick = null;
		}
	}
}

function getTarget(x)
{
    x = x || window.event;
    return x.target || x.srcElement;
}

function copy_values(e_to_ef)
{
	var _e = document.getElementById('email');
	var _ef = document.getElementById('emailField');
	if(e_to_ef)
		_ef.value = _e.value;
	else
		_e.value = _ef.value;
}

function bgImageCache() {
	var m = document.uniqueID /*IE*/
	&& document.compatMode /*>=IE6*/
	&& !window.XMLHttpRequest /*<=IE6*/
	&& document.execCommand ;
	try{
		if(!!m){
			m("BackgroundImageCache", false, true) /* = IE6 only */
		}
	}catch(oh){};
}

function externalLinks( ) {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
	  var anchor = anchors[i];
	  if (anchor.getAttribute("href") &&
	      anchor.getAttribute("rel") == "external")
	    anchor.target = "_blank";
	}
}

function check_hostname( _url ){
	if(window.location.hostname.match(/[a-zA-Z]+\.dev$/)){
		var _host_arr = window.location.hostname.split(/\./);
		_url = _url + '.' + _host_arr[2] + '.dev';
	}
	else if(window.location.hostname.match(/^devel\.www\./))
		_url = 'devel.www.' + _url;
	else if(window.location.hostname.match(/^qa\.www\./))
		_url = 'qa.www.' + _url;
	else if(window.location.hostname.match(/^stage\.www\./))
		_url = 'stage.www.' + _url;

	return _url;
}

function OpenSnip( _snippet, _lid, _aid, _aextra )
{
	_rand = Math.random( );
	_wname = Math.round(_rand * 1000).toString();
	window.open('site_redirect.html?snippet='+_snippet+'&ledid='+_lid+'&aid='+_aid+'&aextra='+_aextra,"Download"+_wname);

}

var _ledid=0;

function SubmitOpenSnip( _snippet ){
	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});
	///start download process via "hitsyndication" host
	if(_ledid!=0) {
		OpenSnip(_snippet, _ledid);
    	var aweber = document.getElementById('aweber_frame');
    	aweber.src = 'aweber_autoform.html';
	}
    else {
    	return false;
    }
	return true;
}
var form_id ='';
function SubmitLead( _formname )
{
	form_id = _formname;
	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});

	if(_ledid!=0) {
    	var aweber = document.getElementById('aweber_frame');
    	aweber.src = 'aweber_autoform.html';
	}
    else {
    	return false;
    }
	setTimeout('submit_form()', 2000 );
	return false;
}

function SubmitPassLead()
{
	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});

	if (_ledid == 0) {
		return false;
	}
	return true;
}

function submit_form()
{
	var _form = document.getElementById(form_id);
	if(_form != null && typeof _form != "undefined" ){
		_form.submit( );
	}
}

function MandatoryCheck(_elm){
	return trim(_elm.value) != '';
}

function EmailCheck(_elm)
{
	reg = new RegExp(/^([\w\-]+)(\.[\w\-]+)*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/);
	var email_addr = _elm.value.split(' ').join('');
	if( reg.exec(email_addr) == null )
	{
		new Ajax.Request('nve_log.html', {parameters: 'nvel=' + email_addr, asynchronous: true, method: 'get' });
		return false;
	}
	else
	{
		return true;
	}
}

function TermsCheck(_elm){
	return _elm.type == "checkbox" ? _elm.checked : true;
}

/*trim functions*/
/*left trim*/
function ltrim(_string){
	return _string.replace(/^\s+/,"");
}

/*right trim*/
function rtrim(_string){
	return _string.replace(/\s+$/,"");
}
/*trim all*/
function trim(_string){
	return _string.replace(/^\s+|\s+$/g,"");
}

handleLedID = function(t){
  _ledid=t.responseText;
}
