//  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  FUNCTIONS FROM TELECT
//  /////////////////////////////////////////////////////////////////////////////////////////////////////////////

//  GLOBALS
siteLocation = "/www/";      // full path of the site from the server root


//  *************************************************************************************************************
//  HANDLES SWITCHING TABS ON PRODUCT PAGES
//  *************************************************************************************************************
function activateProductTab(tabId, count)
{
    // go through each of the tabs, setting them according to which was clicked
    for (i = 1; i <= count; i++)
    {
        // see if we found the clicked item
        if (tabId == (tabId.substr(0, tabId.indexOf("_")) + "_" + i))
        {
            // we found the clicked item, so make sure its tall tab is shown and show its page content
            document.getElementById(tabId).style.display = "none";
            document.getElementById(tabId + "_ov").style.display = "";
            document.getElementById(tabId + "_content").style.display = "";
        }
        
        else
        {
            // only process if the "_ov" item exists
            if (document.getElementById(tabId.substr(0,tabId.indexOf("_")) + "_" + i + "_ov") != null)
            {
                // this is not the clicked item, so show the short tab and hide the page content
                document.getElementById(tabId.substr(0,tabId.indexOf("_")) + "_" + i).style.display = "";
                document.getElementById(tabId.substr(0,tabId.indexOf("_")) + "_" + i + "_ov").style.display = "none";
                document.getElementById(tabId.substr(0,tabId.indexOf("_")) + "_" + i + "_content").style.display = "none";
            }
        }
    }
}


//  *************************************************************************************************************
//  HELPER FUNCTION FOR SETTING THE CURRENT NAVIGATION VALUE
//  *************************************************************************************************************
function setNavigation(navId)
{
    $NAV_SELECTED = navId.toLowerCase();
}


//  *************************************************************************************************************
//  HANDLES OPENING THE IMAGE VIEWER APPLICATION
//  *************************************************************************************************************
function ImageViewerOpenWindow(link)
{
    var catalogId = $(link).attr("CatalogId");
    var productId = $(link).attr("ProductId");
    var variantId = $(link).attr("VariantId");
    
    window.open(siteLocation + "ImageViewer/ImageViewer.aspx?CatalogID=" + catalogId + "&ProductID=" + productId +
        "&VariantID=" + variantId, "ImageViewerWindow", 
        "height=725,width=950,status=no,toolbar=no,menubar=no,location=no,resizable=no");
}


//  *************************************************************************************************************
//  TOGGLES THE DISPLAY OF DETAILED VARIANT INFORMATION ON PRODUCT PAGES
//  *************************************************************************************************************
function ToggleVariantDetailsDisplay(id)
{
    // adapted from an article on 4 Guys from Rolla
    // http://aspnet.4guysfromrolla.com/articles/091504-1.aspx

    var dElem = document.getElementById('d' + id);
    var hElem = document.getElementById('h' + id);
    
    if ((dElem) && (hElem))
    {
        if (dElem.style.display != 'block')
        {
            dElem.style.display = 'block';
            dElem.style.visibility = 'visible';
            hElem.style.display = 'none';
            hElem.style.visibility = 'hidden';
        } 
        else
        {
            dElem.style.display = 'none';
            dElem.style.visibility = 'hidden';
            hElem.style.display = 'block';
            hElem.style.visibility = 'visible';
        }
    }
}


//  *************************************************************************************************************
//  GENERIC FUNCTION FOR TOGGLING ROW ELEMENTS BY ID
//  *************************************************************************************************************
function ToggleRowDisplay(id)
{
    // adapted from an article on 4 Guys from Rolla and an article on snook.ca
    // http://aspnet.4guysfromrolla.com/articles/091504-1.aspx
    // http://www.snook.ca/archives/html_and_css/dynamically_sho/

    var elem = document.getElementById(id);
    
    if (elem)
    {
        // handle the difference between IE rows and Firefox rows using a standin variable
        if(navigator.appName.indexOf("Microsoft") > -1){
            var canSee = 'block'
        }
        else
        {
            var canSee = 'table-row';
        }

        // now toggle the row
        if (elem.style.display != canSee)
        {
            elem.style.display = canSee;
            elem.style.visibility = 'visible';
        } 
        else
        {
            elem.style.display = 'none';
            elem.style.visibility = 'hidden';
        }
    }
}


//  *************************************************************************************************************
//  TOGGLES THE DISPLAY OF ANY GENERIC ITEM
//  *************************************************************************************************************
function ToggleDisplayGeneric(id)
{
    // adapted from an article on 4 Guys from Rolla
    // http://aspnet.4guysfromrolla.com/articles/091504-1.aspx

    var elem = document.getElementById(id);
    
    if (elem)
    {
        if (elem.style.display != 'block')
        {
            elem.style.display = 'block';
            elem.style.visibility = 'visible';
        } 
        else
        {
            elem.style.display = 'none';
            elem.style.visibility = 'hidden';
        }
    }
}


//  *************************************************************************************************************
//  HANDLES OPENING THE PRODUCT LearnMore WINDOW
//  *************************************************************************************************************
function OpenLearnMore(link)
{
    var catalogId = $(link).attr("CatalogId");
    var productId = $(link).attr("ProductId");
    var variantId = $(link).attr("VariantId");
    
    window.open(siteLocation + "Products/LearnMore.aspx?CatalogId=" + catalogId + "&ProductId=" + productId +
        "&VariantId=" + variantId, "LearnMoreWindow", "height=500,width=700,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=no");
}

function OpenLearnMore2(catalogId, productId, variantId)
{
    window.open(siteLocation + "Products/LearnMore.aspx?CatalogId=" + catalogId + "&ProductId=" + productId +
        "&VariantId=" + variantId, "LearnMoreWindow", "height=500,width=700,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=no");
}


//  *************************************************************************************************************
//  PRINT FUNCTION (ADAPTED FROM THE ARTICLE http://www.codeproject.com/useritems/javascript_print_page.asp)
//  *************************************************************************************************************
function PrintPage(originalURL, pageName)
{
    // set up some variables
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
        disp_setting+="scrollbars=yes,width=750, height=600, left=100, top=25";
    var printContent;
    var test = document.getElementById("print-content");
    
    // if the element was found, assign the innerHTML value
    if (test)
        printContent = document.getElementById("print-content").innerHTML;

    // if we have something, print just that, otherwise print the entire page
    if (printContent)
    {
        var docprint=window.open("","",disp_setting);
        docprint.document.open();
        docprint.document.write('<html><head><title>Telect</title>');
        docprint.document.write('<link href="' + siteLocation + 'css/styles.css" rel="stylesheet" type="text/css" />');
        docprint.document.write('<link href="' + siteLocation + 'css/styles_hmpg.css" rel="stylesheet" type="text/css" />');
        docprint.document.write('<link href="' + siteLocation + 'css/styles_layouts.css" rel="stylesheet" type="text/css" />');
        docprint.document.write('<link href="' + siteLocation + 'css/styles_myorder.css" rel="stylesheet" type="text/css" />');
        docprint.document.write('<link href="' + siteLocation + 'css/styles_print.css" rel="stylesheet" type="text/css" />');
        docprint.document.write('<style type="text/css">body { font-size: .8em; color: #666666; margin: 0px; ');
        docprint.document.write('background-image: url(""); background-color: #ffffff; } </style></head>');


        docprint.document.write('<body onLoad="self.print()">');
        docprint.document.write('<div class="banner"><img src="/images/print/logo.gif" /></div>');
        docprint.document.write('<div class="pagedef">');
        //docprint.document.write('	<p><strong>Page:</strong> ' + pageName + ' </p>');
        docprint.document.write('	<p><strong>URL:</strong> ' + originalURL + ' </p>');
        docprint.document.write('</div>');
        docprint.document.write('<div class="pgcontainer">' + printContent + '</div>');
        docprint.document.write('<div class="pgfooter">&copy;2007 <strong>Telect, Inc.</strong> All Rights Reserved</div>');
        docprint.document.write('</body></html>');
        docprint.document.close();
        docprint.focus();
    }
    else
        window.print();
}


//  *************************************************************************************************************
//  HANDLES ADDING A PRODUCT AND ITS ASSOCIATED QUANTITY TO THE ORDER BASKET
//  *************************************************************************************************************
function AddToOrder(catalogID, productID, variantID, referrerURL, projectCart)
{
    // set a default purchase quantity, then try to get the text box object
    purchaseQty = "1";
    var purchaseQtyObj = document.getElementById('txt_' + variantID);

    // if we found the object, set the purchase quantity to its value rather than 1    
    if (purchaseQty)
        purchaseQty = purchaseQtyObj.value;
        
    // if the user wants a new project cart, get its name
    if (projectCart == "_NEW_")
    {
        projectCart = prompt("Please provide the name of your new project:", "");
        if (projectCart != null && projectCart != "")
        {
            projectCart = "_NEW_" + projectCart;
        }
        else
        {
            alert("A name for the new project is needed.\nThe item was not added to a new project.");
            return false;
        }
    }

    window.location = siteLocation + 'Orders/AddToOrder.aspx?CatalogID=' + catalogID +
        '&ProductID=' + productID + '&VariantID=' + variantID + '&Referrer=' + referrerURL +
        '&Qty=' + purchaseQty + '&ProjectCart=' + projectCart;
}


// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS FROM ASC
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  *************************************************************************************************************
//	PERFORMS ANY KIND OF PAGE SPECIFIC CLIENT-SIDE SETUP 
//  *************************************************************************************************************
function initPage(section_id) {	
	// always preload the main tab nav
	MM_preloadImages('/images/promo/newsalerts_ov.gif','/images/nav_solutions_ov.jpg','/images/nav_products_ov.jpg','/images/nav_services_ov.jpg','/images/nav_support_ov.jpg','/images/nav_partners_ov.jpg');
	
	// preload the right column expand/collapse images
	MM_preloadImages('/images/promo/newsalerts_ov.gif');
	
	if (section_id && section_id.constructor == String) {
		if (section_id.toLowerCase() != 'map')
			$NAV_SELECTED = section_id.toLowerCase();
	
			switch (section_id) {
			case "map":
				MM_preloadImages('/images/map/map_africa.gif','/images/map/map_asia.gif','/images/map/map_asiapacific.gif','/images/map/map_europe.gif','/images/map/map_middleeast.gif','/images/map/map_canada.gif','/images/map/map_usa.gif','/images/map/map_mexico.gif','/images/map/map_southamerica.gif');	
				break;	
		}
	}
}

//  *************************************************************************************************************
//	FUNCTIONS FOR TAB NAV ROLLOVERS 
//  *************************************************************************************************************
function tabnavRollOver(domRef, forceReset) {
	// get the current source, parse, and extract name and extension
	// rollover state must be in same directory as static and must have suffix "_ov" (example: static = myimg.jpg; rollover = myimg_ov.jpg)
	var curSrc = domRef.src;
	var fullFileName = curSrc.substring(curSrc.lastIndexOf("/")+1);
	var filePath = curSrc.substring(0, curSrc.lastIndexOf("/")+1);
	
	// makes an array that is made up of: [0] = the file name, [1] = the file extension
	var filePieces = fullFileName.split(".");
	
	// if there is an "_ov" in the name, then need to return to static state
	// if forceReset is true, then it will always return to the static state
	if (filePieces[0].substring(filePieces[0].length - 3) == "_ov")
		var newFile = filePieces[0].substring(0, filePieces[0].lastIndexOf("_ov"));
	else if (forceReset == true)
		var newFile = filePieces[0];
	else
		var newFile = filePieces[0] + "_ov";
	
	// concatenate the file name and extension
	newFile += "." + filePieces[1];
	
	// set the src :)
	domRef.src = filePath + newFile;
}


//  *************************************************************************************************************
//	FUNCTIONS TO HIDE AND SHOW STUFF, BASED ON A PREDEFINED ID CONVENTION 
//  *************************************************************************************************************
function showHideArea(domID, itemPrefix, imgType, extraElements) {
	var domObj = document.getElementById(domID);
	var doHide = (domObj.style.display == 'none') ? false : true;
	
	// set the display of the obj
	domObj.style.display = (doHide == true) ? 'none' : 'block';
	
	// see if there is an arrow button to change and change it
	if (document.getElementById(itemPrefix + "expandbtn")) {
		var newImgPath = getImgPath(imgType, doHide);
		document.getElementById(itemPrefix + "expandbtn").src = newImgPath;
	}
	
	// see if there is a label to change and change it
	if (document.getElementById(itemPrefix + "expandlabel")) {
		if (itemPrefix.indexOf("search") != -1)
			var newLabel = (doHide == true) ? 'Advanced' : 'Basic';
		else
			var newLabel = (doHide == true) ? 'Show' : 'Hide';
		
		document.getElementById(itemPrefix + "expandlabel").innerHTML = newLabel;
	}
	
	// if there are any elements in the extraElements array, and they exist, show or hide them
	if (extraElements) {
		var i = 0;
		var loopLength = extraElements.length;
		for (i; i < loopLength; i++) {
			var idString = itemPrefix + extraElements[i];
			var curDomObj = document.getElementById(idString);
			curDomObj.style.display = (doHide == true) ? 'none' : 'block';
		}
	}
}
function getImgPath(imgType, doHide) {
	var returnStr;
	
	switch (imgType) {
		case 'rcol_circle':
			returnStr = (doHide == true) ? '/images/icon_rcol_expand.jpg' : '/images/icon_rcol_collapse.jpg';
			break;
		case 'rcol_3bluedots':
			returnStr = (doHide == true) ? '/images/bullet_3dots_blue_down.gif' : '/images/bullet_3dots_blue_up.gif';
			break;
		case 'green_circle':
			returnStr = (doHide == true) ? '/images/icon_green_expand.jpg' : '/images/icon_green_collapse.jpg';
			break;
		case 'lineitem_circle':
			returnStr = (doHide == true) ? '/images/lineitem_expand.gif' : '/images/lineitem_collapse.gif';
			break;
	}
	
	return returnStr
}


//  *************************************************************************************************************
//	VARIOUS USEFUL MACROMEDIA FUNCTIONS
//  *************************************************************************************************************
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

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_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_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];}
}