jQuery.fn.exists = function(){return jQuery(this).length>0;}

/* Fancybox popup functions
------------------------------------------------ */
jQuery(document).ready(function($) {
	$('.youtube-movies a').click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'title'			: this.title,
			'width'			: 630,
			'height'		: 495,
			'href'			: this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&autoplay=1',
			'type'			: 'swf',
			'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true',
				'autoplay'			: 'true'
			}
		});
		
		return false;
	});
	
	// Apply a Fancybox to all PDF links
	$('.pdf').click(function(e) {
		e.preventDefault();
		//var thisLink = $(this).attr('href');
		
		$.fancybox({
			'href'		: 'http://docs.google.com/gview?url=' + this.href + '&embedded=true',
			'type'		: 'iframe',
			'width'		: 800,
			'height'	: 600,
			'title'		: this.title + ' (<a href="' + this.href + '">Download</a>)',
			'autoScale' : true
		});
		
		return false;
	});
	
	/*  */
	$('.newsletter-signup-link').click(function(e) {
		e.preventDefault();
		
		$.fancybox({
			'href'		: 'http://www.turrentinebrokerage.com/monthly-newsletter-signup/?embed=true',
			'type'		: 'iframe',
			'width'		: 350,
			'height'	: 450,
			'title'		: this.title,
			'autoScale' : true
		});
	});
	
	
	/* Set the default values for Outlook form */
	$('#extracopies').attr('value', '0');
	$('#extracopiesextended').attr('value', '0.00');
	$('#total').attr('value', '1350.00');
	
});

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
}


function load_via_ajax(divname, filename) { 
    var myfile=filename; //file to get
    xmlhttp.open("GET",myfile,true); //open the pipe, get the file
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            var mydiv = document.getElementById(divname);
            mydiv.innerHTML = xmlhttp.responseText;
        } 
    }
    xmlhttp.send(null)
}

function jsOutlookSubscriptionCalcExtra() {
	var ExtraCopiesField=document.getElementById("extracopies");
	var ExtraCopiesExtendedField=document.getElementById("extracopiesextended");
	var TotalField=document.getElementById("total");
	var ECExtended=(ExtraCopiesField.value*620)
	ExtraCopiesExtendedField.value=ECExtended.toFixed(2);
	TotalField.value=(Number(ECExtended)+1350).toFixed(2);
}
function jsOutlookSubscriptionSubmit() {
	var SubscriptionForm=document.getElementById("outlookform");
	jsOutlookSubscriptionCalcExtra();
	SubscriptionForm.submit();
}

function CVRFormEventHandler(evt) {
        var YearSubscriptionBox=document.getElementById('yearsubscription');
        var AdditionalCopies=document.getElementById('additionalcopies');
        
         AdditionalCopiesValue=0;
        if (AdditionalCopies) {
        if (AdditionalCopies.value=='') {
             AdditionalCopiesValue=0;
        } else {
             AdditionalCopiesValue=AdditionalCopies.value;
        }
        }
        var SingleIssues=document.getElementById('singleissues');
        SingleIssuesValue=0;
        if (SingleIssues) {
        if (SingleIssues=='') {
             SingleIssuesValue=0;
        } else {
             SingleIssuesValue=SingleIssues.value;
        }
        }
        var Total=document.getElementById('total');
        var totalcalc=0;
        if (YearSubscriptionBox) {
	        if (YearSubscriptionBox.checked) {
	             totalcalc=totalcalc+950;
	             AdditionalCopies.disabled='';
	        } else {
	             AdditionalCopies.disabled='disabled';
	        }
        }
        
        totalcalc=totalcalc+Number((AdditionalCopiesValue*50));
        totalcalc=totalcalc+Number((SingleIssuesValue*250));
        
        if (Total && totalcalc != 0) {
	        Total.value=totalcalc;
        }
}
