function ccPopWindow(pgVars){
	var popUrl = pgVars.url;
	var windowName;
	var windowAttributes;
	if (pgVars.windowName=="" || pgVars.windowName=="undefined") windowName="cc_pop_window";
	else windowName=pgVars.windowName;
	if (pgVars.windowAttributes=="" || pgVars.windowAttributes=="undefined") windowAttributes="";
	else windowAttributes=pgVars.windowAttributes;
	var poppedWindow = window.open(popUrl,windowName,windowAttributes);
}

function addLoadEvent(func){
	try{ 
    	if(window.attachEvent){window.attachEvent("onload",func);} 
        if(window.addEventListener){window.addEventListener("load",func,false);} 
        return this; 
    }catch(e){} 
}

function injectFluxWidget(widgetName, options)
{
	if (typeof(Flux) != "undefined" && Flux.Utils.WidgetsLoader) {
		Flux.Utils.WidgetsLoader.createWidget(widgetName, options);
	}
}

function clearSearch(theText) {	
  	if (theText.value == theText.defaultValue) {
		theText.value = "";
	}
}

function blurSearch( theText ){
	if( theText.value == "" ){
		theText.value = theText.defaultValue;
	}
}

function submitSearch( form ){
	var field = $(form).find('input[type="text"]');
	var term = field.attr("value");
	var info = field[0].defaultValue;
	if( info == term ){
		alert( "Please enter a search term." );
		return false;
	}
	return true;
}

function loginsubmit(){
	document.flux_signin.ref.value=location.href;
	document.flux_signin.submit();
}


function newsletter_pop()
{
	var email_value = document.newsletter.email.value;
	var dob_value = document.newsletter.dob.value;
	var zip_value = document.newsletter.zip.value;
	$(".subscription_error").text("Sending request...");
	
	var rand = Math.floor(Math.random()*1111111111);
	var url = "/subscribe_user?email="+email_value+"&newsletter=colbert&dob="+dob_value+"&zip="+zip_value+"&r="+rand;
	$.post(url,"",subscribe_results,"text");
	return false;
	
	
	//var new_url = "http://www.comedycentral-q.mtvi.com/comedycentral/sitewide/droplets/third_party_newsletter_popup.jhtml"+"?newsletterId=1172823250&email=" + email_value + "&BGart=/comedycentral/sitewide/images/colbert_newsletter.jpg&dob=" + dob_value + "&zip=" + zip_value;
	//window.open(new_url,"top","menubar=no,width=520,height=320,toolbar=no");	
}
function subscribe_results(responseText, textStatus, XMLHttpRequest) {
	if (responseText.indexOf("OK") > -1){
		$(".subscription_error").text("You have been subscribed");
	}else if (responseText.indexOf("AGE") > -1) {
		$(".subscription_error").text("You are not eligible to sign up at this time")
	}else{
		$(".subscription_error").text("There was a problem, please try again");
	}
}


/**
 * Email validation script. 
 */
function invalid_email()
{	
	document.getElementById('email_message').style.display="visible";
	document.newsletter.email.focus();
	return false;
}


function validateEmail(whichForm, email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = email;
	if(reg.test(address) == false) {
		$(".email_error").show();
	}else{
		newsletter_pop(); 	
	}
	return false;	
}
function validateNewsletter(whichFrom, email, dob, zip) {
	$(".email_error").hide();
	$(".dob_error").hide();
	$(".zip_error").hide();
	$(".subscription_error").text("");
	var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var dobReg = /^([0-9]{2})+\/([0-9]{2})+\/([0-9]{2})$/;
	var zipReg = /^([0-9]{5})$/;
	var address = email;
	var dateofbirth = dob;
	var zipcode = zip;
	if (emailReg.test(address) == false) {
		$(".email_error").show();
	}
	else if (dobReg.test(dateofbirth) == false) {
		$(".dob_error").show();
	}
	else if (zipReg.test(zipcode) == false) {
		$(".zip_error").show();
	}
	else {
		newsletter_pop();
	}
	return false;
}


function displayMore() {
	$("#moreTagResults").hide();
	$("#lessTagResults").show();
	$("#tagDelimiter").text(",");
	$(".derivitiveTagsMore").css('display', 'inline');
}

function displayLess() {
	$("#moreTagResults").show();
	$("#lessTagResults").hide();
	$("#tagDelimiter").text("...");
	$(".derivitiveTagsMore").hide();
}
/*
function submitWeb() {
	document.searchFormTop.action="http://www.live.com";
	document.searchFormTop.target="_blank";

	var q_input = document.createElement('input');
	q_input.setAttribute("type", "hidden");
	q_input.setAttribute("name", "q");
	q_input.setAttribute("value", document.searchFormTop.keywords.value);
	document.searchFormTop.appendChild(q_input);
		
	if((document.searchFormTop.keywords.value == "")||(document.searchFormTop.keywords.value == "Search")){
		alert("Please enter your search term in the field to the left before pressing this button");
		document.searchFormTop.removeChild(q_input);
		return false;
	}

	else{
		document.searchFormTop.submit();
		document.searchFormTop.removeChild(q_input);
		return false;
	}
}

function submitSite() {
	
	document.searchFormTop.action="/video";
	document.searchFormTop.target="_self";

	if((document.searchFormTop.keywords.value == "")||(document.searchFormTop.keywords.value == "Search")){
		alert("Please enter your search term in the field to the left before pressing this button");
		return false;
	}

	else{
		document.searchFormTop.submit();
	}	
	
}
*/
function submitWeb(formType, mkt) {
	document.searchFormTop.action="http://www.live.com";
	document.searchFormTop.target="_blank";

	if((document.searchFormTop.searchterm.value == "")||(document.searchFormTop.searchterm.value == "Search")){
		alert("Please enter your search term in the field to the left before pressing this button");
		return false;
	}

	else{

		if(formType == null || formType == "") formType = 'VCM002';
		if(mkt == null || mkt == "") mkt = 'en-US';
		var q_input = document.createElement('input');
		q_input.setAttribute("type", "hidden");
		q_input.setAttribute("name", "q");
		q_input.setAttribute("value", document.searchFormTop.searchterm.value);
		document.searchFormTop.appendChild(q_input);

		var q_input1 = document.createElement('input');
		q_input1.setAttribute("type", "hidden");
		q_input1.setAttribute("name", "mkt");
		q_input1.setAttribute("value", mkt);
		document.searchFormTop.appendChild(q_input1);

		var q_input2 = document.createElement('input');
		q_input2.setAttribute("type", "hidden");
		q_input2.setAttribute("name", "FORM");
		q_input2.setAttribute("value", formType);
		document.searchFormTop.appendChild(q_input2);

		document.searchFormTop.submit();
		document.searchFormTop.removeChild(q_input);
		document.searchFormTop.removeChild(q_input1);
		document.searchFormTop.removeChild(q_input2);
		return false;
	}
}

function submitSite(action) {
	if (action == "" || action ==null) action = "/video";
	document.searchFormTop.action=action;
	document.searchFormTop.target="_self";

	if((document.searchFormTop.searchterm.value == "")||(document.searchFormTop.searchterm.value == "Search")){
		alert("Please enter your search term in the field to the left before pressing this button");
		return false;
	}

	else{
		var q_input = document.createElement('input');
		q_input.setAttribute("type", "hidden");
		q_input.setAttribute("name", "searchtype");
		q_input.setAttribute("value", "site");
		document.searchFormTop.appendChild(q_input);
		var q_input2 = document.createElement('input');
		q_input2.setAttribute("type", "hidden");
		q_input2.setAttribute("name", "keywords");
		q_input2.setAttribute("value", document.searchFormTop.searchterm.value);
		document.searchFormTop.appendChild(q_input2);
		document.searchFormTop.submit();
	}	
	
}


function getMOScript(){
	return "onmouseover='this.src=this.src.replace(\"up\",\"over\")' onmouseout='this.src=this.src.replace(\"over\",\"up\")'";
}