// JavaScript Document
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

//Function that finds elements by the tag name and returns them in an array.
document.getElementsByClassName = function(cl) {
		var retnode = [];
		var myclass = new RegExp('\\b'+cl+'\\b');
		var elem = this.getElementsByTagName('*');
		for (var i = 0; i < elem.length; i++) {
			var classes = elem[i].className;
			if (myclass.test(classes)) retnode.push(elem[i]);
		}
		return retnode;
};


//Function makes an asynchronous call to write outbound link data to database.
function trackOutboundClick(HA_ID, ConvType) {

	if (encodeURIComponent) {
		var url = "/trackOutboundClick.aspx?HA_ID=" + encodeURIComponent(HA_ID) + "&ConvType=" + encodeURIComponent(ConvType);
		$.post(url,
		    function(data) {
		        $('body').append(data);
		    }
		);
	}
   
	return true;

}

//function adds an onClick event of a certain type to items within a certain id.
function addOutboundTracking(HA_ID, ConvType, ElementId){

	var LinkArea = document.getElementById(ElementId);

	if (lLinkArea) {

		var Links = LinkArea.getElementsByTagName('a');
	 
	     for(var i=0; i < Links.length; i++) { 
			    Links[i].onclick = function() {
			    return trackOutboundClick(HA_ID, ConvType);
		    }
	     }
	} 
}



function convertLinks(offer_url, query_params, element_id){
	var myURL = "";

	//determine if we need to a a "?" or a "&" to the base url.
	if(offer_url.indexOf('?',0) > 0)
	{
		myURL =offer_url+"&";
	} 
	else 
	{
		myURL = offer_url+"?";
	}
	
	myURL += query_params;

	//find all links within a certain id.
	var linkarea = document.getElementById(element_id);
	
	//if any exist...
	if(linkarea){
	
	var links = linkarea.getElementsByTagName('a');
	//go through and convert them.
	for(var i=0; i < links.length; i++) { 
			
		var linkHTML = links[i].innerHTML;//because of weird IE bug we need to get the link text first.
		links[i].href = myURL;
		links[i].innerHTML = linkHTML; //sets the link text back to what it was before converting the href.
		
	 }
	
	
	}
}

