/***************************************************/
/* JavaScript for the BPC Extranet Web Application */
/***************************************************/

// Global Variables
var timerSet = -1;

// Get the correct Form Object...
var isMac = ((navigator.userAgent.indexOf("Mac") >= 0) && (navigator.userAgent.indexOf("Netscape6") < 0)) ? true : false;

// On start determine the Web Application Context Path 
// NOTE: We Only Check to see if 2 levels deep or not!
var webPathName = location.pathname;
var indexPos    = webPathName.indexOf("/",1);
var webContext  = "/" + webPathName.substring(1, indexPos);
var indexPos2   = webPathName.indexOf("/", indexPos + 1);

// Check to see if we need to get part 2 of the Context path or not...
if (( indexPos2 > 0 ) && ( indexPos2 < webPathName.length )) {
//	webContext = webContext + webPathName.substring(indexPos, indexPos2) + "/";
	webContext = webContext + "/webapp/";
} else {
	webContext = webContext + "/";
}


// Used to suppress Error Messages...
function noErrorMessages () { return true; }


// Suppress ALL JavaScript error Messages...
window.onerror = noErrorMessages;

function getMarket() {
  var metas = document.getElementsByTagName('META');
  var i;
  var MarketID = 'none';
  for (i = 0; i < metas.length; i++){
    if (metas[i].getAttribute('NAME') == 'market'){
      MarketID = metas[i].getAttribute('CONTENT');
      break;
    }
  }
  
  return MarketID;
  
}

function setNavBar(){	
    
    var marketID = getMarket(); 
	var navbar = getBrowserObject("lNavBar");
	var hitForm = getBrowserObject("hitForm");
	var hit = '0';
	if (hitForm){
		hit = hitForm.hit.value;
	}
	if (navbar && hit == '0'){
		
		var newSource = '/Professional/webapp/LeftNavBar';
		var oldSource = navbar.src;
		
		if( marketID != 'none' && marketID != '*'){
			newSource = newSource + '?m=' + marketID;
		}
	   
		navbar.src = newSource;
				
		
	}
	
	if (hitForm){
		 hitForm.hit.value = '1';
	}
}


// Gets the correct object based upon which browser is in 
// use when the functions are called!
function getBrowserObject( element) {
	if ( !document.all && document.getElementById) {
		// NN6+
		return eval( "document.getElementById('" + element + "')");
	} else if ( document.all) {
		// IE5.x+
		return eval( "document.all." + element);
	}	
}


// Sets the select object to the correctly
// selected Option tag/field....
function setSelect( objName, objValue){
	var selObj = getBrowserObject( objName);
 	var selLength = selObj.length;
	for( var i=0; i < selLength; i++) {
		if ( selObj.options[i].value == objValue) {
			selObj.options[i].selected = true;
			break;
		}
	}
}


/*
 * General Java Script Functions for the BPC Extranet - Catalog *
 */

function addField (form, fieldType, fieldName, fieldValue) {
  if (document.getElementById) {
    var input = document.createElement('INPUT');
      if (document.all) { // what follows should work 
                          // with NN6 but doesn't in M14
        input.type = fieldType;
        input.name = fieldName;
        input.value = fieldValue;
      }
      else if (document.getElementById) { // so here is the
                                          // NN6 workaround
        input.setAttribute('type', fieldType);
        input.setAttribute('name', fieldName);
        input.setAttribute('value', fieldValue);
      }
    form.appendChild(input);
  }
}


function adjustFrame( iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 8 + 'px';
    iframeElement.style.width  = iframeWindow.document.width + 6 + 'px';
  } else if (document.all) {
  	var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat') {
    	iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 8 + 'px';
      	iframeElement.style.width  = iframeWindow.document.documentElement.scrollWidth + 6 + 'px';
    } else {
      	iframeElement.style.height = iframeWindow.document.body.scrollHeight + 8 + 'px';
     	iframeElement.style.width = iframeWindow.document.body.scrollWidth + 6 + 'px';
    }
  }
}


function MM_swapImgRestore() { //v3.0
   var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function open_win( url, type) {
	switch( type) {
		case "map":
			mywin = window.open( url,"map",'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=735,height=527,copyhistory=0');		
			break;
		case "img":
			mywin = window.open( url,"img",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=320,height=240');
			break;
		case "acc":
			mywin = window.open( url,"acc",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=300,height=200');			
			break;
		case "new":
			mywin = window.open( url,"new",'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');			
			break;
		default:
			mywin = window.open( url,"default",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=400');
			
	}
	
	mywin.focus();
}



function open_window( url) {
	open_win( url, "general");
}



function openMapPopup( url) {
	open_win( url, "map");
}



function showDetail( tagType, tagName) {
   if (document.all) {
   	 var tags = window.frames.mFrame.document.all.tags( tagType);
   } else if ( document.getElementsByTagName) {
     var tags = document.getElementById( 'mFrame').contentDocument.getElementsByTagName( tagType);
   } 
   if (tags) {
     for (var d = 0; d < tags.length; d++)
     	if ( tags[d].id == tagName) { 
     		tags[d].style.display="";
     	} else {
     		tags[d].style.display="none";
     	}
   }
}


