//
// manages default values in form fields
//
function resetFields(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.type == "submit") continue;
		if (!element.defaultValue) continue;
		element.onfocus = function(){
			if (this.value == this.defaultValue) {
				this.value ="";
			}
		}
		element.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
			}
		}
	}
}
function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		// karim hack :: only login, search form can use this
		if (thisform.id == 'login-form' || 
			thisform.id == 'plogin-form' ||
			thisform.id == 'search_mini_form' || 
			thisform.id == 'discount-coupon-form' ||
			thisform.id == 'busse-forgotpassword' ) {
			resetFields(thisform);
		}
	}
}
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//
//	accordion functions for my-home.html
//

var accordionItems = new Array();

function initAccordion() {
	if(!document.getElementById('prescription-status')) return false; //we are not on the my-home page

	// Grab the accordion items from the page
	var divs = document.getElementsByTagName( 'div' );
	for ( var i = 0; i < divs.length; i++ ) {
		if ( divs[i].className == 'accordionItem' ) accordionItems.push( divs[i] );
	}
	
	// Assign onclick event to the first accordion child - in our case the div.header 
	for ( var i = 0; i < accordionItems.length; i++ ) {
		//IE and FF seem to see different first child nodes, e.g. childNodes[0] in IE is same as childNodes[1] in FF
		var header = findFirstElementNode(accordionItems[i]);
		header.onclick = toggleAccordionItem;
	}

	// Hide all accordion item bodies. Bodies follow the first child
	var bodyNode = 'null';
	
	// first accordion item will be visible so arrow will point down
//	var firstHeader = findFirstElementNode(accordionItems[0]);
//	var arrow = findFirstElementNode(firstHeader);
	
//	arrow.className = 'arrow-down';
	
	for ( var i = 0; i < accordionItems.length; i++ ) {
		bodyNode = get_nextsibling(findFirstElementNode(accordionItems[i]));
		if (bodyNode.id != 'expand-by-default') {
			bodyNode.className = 'prescription-status-body-hide';
			//accordionItems[i].hidden = 'true'; //set state to hidden
		
			//show all headers with arrow up
			otherHeader = findFirstElementNode(accordionItems[i]);
			arrow = findFirstElementNode(otherHeader);
	
			arrow.className = 'arrow-up';
		}
		else { 
			//show all headers with arrow up
			otherHeader = findFirstElementNode(accordionItems[i]);
			arrow = findFirstElementNode(otherHeader);
	
			arrow.className = 'arrow-down';
		}
		
	}
//	accordionItems[0].hidden = 'false'; //first accordion item is shown upon init
}


function toggleAccordionItem() {
	var bodyNode = get_nextsibling(this);
	var itemClass = bodyNode.className;
	var oldArrow = findFirstElementNode(this);
	var Header;
	var arrow;
	
//	if (this.parentNode.hidden == 'false') return;
	
	// Hide all items
	for ( var i = 0; i < accordionItems.length; i++ ) {
		bodyNode = get_nextsibling(findFirstElementNode(accordionItems[i]));
		bodyNode.className = 'prescription-status-body-hide'; 
		//accordionItems[i].hidden = 'true'; //set state to hidden
		
		//show all hidden headers with arrow up
		Header = findFirstElementNode(accordionItems[i]);
		arrow = findFirstElementNode(Header);
	
		arrow.className = 'arrow-up';
	}
	
	// Show this item if it was previously hidden
	if ( itemClass == 'prescription-status-body-hide' ) {
		bodyNode = get_nextsibling(this);
		bodyNode.className = 'prescription-status-body';
		//this.parentNode.hidden = 'false';
		
		oldArrow.className = 'arrow-down';
	}
}

var accordionItems2 = new Array();

function initAccordion2() {
	
	if(!document.getElementById('prescription-status')) return; //we are not on the my-home page
	
	// Grab the accordion items from the page
	var divs = document.getElementsByTagName( 'div' );
	for ( var i = 0; i < divs.length; i++ ) {
		if ( divs[i].className == 'accordionItem2' ) accordionItems2.push( divs[i] );
	}
	//accordionItems2 = document.getElementsByClassName( 'accordionItem2' );
	//Array.filter( accordionItems2, function(elem) { return elem.nodeName == 'DIV'; } );
	
	// Assign onclick event to the first accordion child - in our case the div.header 
	for ( var i = 0; i < accordionItems2.length; i++ ) {
		//IE and FF seem to see different first child nodes, e.g. childNodes[0] in IE is same as childNodes[1] in FF
		var header = findFirstElementNode(accordionItems2[i]);
		header.onclick = toggleAccordionItem2;	
	}

	// Hide all accordion item bodies except the first. Bodies follow the first child
	var bodyNode = 'null';
	
	// first accordion item will be visible so arrow will point down
//	var firstHeader = findFirstElementNode(accordionItems2[0]);
//	var arrow = findFirstElementNode(firstHeader);
	
//	arrow.className = 'arrow-down';
	
	for ( var i = 0; i < accordionItems2.length; i++ ) {
		bodyNode = get_nextsibling(findFirstElementNode(accordionItems2[i]));
		bodyNode.className = 'single-purchase-body-hide';
		//accordionItems2[i].hidden = 'true'; //set state to hidden
		
		//show all other headers with arrow up
		otherHeader = findFirstElementNode(accordionItems2[i]);
		arrow = findFirstElementNode(otherHeader);
	
		arrow.className = 'arrow-up';
	}
//	accordionItems2[0].hidden = 'false'; //first accordion item is shown upon init
}

function toggleAccordionItem2() {
	var bodyNode = get_nextsibling(this);
	var itemClass = bodyNode.className;
	var oldArrow = findFirstElementNode(this);
	var Header;
	var arrow;

//	if (this.parentNode.hidden == 'false') return;
	
	// Hide all items
	for ( var i = 0; i < accordionItems2.length; i++ ) {
		bodyNode = get_nextsibling(findFirstElementNode(accordionItems2[i]));
		bodyNode.className = 'single-purchase-body-hide'; 
		//accordionItems2[i].hidden = 'true'; //set state to hidden
		
		//show all hidden headers with arrow up
		Header = findFirstElementNode(accordionItems2[i]);
		arrow = findFirstElementNode(Header);
	
		arrow.className = 'arrow-up';
	}
	
	// Show this item if it was previously hidden
	if ( itemClass == 'single-purchase-body-hide' ) {
		bodyNode = get_nextsibling(this);
		bodyNode.className = 'single-purchase-body';
		//this.parentNode.hidden = 'false';
		
		oldArrow.className = 'arrow-down';
	}
}

// find first element child since FF and IE show different firstChild node
// IE and FF seem to see different first child nodes, e.g. childNodes[0] in IE is same as childNodes[1] in FF
		
function findFirstElementNode(Parent){
	for (var j = 0; Parent.childNodes[j].nodeType != 1; j++) {
	}
	return Parent.childNodes[j];
}


//check if the next sibling node is an element node
function get_nextsibling(currentNode){
	var nextNode = currentNode.nextSibling;
	while (nextNode.nodeType!=1) {
		nextNode=nextNode.nextSibling;
	 }
	return nextNode;
}

//
// toggles the filter div in categories.html and products-a-z.html
//
function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
	  document.getElementById('showParameters').className = 'down-up';	//change arrow link 
    }else{
      document.getElementById(divid).style.display = 'none';
	  document.getElementById('showParameters').className = 'up-down';	//change arrow link
    }
  }
function activateFilters(){
	if (!document.getElementById('showParameters')) return false;	// if this element does not exist we are on a different page
	if (!document.getElementById('filterOpen')) return false;		// don't want an error thrown
	var link = document.getElementById('showParameters');
	document.getElementById('filterOpen').style.display = 'none';
	link.onclick = function(){
		toggleDiv('filterOpen');
		return false;
	}
}

//
// toggles category options in  product filters popup
//
function toggleOption(object){
	var targetID = object.id + 'Options';
	if(document.getElementById(targetID).style.display == 'none'){
      document.getElementById(targetID).style.display = 'block';
	  object.className = 'down-up';	//change arrow link 
    }else{
      document.getElementById(targetID).style.display = 'none';
	  object.className = 'up-down';	//change arrow link
    }
  }
function activateOptionsLinks(){
	if (!document.getElementById('productFilters')) return false;	// if this element does not exist we are on a different page
	var optionsField ='';
	var links = document.getElementsByTagName('a');
	
	for(var i=0;i<links.length;i++) {
		if(links[i].className == 'up-down'){
			optionsField = links[i].id +'Options';
			document.getElementById(optionsField).style.display = 'none';
		
			// show default config with Diabetic Supplies shown
			if (optionsField == 'showDiabeticSuppliesOptions') {
				document.getElementById(optionsField).style.display = 'block';
				links[i].className = 'down-up';	//change arrow link 
			}
			
			links[i].onclick = function(){
				toggleOption(this);
				return false;
			}			
		}
	}
}

//
// toggles tabs in  product filters popup
//
function toggleTab(object){
	//hide all tabs
	var links = document.getElementsByTagName('a');
	
	for(var i=0;i<links.length;i++) {
		if(links[i].className == 'tab'){
			optionsField = links[i].id +'Tab';
			document.getElementById(optionsField).style.display = 'none';
			links[i].parentNode.className = 'inactive';
			}			
		}
	//show the selected tab	
	var targetID = object.id + 'Tab';
	document.getElementById(targetID).style.display = 'block';
	
	object.parentNode.className = 'current';

  }
function activateTabLinks(){
	if (!document.getElementById('tabsContainer')) return false;	// if this element does not exist there are no tabs on this page
	var optionsField ='';
	var links = document.getElementsByTagName('a');
	
	for(var i=0;i<links.length;i++) {
		if(links[i].className == 'tab'){
			optionsField = links[i].id +'Tab';
			document.getElementById(optionsField).style.display = 'none';
			
			// show default config with Product Categories shown
			if (optionsField == 'productCategoriesTab') {
				document.getElementById(optionsField).style.display = 'block';
				links[i].parentNode.className = 'current';
			}
			if (optionsField == 'full-descriptionTab') {
				document.getElementById(optionsField).style.display = 'block';
				links[i].parentNode.className = 'current';
			}
			
			
			links[i].onclick = function(){
				toggleTab(this);
				return false;
			}			
		}
	}
}

/* 
 * this is the generic function to show/activate boxes 
 *
*/
function showOverlay(anchorId, divId) {
	if (document.popup) {
		document.popup.style.visibility = 'hidden';
	}
	if (!document.getElementById(anchorId) || !document.getElementById(divId)) return;
	var activePopup = document.getElementById(divId);
	activePopup.style.visibility = 'visible';
	document.popup = activePopup;
	document.justSetPopup = 'true';
}
function activateOverlay(anchorId, divId) {
	if (!document.getElementById(anchorId) || !document.getElementById(divId)) return;
	var link = document.getElementById(anchorId);
	link.onclick = function() {
		showOverlay(link.id, divId);
		return false;
	}	
}
function activateOverlayByClass(className, divId) {
	if (!document.getElementById(divId)) return;
	// now traverse thru each A tag with class=className
	var allATags = document.getElementsByTagName('A');
	for (i=0; i<allATags.length; i++) {
		if (allATags[i].className==className) {
			var link = allATags[i];
			link.onclick = function() {
				showOverlay(link.id, divId);
				return false;
			}
		}
	}	
}

//
// begin tabs js for account nav
//

function setActiveTabs () {
	amElem = document.getElementById('account-manager');
	if (amElem != null) {
		aObj = document.getElementById('account-manager').getElementsByTagName('a');
		for(z=0;z<aObj.length;z++) {
			if(document.location.href.indexOf(aObj[z].href)>=0) {
				aObj[z].className='selected';
			}
		}
    }
}

//
// end tabs js for account nav
//

function showPatient(){
	if(!document.getElementById('showPatient')) return;	//just in case
	
	//if the other popup is already open close it first
	if(document.popup){
		document.popup.style.visibility = 'hidden';
	}

	var activePopup = document.getElementById('morePatientsWrap');
	activePopup.style.visibility = 'visible';
	
	document.popup = activePopup;	// store the active popup window object in the document object
	document.justSetPopup = 'true';	// to make sure the event handler doesn't reset instantly
}
function activatePatientsPane(){
	
	if(!document.getElementById('showPatient')) return;	//there is no login on this page
	
	var link = document.getElementById('showPatient');
	link.onclick = function(){
		showPatient();
		return false;
	}	
}

//
//	event function when click on show-patient button in account manager
//
function amShowPatient(){
	
	if(!document.getElementById('amShowPatient')) return;	//just in case
	
	//if the other popup is already open close it first
	if(document.popup){
		document.popup.style.visibility = 'hidden';
	}
	
	var activePopup = document.getElementById('amMorePatientsWrap');
	activePopup.style.visibility = 'visible';
	
	document.popup = activePopup;	// store the active popup window object in the document object
	document.justSetPopup = 'true';	// to make sure the event handler doesn't reset instantly
}
function activateAMPatientsPane(){
	
	if(!document.getElementById('amShowPatient')) return;	//there is no login on this page
	
	var link = document.getElementById('amShowPatient');
	link.onclick = function(){
		amShowPatient();
		return false;
	}	
}

//
// 	function to hide any popup window when click in document  outside of popup 
//	window pane is detected
//
function hideActivePopup(e){
	var target = (e && e.target) || (event && event.srcElement);
	
	if(!document.popup) return;	// no active popup object
	if (document.justSetPopup == 'true') {
		document.justSetPopup = 'false';
		return; // this click opened the popup
	}

	// don't hide if click was within active popup window
	var parent = checkParent(target);

	if(parent){
		document.popup.style.visibility = 'hidden';
		} 
}
function checkParent(t){ 
	while(t.parentNode){ 
		if(t==document.popup){ 
		return false 
		} 
	t=t.parentNode 
	} 
return true; 
}

document.onclick=hideActivePopup; 

window.onload = function(){
	prepareForms();
	activateFilters();
//	initAccordion();
//	initAccordion2();
	activateOptionsLinks();
	activateTabLinks();
    setActiveTabs();

	
	//overlays
	activateOverlay('showSavings', 'days360Overlay');
	activateOverlay('shareSocialLinks', 'showSocialLinks');
	activateOverlay('amShowPatient', 'amMorePatientsWrap');
	activateOverlay('showPatient', 'morePatientsWrap');
	activateOverlay('location-status', 'shipping-locations-box');
	activateOverlay('show-login', 'login-box');
	activateOverlay('tax-region-edit','tax-state');	
	activateOverlayByClass('shipping-location-overlay', 'shadow');
}
	
 

