/*
	Base javascript code to be referenced from all pages
	Author: MFP
	Version: $Id: fcbasescript.js,v 1.1.1.1 2005/08/02 23:25:52 atg Exp $
*/

function popGeneral(url,width,height) {
	var generalpop = window.open(url,"_blank","width="+width+",height="+height+",menubar=no,location=no,resizable=no,scrollbars=no,status=no");
}

function openWin (winURL,winName,winParams) 
{
	var popWindow = open(winURL,winName,winParams);
	if (window.focus && popWindow!=null) 
	{
		popWindow.focus();
		if (popWindow.opener == null) popWindow.opener = self;
	}
}
function openParentAndChild(parentURL, childURL, childName, childParams, redirectSelf)
{
	if (redirectSelf=="true") document.location.href=parentURL;
	else open(parentURL);
	openWinRequired(childURL, childName, childParams);
}


function openWinRequired (winURL,winName,winParams) 
{
	var popWindowRequired = open(winURL,winName,winParams);
	if (isNullWindow(popWindowRequired)==true) {
		return;
	}
	else 
	{
		if (popWindowRequired.opener == null) popWindowRequired.opener = self;
		if (window.focus) popWindowRequired.focus();
	}
}

function closeCTPromo() 
{
	bannerId = document.getElementById('ctPromo');
	if (null!=bannerId)
	{
		bannerId.style.display="none";
	}
}

function cartPopupWin(url, width, height, techSupport) {
	  if (width == null)
	  	width = 510;
	  if (height == null)
	  	height = 500;
	  	
      winStats='height=' + height + ',width=' + width + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,directories=no,resizable=yes';
   if (navigator.appName.indexOf("Microsoft")>=0) {
      winStats+=',left=50,top=50'
      
    }else{
      winStats+=',screenX=50,screenY=50' 
    }
	//append referer to URL for livePerson
	if (url.indexOf("liveperson.net") != -1) {
		if (url.indexOf("?") != -1) {
			var urlParams = new Array();
			urlParams = url.split("?");
			var baseURL = urlParams[0];
			var params = urlParams[1];
			var newParams = "referrer="+escape(document.location);
			if (techSupport) {
				newParams += '&special='+escape('postChatURL:http://www.franklinplanner.com/Product_Support/m1_online_survey.html,postChatParms:scrollbars=1 width=800 height=800 resizable=1');
			}
			newParams += "&"+params;
			url = baseURL+"?"+newParams;
		}
		else url+="?referrer="+escape(document.location);
	}
    popWin=window.open(url,"FranklinCovey",winStats);
    popWin.focus();
}

function openQuote(sURL2){
	newwindow2=open(sURL2,"one2e","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=no,status=no,width=411,height=175");
	if (window.focus) {
		newwindow2.focus();
	}
}

function jumpCategory(formname, selectname) {
	/*
	Given a formname and a select element name this script assumes that the 
	select element contains options whos values are urls and directs accordingly
	*/
	var selectObj = document.forms[formname].elements[selectname];
	var which = selectObj[selectObj.selectedIndex].value;
	if (which != "") document.location.href=which;
}

function noCookies(newPage){
		alert("Cookies are required to use the FranklinCovey Shopping web site.");
		//document.location = '/shopping/nocookies.html';
		//document.location = 'http://www.franklinplanner.com/';
		document.location = newPage;
}

function testCookies(newPage){
	if(document.cookie){
		var cookieoff = "ATG=";
		document.cookie = cookieoff;
		//alert("Stall");
		var cookieval = "ATG=true";
		document.cookie = cookieval;
		if(document.cookie.indexOf(cookieval) == -1)
			noCookies(newPage);
	} else {
			noCookies(newPage);
	}
}

function isNullWindow(win) {
//	if(win == null || win.focus == null)
	if(win == null)
	{
		var text = "This site requires pop-ups to be enabled.\n";
		text += "If you would like to view this content, please disable\n";
		text += "any pop-up blocking software and restart your browser.\n\n";
		alert(text);
		return true;
	}
	return false;
}

//////script used for element positioning of "new" and "limited qty" images
// overly simplistic test for IE
isIE = (document.all ? true : false);
// both IE5 and NS6 are DOM-compliant (well, sort of...)
isDOM = (document.getElementById ? true : false);

// get the true offset of anything on NS4, IE4/5 & NS6, even if it's in a table!
function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }
function getAbsPos(elt,which) {
 iPos = 0;
 while (elt != null) {
  iPos += elt["offset" + which];
  elt = elt.offsetParent;
 }
 return iPos;
}

function getDivStyle(divname) {
 var style;
 if (isDOM) { style = document.getElementById(divname).style; }
 else { style = isIE ? document.all[divname].style
                     : document.layers[divname]; } // NS4
 return style;
}

function hideElement(divname) {
 getDivStyle(divname).visibility = 'hidden';
}

// annoying detail: IE and NS6 store elt.top and elt.left as strings.
function moveBy(elt,deltaX,deltaY) {
 elt.left = parseInt(elt.left) + deltaX;
 elt.top = parseInt(elt.top) + deltaY;
}

function toggleVisible(divname, belowPos) {
 divstyle = getDivStyle(divname);
 if (divstyle.visibility == 'visible' || divstyle.visibility == 'show') {
   divstyle.visibility = 'hidden';
 } else {
   fixPosition(divname, belowPos);
   divstyle.visibility = 'visible';
 }
}

function setPosition(elt,positionername,isPlacedUnder, howFarBelow) {
 var positioner;
 if (isIE) {
  positioner = document.all[positionername];
 } else {
  if (isDOM) {
    positioner = document.getElementById(positionername);
  } else {
    // not IE, not DOM (probably NS4)
    // if the positioner is inside a netscape4 layer this will *not* find it.
    positioner = document.images[positionername];
  }
 }
 elt.left = getAbsX(positioner)+4;
 elt.top = getAbsY(positioner) + (isPlacedUnder ? positioner.height : howFarBelow);
}

function fixPosition(divname, belowPos) {
 divstyle = getDivStyle(divname);

 positionerImgName = divname + 'Pos';
 isPlacedUnder = false;
 if (!isPlacedUnder) {
  setPosition(divstyle,positionerImgName,false,belowPos);
 } else {
  setPosition(divstyle,positionerImgName,true,belowPos)
 }
}

// fixPositions() puts everything back in the right place after a resize.
function fixPositions()
{
 // add a fixPosition call here for every element
 // you think might get stranded in a resize/reflow.
// fixPosition('mookie');
}
function validateForm() {
	// init for required fields
	var reqFields = new Array();
	reqFields[0] = Array('PRIMARY_EMAIL_ADDR', 'E-mail');
	// clear errors
	var errFields = new Array();
	
	// non-empty test	
	for(i in reqFields)
		if (_mktf[reqFields[i][0]].value == '')
			errFields.push(reqFields[i]);
			
	// report errors and stop		
	if (errFields.length > 0)
		return alertFields(errFields);
	
	// clear errors for conditional testing
	errFields = new Array();
	
	// conditional testing
	if (_mktf['PRIMARY_EMAIL_ADDR'].value.indexOf('@') == -1)
		errFields.push(Array('E-mail', 'must be a valid e-mail address'));
	
	// report errors and stop
	if (errFields.length > 0)
		return alertErrors(errFields);
		
	_mktf.submit()
}

function alertFields(fields) {
	var msg = 'The following fields must be filled in.\n';
	
	for (n in fields)
   		msg += '\n\t' + fields[n][1];
	
	alert(msg);
}

function alertErrors(fields) {
	var msg = 'Please modify the following information.\n';
	
	for (n in fields)
		msg += '\n' + fields[n][0] + ": " + fields[n][1];

	alert(msg)
}

function loadAd() {
 		
    // Get a random array item
    var allAds = popAds.ads;
    var validAds = [];
    var today = new Date();
    var imgWidth = '280px';
    var closeImgColor = 'black';
    var closeImgStyle = "padding:5px 5px 0 0";
    
    // First create an array of the existing pop lists and include only those that 
    // are within defined start/stop dates
    var vid = 0;
    for (i=0;i<allAds.length;i++) {
    	show = true;
    	// Is there a start date and has it passed?
    	if ( allAds[i].start ) {
    	  var darr = [];
    	  darr = allAds[i].start.split('-');
    	  var startDate = new Date(darr[2],darr[0] - 1, darr[1],00,00,00);
    	  if ( today.getTime() < startDate.getTime() ) {
    	   	show = false;
    	  }
    	}
    	if ( show && allAds[i].end ) {
    	   var darr = [];
    	   darr = allAds[i].end.split('-');
    	   var endDate = new Date(darr[2],darr[0] - 1, darr[1],23,59,59);
    	   if ( today.getTime() > endDate.getTime() ) {
    	     show = false;
    	   }
    	}
    	if ( show ) {
    	   validAds[vid] = allAds[i];
    	   vid++;
    	}
    }
    if ( validAds.length > 0 ) {  // Pop a random ad
        var adDiv = document.getElementById('popAd');   
    	var id = Math.floor(Math.random() * validAds.length);
    	if ( validAds[id].closeImageColor ) {
    	  closeImgColor = validAds[id].closeImageColor;
    	}
    	var closeImg = "http://www.franklinplanner.com/images/popovers/close/" + closeImgColor + ".png";
    	if ( validAds[id].closeImageStyle ) {
    	  closeImgStyle = validAds[id].closeImageStyle;
    	}
    	if ( validAds[id].imgWidth ) {
    	  imgWidth = validAds[id].imgWidth;
    	}
    	
    	var imgSrc = "";
    	doHTML = false;
    	if ( validAds[id].html ) {
    	  doHTML = true;
    	  theSrc = validAds[id].html;
    	} else {
    	  theSrc = validAds[id].image;
    	}
    	
    	
    	var link = validAds[id].link;
    	
    	var posTop  = '140';
        var posLeft = 0;
        if ( $(window).width() > 1140 ) {     
          posLeft = ($('#fclogo').position().left - 135);
        }
    	
    	// Set up the dom stuff for the ad.
    	// Create the table container
    	var tbl = document.createElement('table');
    	$(tbl).attr('cellpadding','0');
    	$(tbl).attr('cellspacing','0');
    	
    	var tbdy = document.createElement('tbody');
    	var trTop = document.createElement('tr');
    	var tdTopLeft = document.createElement('td');
    	var tdTop = document.createElement('td');
    	var tdTopRight = document.createElement('td');
    	var trMid = document.createElement('tr');
    	var tdMidLeft  = document.createElement('td');
    	var tdMid      = document.createElement('td');
    	$(tdMid).css({'background-color':'#ffffff'});
    	
        if ( doHTML ) {
          $(tdMid).html('<div><div style="width:' + imgWidth + ';position:absolute;text-align:right;">' +
                '<div style="' + closeImgStyle + '">' +
    			'<img class="aPng" src="' + closeImg + '" style="cursor:pointer;vertical-align:middle;border:none;" onclick="$(\'#popAd\').hide();adPopped=false;"/>' +
    			'</div></div>' + theSrc + '</div>');
        } else {
    	  $(tdMid).html('<div><div style="width:' + imgWidth + ';position:absolute;text-align:right;">' +
    			'<div style="' + closeImgStyle + '">' +
    			'<img class="aPng" src="' + closeImg + '" style="cursor:pointer;vertical-align:middle;border:none;" onclick="$(\'#popAd\').hide();adPopped=false;"/>' +
    			'</div></div>' +
    			'<a href="' + link + '"><img src="' + theSrc + '" style="border:none;display:block;"/></a></div>');
        }
    
    	var tdMidRight = document.createElement('td');
    	var trBtm = document.createElement('tr');
    	var tdBtmLeft = document.createElement('td');
    	var tdBtm = document.createElement('td');
    	var tdBtmRight = document.createElement('td');
    	var imgTopRight = document.createElement('img');
    	var imgTopLeft = document.createElement('img');
    	var imgTopRight = document.createElement('img');
    	var imgBtmLeft = document.createElement('img');
    	var imgBtmRight = document.createElement('img');
    	    	
    	$(imgTopLeft).attr('class','aPng');
    	$(imgTopLeft).css({'height':'15px','width':'15px'}); 
    	imgTopLeft.src='http://www.franklinplanner.com/images/popovers/shdw/tleft.png';
    	   
    	
    	tdTopLeft.appendChild(imgTopLeft);
    	
    	$(tdTop).attr('class','aPng');
    	$(tdTop).css({'height':'15px','background-image':'url(\'http://www.franklinplanner.com/images/popovers/shdw/top.png\')','background-repeat':'repeat-x'});
    	//$(tdTop).html('&nbsp;');
    	
    	$(imgTopRight).attr('class','aPng');
    	imgTopRight.src='http://www.franklinplanner.com/images/popovers/shdw/tright.png';
    	$(imgTopRight).css({'height':'15px','width':'15px'});   
    	tdTopRight.appendChild(imgTopRight);
    	
    	// Set up the drop shadow images Middle
    	$(tdMidLeft).attr('class','aPng');
    	$(tdMidRight).attr('class','aPng');
    	$(tdMidLeft).css({'width':'15px','background-image':'url(\'http://www.franklinplanner.com/images/popovers/shdw/left.png\')','background-repeat':'repeat-y'});
    	$(tdMidRight).css({'width':'15px','background-image':'url(\'http://www.franklinplanner.com/images/popovers/shdw/right.png\')','background-repeat':'repeat-y'});
    	//$(tdMidLeft).html('&nbsp;');
    	//$(tdMidRight).html('&nbsp;');
    	
    	// Set up the drop shadow images Bottom
    	
    	$(imgBtmLeft).attr('class','aPng');
    	
    	imgBtmLeft.src='http://www.franklinplanner.com/images/popovers/shdw/bleft.png';
    	$(imgBtmLeft).css({'height':'15px','width':'15px'});
    	tdBtmLeft.appendChild(imgBtmLeft);
    	$(tdBtm).css({'height':'15px','background-image':'url(\'http://www.franklinplanner.com/images/popovers/shdw/bottom.png\')','background-repeat':'repeat-x'});
    	$(tdBtm).attr('class','aPng');
    	
    	//$(tdBtm).html('&nbsp;');
    	
    	$(imgBtmRight).attr('class','aPng');
    	imgBtmRight.src='http://www.franklinplanner.com/images/popovers/shdw/bright.png';
    	$(imgBtmRight).css({'height':'15px','width':'15px'});   
    	tdBtmRight.appendChild(imgBtmRight);
    	  	
    	trTop.appendChild(tdTopLeft);
    	trTop.appendChild(tdTop);
    	trTop.appendChild(tdTopRight);
    	
    	trMid.appendChild(tdMidLeft);
    	trMid.appendChild(tdMid);
    	trMid.appendChild(tdMidRight);
    	
    	trBtm.appendChild(tdBtmLeft);
    	trBtm.appendChild(tdBtm);
    	trBtm.appendChild(tdBtmRight);
    	
    	tbdy.appendChild(trTop);
    	tbdy.appendChild(trMid);
    	tbdy.appendChild(trBtm);
    	tbl.appendChild(tbdy);
    	adDiv.appendChild(tbl);
    	
    	$(adDiv).css({'top':posTop + 'px','left':posLeft + 'px','z-index':100});
    	adPopped = true;
    	if ( jQuery.browser.msie ) {
    	  $(adDiv).show();  
    	} else {
    	  $(adDiv).fadeIn('slow');  
    	}  
    }
}

function placePopAd() {
  if ( adPopped ) {
     var wi = $(window).width();
     var posLeft = 0;
     if ( $(window).width() > 1140 ) {     
       posLeft = ($('#fclogo').position().left - 135);
     }
     $('#popAd').css({'left':posLeft + 'px'});
  }
}
// correctly handle PNG transparency in Win IE 5.5 & 6
function checkPNG() {

	 if ( jQuery.browser.msie && jQuery.browser.version < 7 && document.body.filters ) {
	   for(var i=0; i<document.images.length; i++) {
	     var img = document.images[i];
	     var imgName = img.src.toUpperCase();
	         
	     if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
	        //alert(imgName);
	        var imgID = (img.id) ? "id='" + img.id + "' " : "";
	        var imgClass = (img.className) ? "class='" + img.className + "' " : "";
	        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
	        var imgStyle = "display:inline-block;" + img.style.cssText; 
	        if (img.align == "left") imgStyle = "float:left;" + imgStyle;
	        if (img.align == "right") imgStyle = "float:right;" + imgStyle;
	        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
	        var strNewHTML = "<span " + imgID + imgClass + imgTitle
	        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	        + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" ;
	        img.outerHTML = strNewHTML

	        i = i-1
	      }
	   }
	}    
}
$(document).ready( function() {
  checkPNG();
}); 

/* Functions related to Freddie and Ma */
function setBorder(obj,onoff,dValue) {
	isFM = true;
	if ( onoff == 1 ) {
		obj.style.border = "3px solid #959a00";
	} else {
	  if ( selectedPattern != obj.id ) {
		obj.style.border = "3px solid #ffffff";
	  }
	}  
}
function setDriverValue(pattern_id,dValue) {
  isFM = true;
  if ( selectedPattern != "" ) {
	$('#' + selectedPattern).css('border','3px solid #ffffff');
  }
  $('#' + pattern_id).css('border','3px solid #959a00');
  document.SkuSelector.driverValue.value = dValue;
  selectedPattern = pattern_id;
}
function setQtyPos() {
  $('#product-quantity').css('top',$('#product-primary-driver').height());
  $('#product-quantity').css('left','-130px');
}

