//alert ("Hi - Welcome to catch_click.js");

catch_click_lockout 	= false;
new_win 				= null;

//----------
/*
var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
if(mine) {
	var popUpsBlocked = false;
	
	alert ('popUpsBlocked is FALSE');
	
} else {
	var popUpsBlocked = true;
	
	alert ('popUpsBlocked is TRUE');
}
mine.close();
*/
//----------

function php_lookup_and_catch_click(link_ref_number, target, campaign_path)
{
	catch_click_lockout = false;
	//alert ("php_lookup_and_catch_click called: link_ref_number = " + link_ref_number + ", target = " + target + ", campaign_path = " + campaign_path);
	
	// set values into global vars:
	cc_URL 				= '';
	cc_link_ref_number 	= link_ref_number;
	cc_target 			= target;
	cc_campaign_path	= campaign_path;
	
	//------------
	// now we want to call the php lookup file - and as a result it will write a value into global var cc_URL:
	var script 		= document.createElement('script');
	script.type 	= 'text/javascript'; 
	//script.src 		= location.protocol + '//www.hostimages.com/sf/cm_root/get_click_routing.php?link_ref_number=' + link_ref_number;				// SANDBOX
	script.src 		= location.protocol + '//www.firstcommunicate.com/sf/cm_root/get_click_routing.php?link_ref_number=' + link_ref_number;		// PRODUCTION
	document.getElementsByTagName('head')[0].appendChild(script);
	//------------
}

function php_lookup_done()
{
	//alert ("php_lookup_done called: cc_link_ref_number = " + cc_link_ref_number + ", cc_URL = " + cc_URL + ", cc_target = " + cc_target + ", cc_campaign_path = " + cc_campaign_path);

	// grab from global vars:
	//catch_click(cc_link_ref_number, cc_URL, cc_target, cc_campaign_path);
	
	if ((cc_link_ref_number != null) && (cc_link_ref_number != '') && (cc_URL != null) && (cc_URL != '')) {
		catch_click(cc_link_ref_number, cc_URL, cc_target, cc_campaign_path);
	} else {
		alert ('link not found');
	}
}

function click_logging_done()
{
	//alert ("click_logging_done called: reply = " + reply);
	
	//------
	// This is for debugging purposes:
	if ((reply != true) && (reply != 'true')) {
		alert(reply);
	}
	//------
	
	//window.open(go_next_URL, go_next_target);
	
	if (catch_click_lockout == false) {
		catch_click_lockout = true;

		if (new_win != null) {
			//window.open(go_next_URL, go_next_target);
			new_win.location = go_next_URL;
		} else {
			window.location = go_next_URL;
		}
	} else {
		catch_click_lockout = false;
	}
}

function callAfterPause(arg)
{
	//alert ("callAfterPause called: arg = " + arg + ", reply = " + reply);
	//alert ("reply = " + reply);
	
	/*
	if ((reply != true) && (reply != 'true')) {
		alert(reply);
	}
	*/
	
	/*
	if ((go_next_target == null) || (go_next_target == '')) {
		go_next_target = '_self';
	}
	*/
	
	//window.open(go_next_URL, go_next_target);
	
	if (catch_click_lockout == false) {
		catch_click_lockout = true;
		
		if (new_win != null) {
			//window.open(go_next_URL, go_next_target);
			new_win.location = go_next_URL;
		} else {
			window.location = go_next_URL;
		}
	} else {
		catch_click_lockout = false;
	}
}

//function catch_click(URL, link_ref_number, target)
// link_ref_number is required, URL is optional - if not provided it will look up the URL for the link from SF
// target is not required - it will default to '_self', campaign_path is only required if the link is used on a 
// page where the user will not be on, or have gone through, a Landing Page. 
// campaign_path goes: initiative_id.program_id.campaign_id.campaign_segment_id - and if program is missing it goes: initiative_id.null.campaign_id.campaign_segment_id
function catch_click(link_ref_number, URL, target, campaign_path)
{
	catch_click_lockout = false;
	//alert ("catch_click called: link_ref_number = " + link_ref_number + ", URL = " + URL + ", target = " + target + ", campaign_path = " + campaign_path);
	
	//alert ("window.location.href = " + window.location.href);
	
	
	if ((URL == null) || (URL == '')) {
		php_lookup_and_catch_click(link_ref_number, target, campaign_path);
		return;
	}
	
	go_next_URL 	= URL;
	
	//go_next_target 	= target;
	
	new_win = null;
	
	if ((target == null) || (target == '')) {
		go_next_target = '_self';
	} else {
		go_next_target 	= target;
		
		if (target == '_blank') {
			new_win = window.open();
			
			if(new_win) {
				var popUpsBlocked = false;
				
				//alert ('popUpsBlocked is FALSE');
				
			} else {
				var popUpsBlocked = true;
				
				//alert ('popUpsBlocked is TRUE');
			}
		}
	}
	
	//------------
	reply = false;
	
	// now we want to call the php file that will log the click:
	var script 		= document.createElement('script');
	script.type 	= 'text/javascript'; 
	//script.src 		= location.protocol +'//www.hostimages.com/sf/cm_root/click_logger.php?link_ref_number=' + link_ref_number + '&page_url=' + escape(window.location.href) + '&campaign_path=' + campaign_path;			// SANDBOX
	script.src 		= location.protocol +'//www.firstcommunicate.com/sf/cm_root/click_logger.php?link_ref_number=' + link_ref_number + '&page_url=' + escape(window.location.href) + '&campaign_path=' + campaign_path;		// PRODUCTION
	//script.src 		= 'http://www.hostimages.com/sf/testing/click_tracking/test_alert.php';
	//script.src 		= "alert('dummy alert');";
	document.getElementsByTagName('head')[0].appendChild(script);
	//------------

	//------------
	/*
	// Alternative way to code this:
	var head = document.getElementsByTagName('head').item(0);
	var script = document.createElement('script');
	script.setAttribute( 'type', 'text/javascript' );
	script.setAttribute( 'src', 'remote.php?value=my message');
	head.insertBefore( script, head.firstChild );
	*/
	//------------
	
	//setTimeout("callAfterPause('monkey');", 3000);		// you can also pass args if required
	//setTimeout("callAfterPause();", 300);					// 300 milliseconds seems to work
	setTimeout("callAfterPause();", 500);					// This will only be necessary if click_logger.php fails or takes too long
}
