var dom1=(document.getElementById)?1:0;
var ie4plus=(document.all)?1:0;
var ns4=(document.layers)?1:0;
var safari=(navigator.appVersion.indexOf("Safari") != -1)?1:0;
var mac=(navigator.appVersion.indexOf("Macintosh") != -1)?1:0;
var lowerUserAgent=navigator.userAgent.toLowerCase();
var opera = (lowerUserAgent.indexOf('opera')!=-1)?true:false;

/*
if (! ("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
                 , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
}
*/

function validateCatalogRequest(){
	var f=document.requestCats;
	var errors=[];
	var errorFields=[];
    $("input:text","#requestCats").each(function(){if(this.value == this.title)this.value="";});	
	if(f.catreqfirstname.value==""){
		errors[errors.length]="You didnt provide a First Name";
		errorFields[errorFields.length]=f.catreqfirstname;		
	}
	if(f.catreqlastname.value==""){
		errors[errors.length]="You didnt provide a Last Name";
		errorFields[errorFields.length]=f.catreqlastname;		
	}
	if(f.catreqhomeaddress1.value==""){
		errors[errors.length]="You didnt provide a Street Address";
		errorFields[errorFields.length]=f.catreqhomeaddress1;		
	}
	if(f.catreqhomecity.value==""){
		errors[errors.length]="You didnt provide a City";
		errorFields[errorFields.length]=f.catreqhomecity;
	}
	if(f.catreqhomezip.value==""){
		errors[errors.length]="You didnt provide a Zip";
		errorFields[errorFields.length]=f.catreqhomezip;
	}
	if(f.catreqhomestate.options[f.catreqhomestate.options.selectedIndex].value==""){
		errors[errors.length]="You didnt select a State or Province";
		errorFields[errorFields.length]=f.catreqhomestate;
	}
	if(f.catreqsemail.value==""){
		//errors[errors.length]="You didnt provide an Email";
		//errorFields[errorFields.length]=f.catreqsemail;
	}else{
		var temp = f.catreqsemail.value.replace(/\s/g, "");
		if (!(temp.match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)) && temp.charAt(0) != "." && !(temp.match(/\.\./))){
			errors[errors.length]="You didnt provide a valid Email.  Email is Optional.";
			errorFields[errorFields.length]=f.catreqsemail;
		}
	}
	if(errors.length==0){
		$.ajax({
		  type: "GET",
		  url: f.action,
		  data: $("#requestCats").serialize(),
		  success: function(msg){
			$("#requestcatalog").hide();
		  	if(msg.indexOf("error")!=-1){				
				$("#requestcatalogResponse").hide();
				$("#requestcatalogResponse").html(msg);
				$("#requestcatalogResponse").fadeIn("slow").animate({opacity:1.0},5000).fadeOut("slow",function(){$("#requestcatalog").show()});
			}else{
			    //alert( "Data Returned: " + msg );
				//$("#requestcatalog").fadeOut("fast",function(){$("#requestcatalogResponse").html(msg).fadeIn("slow",function(){prepareFormHelpers()})});
				$("#requestcatalogResponse").hide();
				$("#requestcatalogResponse").html(msg);
				$("#requestcatalogResponse").fadeIn("slow",function(){document.requestCats.reset();prepareFormHelpers()}).animate({opacity:1.0},5000).fadeOut("slow",function(){$("#requestcatalog").show()});


			}
		  }
		});	
	}else{
		var string1="Sorry, there was a problem:\n"
		var string2=errors.join("\n");
		var string3=string1+string2;
		alert(string3);
		prepareFormHelpers();
	}
	return false;
	//$("#requestCats").serialize();
}

function printPage(){
	if(window.print) window.print();
	else{
		var string1="I could not print the page automatically in your browser.\nTo print the page, after pressing OK,\n";
		var string2=(mac)?"Press Command + P":"Press Ctrl + P";
		var helpfuleInstrStr=string1+string2;
		alert(helpfuleInstrStr);
	}
}
function printFrame(){
	if(window.print) noticeframe.print();
	else{
		var string1="I could not print the page automatically in your browser.\nTo print the page, after pressing OK,\n";
		var string2=(mac)?"Press Command + P":"Press Ctrl + P";
		var helpfuleInstrStr=string1+string2;
		alert(helpfuleInstrStr);
		top.location.href=window.noticeframe.location.href;
	}
}

function prepareFormHelpers(){
    $("input:text, textarea, select","#requestCats").each(function(){
        if(this.value == ''){
			//this.className +=" inactive";
			if(this.style)this.style.color="#999999";
			if(this.style && this.type != 'select-one') this.style.paddingLeft="6px";
			if(this.type != 'select-one') this.value = this.title;
		}
    });
    $("input:text, textarea, select","#requestCats").focus(function(){
		if(this.type != 'select-one') {
			if(this.value == this.title){
				this.value = '';
				//var  storedClass=this.className;
				//var aClassParts=storedClass.split("inactive");
				//this.className=aClassParts[0];
				if(this.style){
					this.style.color="#000000";
					this.style.paddingLeft="0px";
				}
			}
		}else{
				//var storedClass=this.className;
				//var aClassParts=storedClass.split("inactive");
				//this.className=aClassParts[0];
				if(this.style){
					this.style.color="#000000";
				}
		}
    });
    $("input:text, textarea","#requestCats").blur(function(){
		if(this.type != 'select-one') {
			if(this.value == ''){
				//this.className+=" inactive";
				if(this.style){
					this.style.color="#999999";				
					this.style.paddingLeft="6px";
				}
				this.value = this.title;
			}
		}else{
			if(this.options[this.options.selectedIndex].value == ""){
				//this.className+=" inactive";
				if(this.style)this.style.color="#999999";
			}
		}
    });
}

if(document.images){
	var pbuttover=new Image();pbuttover.src="/img/butt_promomore_over.gif";
	var pbuttoff=new Image();pbuttoff.src="/img/butt_promomore_off.gif";
	var arrowbuttover=new Image();arrowbuttover.src="/img/butt_reddoublearrows_over.gif";
	var arrowbuttoff=new Image();arrowbuttoff.src="/img/butt_reddoublearrows_off.gif";
	var gobuttover=new Image();gobuttover.src="/img/butt_go_over.gif";
	var gobuttoff=new Image();gobuttoff.src="/img/butt_go_off.gif";
}
function pact(imgN){if (document.images) document[imgN].src=pbuttover.src;}
function pinact(imgN){if (document.images) document[imgN].src=pbuttoff.src;}
function gact(imgN){if (document.images) document[imgN].src=gobuttover.src;}
function ginact(imgN){if (document.images) document[imgN].src=gobuttoff.src;}
function act(imgN){	if (document.images) document[imgN].src=eval(imgN+'over.src');}
function inact(imgN){if (document.images) document[imgN].src=eval(imgN+'off.src');}

function isUndefined(v) {
    var undef;
    return v===undef;
}


function generalOpenWin(href,winName,winFeatures){
	if (winFeatures=='tclFeatures'){winFeatures=(safari)?'width=540,height=435,status=0':'width=540,height=417,status=0'};
	if (winFeatures=='default'){winFeatures='width=540,height=435,status=0'};
	if (winFeatures=='defaultScroll'){winFeatures='width=630,height=490,status=1,scrollbars=1,resizable=1'};
	if (winFeatures=='defaultScrollWide'){winFeatures='width=760,height=490,status=1,scrollbars=1,resizable=1'};
	if (winFeatures=='offSite' || winFeatures=='fullFeatured'){winFeatures='width=720,height=560,location=1,status=1,scrollbars=1,resizable=1,toolbar=1,menubar=1'}
	if (winFeatures=='general'){winFeatures=(mac)?'width=382,height=375,scrollbars':'width=401,height=375,scrollbars'};
	if (winFeatures=='siteWin'){winFeatures='width=640,height=480,scrollbars,resizable,status'};
	if (winFeatures=='hddvd'){winFeatures='width=900,height=600,location=0,scrollbars=0,toolbar=0,menubar=0,status=0,resizable=1'};
	if (winFeatures=='fullFeaturedWide'){winFeatures='width=890,height=600,status=1,scrollbars=1,resizable=1'};
	
	if (document.all||document.getElementById||document.layers){
		var swidth=screen.width
		var sheight=screen.height
		if(swidth<=800){
			winFeatures+=",top=0,left=0";
		}else{
			//assuming width=760,height=550
			var tleft=(swidth/2)-380;
			var ttop=(sheight/2)-275;
			if(tleft<0)tleft=0;
			if(ttop<0)ttop=0;
			var tstr=",top="+ttop+",left="+tleft;
			winFeatures+=tstr;
		}
	}	
	var winName=window.open(href,winName,winFeatures)
	if (window.focus)winName.focus();
}


var defaultpopupfeatures = 'width=540,height=435,status=0';
function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = defaultpopupfeatures;
	else if (features=='default')features='width=540,height=435,status=0';
	else if (features=='defaultScroll')features='width=630,height=490,status,scrollbars,resizable';
	else if (features=='offSite' || features=='fullFeatured')winFeatures='width=720,height=560,location,status,scrollbars,resizable,toolbar,menubar'
	else if (features=='general')features=(mac)?'width=382,height=375,scrollbars':'width=401,height=375,scrollbars';
	else if (features=='siteWin')features='width=640,height=480,scrollbars,resizable,status';	
	//setting location onscreen
	if (ie4plus||dom1||ns4){
		var swidth=screen.width;
		var sheight=screen.height;
		if(swidth<=800){
			features+=",top=0,left=0";
		}else{
			//centered assuming width=760,height=550
			var tleft=(swidth/2)-380;
			var ttop=(sheight/2)-275;
			if(tleft<0)tleft=0;	if(ttop<0)ttop=0;
			var tstr=",top="+ttop+",left="+tleft;
			features+=tstr;
		}
	}	
    if (isUndefined(target)) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    //return theWindow;
}
function cleanOpenWin2(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    //return 
	raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}


function cleanOpenWin(oAnchor,sWindow,sProps){
	var sUrl = '';
	if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
	if(sUrl=='' && ie4plus) sUrl = window.event.srcElement.getAttribute('href');
	if(sUrl=='') sUrl = oAnchor.href;
	var sWindowName = sWindow?sWindow:'_blank';

	//basic features: default if unspecified, predefined, or instance
	if(!sProps) winFeatures = 'width=540,height=435,status=0';
	else if (sProps=='default')winFeatures='width=540,height=435,status=0';
	else if (sProps=='defaultScroll')winFeatures='width=630,height=490,status,scrollbars,resizable';
	else if (sProps=='offSite' || winFeatures=='fullFeatured')winFeatures='width=720,height=560,location,status,scrollbars,resizable,toolbar,menubar';
	else if (sProps=='general')winFeatures=(mac)?'width=382,height=375,scrollbars':'width=401,height=375,scrollbars';
	else if (sProps=='siteWin')winFeatures='width=640,height=480,scrollbars,resizable,status';
	else winFeatures=sProps;
	
	//setting location onscreen
	if (ie4plus||dom1||ns4){
		var swidth=screen.width
		var sheight=screen.height
		if(swidth<=800){
			winFeatures+=",top=0,left=0";
		}else{
			//centered assuming width=760,height=550
			var tleft=(swidth/2)-380;
			var ttop=(sheight/2)-275;
			if(tleft<0)tleft=0;
			if(ttop<0)ttop=0;
			var tstr=",top="+ttop+",left="+tleft;
			winFeatures+=tstr;
		}
	}		
	
	if(sUrl) var oPopup = window.open(sUrl,sWindowName,winFeatures);
	if(oPopup && !isOp) oPopup.focus();// An Opera bug returns too early if you focus the window, so we don't focus it in that browser.  Only a noticable defect (in that browser) if a window is already open and hidden behind another window.
	alert('oPopup='+oPopup);
	return (oPopup)?false:true;	// If popup was created successfully, cancel link in calling window. Acts as regular link in browser that has popup blocking enabled.
}


function hideThumbnail(){
	var thumbnailDiv=getElementRef('thumbdiv');
	if(thumbnailDiv && thumbnailDiv.style)thumbnailDiv.style.display='none';
}
function showThumbnail(oLink,thumbsrc,model){
	if( oLink.offsetParent ) {
		for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
			posX += oLink.offsetLeft; posY += oLink.offsetTop;
		}
	} else {
			posX= oLink.x; 	posY=oLink.y;
	}
	//alert(posX + ',' + posY)
	var thumbnailDiv=getElementRef('thumbdiv');
	//var thumbnailTxt=getElementRef('thumbnaildivdescription');
	if(thumbnailDiv && thumbnailDiv.style){
		document["thumbdivimg"].src=thumbsrc;
		document["thumbdivimg"].alt=model;
		thumbnailDiv.style.left=115;
		thumbnailDiv.style.top=posY-10;
		thumbnailDiv.style.display='block';	
		//thumbnailTxt.innerHTML=model;		
	}
}


function getElementRef(id){
	if (dom1) return document.getElementById(id);
	else if (ie4plus) return document.all[id];
	else if (ns4) return document.layers[id];
	else return null;
}




/* functions for controlling the left-side panels */


var cookieName='collapsedPanels';
var collapsedPanelsList = init_array();
var daysToKeep = 365; // 

var expires = new Date();
expires.setTime(expires.getTime()+(daysToKeep*24*60*60*1000));

function initPanelSettings(){
	get_array(cookieName, collapsedPanelsList);//updates array from cookie
	//numPanelsToHide=collapsedPanelsList.length;
	
	collapsedPanelsListstr=collapsedPanelsList.join("\n");
	//alert('collapsedPanelsList='+collapsedPanelsListstr)
	//alert(collapsedPanelsList.length)
	//if(collapsedPanelsList.length>0){
		//if(collapsedPanelsList[0]==null)alert(collapsedPanelsList[0])
		/*
		if(collapsedPanelsList.length>0){
			for (i=0; i<collapsedPanelsList.length; i++) {
				//remove null values?
				//alert(collapsedPanelsList[i])
				if(collapsedPanelsList[i]==null || collapsedPanelsList[i]=="null") {
					var x=i;
					//alert('trying to del collapsedPanelsList['+i+']');
					del_entry(cookieName, collapsedPanelsList, xs, expires)
				}
			}	
		}
		*/
		for (i=0; i<collapsedPanelsList.length; i++) {
			togglePane(collapsedPanelsList[i],true)//closes panel without re-adding to array (second param)
		}

}
/*
function isPanelDisplayed(id){
	var theObj=getElementRef(id);
	if(theObj==null)return null;
	if (theObj.style){
		if(theObj.style.display=='block') return true;
		else return false;
	}else if (theObj.display){
		if(theObj.display)=='block') return true;
		else return false;
	}else{
		return null;
	}
}
*/
function openPane(id){
	var theObj=getElementRef(id);
	if(theObj==null)return;
	if(theObj.style)theObj.style.display='block';
	else if(theObj.display)theObj.display='block';
}
function closePane(id){
	var theObj=getElementRef(id);
	if(theObj==null)return;
	if(theObj.style)theObj.style.display='none';
	else if(theObj.display)theObj.display='none';
}

function togglePane(id,nonewcookieentry){
	//alert('togglePane')
	var theObj=getElementRef(id);
	if(theObj==null)return;//why try if we know the browser cant
	//get_array(cookieName, collapsedPanelsList);//update array from cookie
	
	if (theObj) {
		if (theObj.style){//browsers that support the style object
			if (theObj.style.display=='block') {
				//hiding panel
				//alert('hiding '+id)
				theObj.style.display='none';
				convertToShow(id);
				if(nonewcookieentry!=true){
					//add panel to cookie
					
					//attempting to see if this is already in the array
					var existsinarray=false;
					for (var j=0; j<collapsedPanelsList.length; j++) {
						if(id==collapsedPanelsList[j]) existsinarray=true;
					}
					
					if (!existsinarray){
						collapsedPanelsList[collapsedPanelsList.length]=id;
						set_array(cookieName, collapsedPanelsList, expires);
					}
				}
			}
			else {
				//showing panel
				//alert('showing '+id)
				theObj.style.display='block';
				convertToHide(id)
				//removing panel from cookie
				for (var j=0; j<collapsedPanelsList.length; j++) {
					if(collapsedPanelsList[j]==id){
						var pos = j; // assign the number of the value within the cookie that is to be deleted
						del_entry(cookieName, collapsedPanelsList, pos, expires);//
					}
				}
			}
		} else {//browsers that DONOT support the style object
			if (theObj.display=='block') {
				//hiding panel
				theObj.display='none';
				convertToShow(id);
				if(nonewcookieentry!=true){
					//add panel to cookie
					collapsedPanelsList[collapsedPanelsList.length]=id;
					set_array(cookieName, collapsedPanelsList, expires);
				}
			}
			else {
				//showing panel
				theObj.display='block';
				convertToHide(id);
				//removing panel from cookie
				for (var j=0; j<collapsedPanelsList.length; j++) {
					if(collapsedPanelsList[j]==id){
						var pos = j; // assign the number of the value within the cookie that is to be deleted
						del_entry(cookieName, collapsedPanelsList, pos, expires);//
					}
				}
			}
		}
	}
}

var hidebutt=new Image();hidebutt.src='/img/butt_red_hide.gif';
var showbutt=new Image();showbutt.src='/img/butt_red_show.gif';
var hidenewsbutt=new Image();hidenewsbutt.src='/img/butt_greybar_hide.gif';
var shownewsbutt=new Image();shownewsbutt.src='/img/butt_greybar_show.gif';

function convertToHide(n){
	if(document.images){
		imgname='showhide'+n;
		thisbutt=document[imgname];
		thisbutt.src=(n.indexOf('news')==-1)?hidebutt.src:hidenewsbutt.src;
		thisbutt.alt='Click to hide';
	}
}
function convertToShow(n){
	if(document.images){
		imgname='showhide'+n;
		thisbutt=document[imgname];
		thisbutt.src=(n.indexOf('news')==-1)?showbutt.src:shownewsbutt.src;
		thisbutt.alt='Click to show';
	}
}


function bookmarkThis(url,txt){
	//if(ie4plus) window.external.AddFavorite(url,txt);
	if(window.external && window.external.AddFavorite)window.external.AddFavorite(url,unescape(txt));
	else {
		if(mac){
			alert("You must press Apple-D to bookmark this page.")
		}else{
			alert("You must press CTRL-D to bookmark this page.")
		}
	}
}



var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function flashholder_DoFSCommand(command, args) {
  var flashholderObj = InternetExplorer ? flashholder : document.flashholder;
  if(command=="SetWindow") window.status=args;
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub flashholder_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call flashholder_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}


function statusMessage(txt){
	window.status=txt;
}

var regzaWin;
var regzaUrl='/regza/regzasite.aspx';
function openRegzaSite(){
	var w;
	var h;
	var scrollbars;
	if(screen.width && screen.height){
		w=screen.width;
		h=screen.height;
		scrollbars=(screen.width < 940 || screen.height<768)?1:0;
	}else{
		w=1024;
		h=768;
		scrollbars=1;
	}
	var windowFeatures="top=1,left=1,width="+w+",height="+h+",toolbar=0,menubar=0,location=0,statusbar=0,resizable=1,fullscreen=1,scrollbars="+scrollbars//+auto;;
	regzaWin=window.open(regzaUrl,'',windowFeatures);
	if(window.focus)regzaWin.focus();
}

/*****************************************************************
	GetVars javascript class by Peter Bailey - Copyright (c) 2003
	Website: http://www.peterbailey.net/site/dev/jsclasses/
******************************************************************/

function GetVars( def, winRef ) 
{
	var win = ( typeof winRef != 'undefined' ) ? winRef : self;
	this._def_ = def;
	var query, queries = win.location.search.substring(1).split( /[&;]/ );
	for ( var i=0; ( query = queries[i] ); i++ )
	{
		query		= query.split( /\=/ );
		query[1]	= ( typeof query[1] == 'undefined' ) ? this._def_ : unescape( query[1].replace( /\+/g, " " ) );
		if ( this.exists( query[0] ) )
		{
			if ( typeof this[query[0]] == 'string' )
			{
				this[query[0]] = [this[query[0]],query[1]];
			} else {
				this[query[0]].push( query[1] );
			}
		} else {
			this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : query[1];
		}
	}
}

GetVars.prototype.assign = function( key, def )
{
	var deflt = ( typeof def == 'undefined' ) ? this._def_ : def;
	return ( !this.exists( key ) || this[key] == this._def_ ) ? deflt : this[key];
}

GetVars.prototype.exists = function( key )
{
	return ( typeof this[key] != 'undefined' );
}

GetVars.prototype.makeGlobal = function( action, prefix )
{
	prefix = ( typeof prefix == 'undefined' ) ? '' : prefix;
	for ( var i in this )
	{
		if ( typeof i != 'function' && i != '_def_' )
		{
			var gv = prefix + i;
			if ( window[gv] ) // Global already exists
			{
				switch( action )
				{
					case 0 : // Overwrite
						window[gv] = this[i];
						break;
					case 1 : // Skip
						break;
					case 2 : // Prompt
						if ( confirm( "The global variable '" + gv + "' aleady exists\nclick 'Ok' to overwrite, 'Cancel' to skip" ) )
							window[gv] = this[i];
						break;
				}
			}
			// Global doesn't already exists, assign as is
			else window[gv] = this[i];
		}
	}
}

