
$(function() {

  // target=_blank for all external/download links
  $('a').each(function() {
    var href = $(this).attr('href');
		if(href) {
			href = href.toLowerCase();
			if($(this).hasClass('popup')) {			  
			  $(this).click(function(event) {
			    popwin(href, 'popup1', 800, 500);			    
			    event.preventDefault();			    			  
			  });
			}			
			else if(href.indexOf('http://') == 0) {
			  $(this).attr('target', '_blank');
			}
			else if(href.indexOf('.pdf')>-1 || href.indexOf('.doc')>-1) {
				
				$(this).attr('target', '_blank');
				
				$(this).click(function(event){
				
				  if(!getcookie('confirmed')) {
						event.preventDefault();
						window._getInfoLink = $(this).attr('href');
						$('#jqmTerms').jqm({
							ajax:'/TermsModal.aspx', 
							modal:true
						}).jqmShow();
				  }
				});
				
			}
		}
  });
});

function hideTerms(result) {
	$('#jqmTerms').jqmHide();
	if(result == 1) {
		setcookie('confirmed', 1);
		window.open(window._getInfoLink);
	}
}

function popwin(url, name, w, h) {
  w += 32; h += 96;
  x = (screen.width - w) / 2;
  y = (screen.height - h) / 2;
  var w = window.open(url, name,'width='+w+',height='+h+','+'left='+x+',top='+y+','+'location=no,menubar=no,status=no, toolbar=no, scrollbars=yes, resizable=yes');
  if(w) w.focus();
}

function getcookie(name){
	var res = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
	if(res)
		return(unescape(res[2]));		
	return null;
}

function setcookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
	var cookie_string = name + "=" + escape(value);

	if(exp_y)
	{
		var expires = new Date(exp_y, exp_m, exp_d);
		cookie_string += "; expires=" + expires.toGMTString();
	}
	if(path)					
		cookie_string += "; path=" + escape(path);

	if(domain)
				cookie_string += "; domain=" + escape(domain);
	
	if(secure)
				cookie_string += "; secure";
	
	document.cookie = cookie_string;
}
