<!-- Javascript helper methods for embedded search -->
  d=document;
  tosWindow = '';
  
  function setFocus() {
    if (d.getElementById) d.getElementById('yschsp').focus();
  }

  if (d.getElementById) {
     oTog=d.getElementById('yschtg_w');
     patchCollectionLinks(oTog, true);
     oTog=d.getElementById('yschtg');
     patchCollectionLinks(oTog);     
  }
  
  function patchCollectionLinks(coll, openNewWindow) {
     if (coll) {
        aLnx=coll.getElementsByTagName('a');
        for (var i=0;i<aLnx.length;i++) {
            if (openNewWindow) {
               aLnx[i].target = "_blank";
            }
            aLnx[i].onclick=passP; 
        }
     }
  }

  function passP(e) {
    e=(e)?e:((window.event)?window.event:"");
    if (e) {      
      var o=(e.target)?((e.target.nodeType==3)?e.target.parentNode:e.target):e.srcElement;
      if (o.tagName=='SPAN'||o.tagName=='EM'||o.tagName=='STRONG') o=o.parentNode;  // Local  
      var qs="p";
      if (o.href.indexOf("yahoo") == -1) {
         qs="query";  
      }      
      var href = o.href; 
      if (window.RegExp&&window.encodeURIComponent) {
        var sP=encodeURIComponent(d.getElementById('yschsp').value);                  
        var re= new RegExp("(\\?|&)" + qs + "\\=(?:.*?)(&|$)", "i");               
        if (href.indexOf(qs + "=")!=-1) {              
          href=href.replace(re,"$1" + qs + "="+sP+"$2");           
        }         
        else {
          href+="&" + qs + "="+sP;
        }       
      }            
      if (o.href.indexOf("yahoo") != -1 && readCookie('ytos') == null) {        
        popYTos(href);
        return false; 
      }      
      o.href = href;    
    } 
    return true;    
  } 
  
  function popYTos(href) { 
     var ref = href;
     if (window.encodeURIComponent) {
        ref = encodeURIComponent(ref);
     }
     var tosUrl = "tos?yref=" + ref;    
  	 if (!tosWindow.closed && tosWindow.location) {		
		tosWindow.close();
	 } 		
	 tosWindow=window.open(tosUrl,'','height=360,width=325,resizable=yes,scrollbars=yes');
	 if (!tosWindow.opener) tosWindow.opener = self;			 
  }  
  
  function readCookie(name) {
	var nameStr = name + "=";
	var cookieArray = document.cookie.split(';');
	for(var i = 0; i < cookieArray.length;i++) {
	   var cookie = cookieArray[i];
	   while (cookie.charAt(0) == ' ') cookie = cookie.substring(1,cookie.length);
	   if (cookie.indexOf(nameStr) == 0) {
          return cookie.substring(nameStr.length,cookie.length);
       }
	}
	return null;
  }
  
  function addReferrer(e) {
     if (d.getElementById && window.encodeURIComponent) {       
       wref=d.getElementById('wref');                   
       q=d.getElementById('yschsp');
       url = "http://webindex.mccsokinawa.com/search/;jsessionid=db942o7ge8pfp"+"?query="+encodeURIComponent(q.value)+"&queryTimeout="+3000;
          //url += "&index="+encodeURIComponent("external"); original code, changed to below to allow embedded form 8/25/09
          url += "&index="+encodeURIComponent("external");    
        
       wref.value = url;             
     } 
  }







/*** DHTML textbox character counter (IE4+) script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)*/
function taLimit() {
	var taObj=event.srcElement;
	if (taObj.value.length==taObj.maxLength*1) return false;
}

function taCount(visCnt) {
	var taObj=event.srcElement;
	if (taObj.value.length>taObj.maxLength*1) 		taObj.value=taObj.value.substring(0,taObj.maxLength*1);
	if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}

/* Open new window */
function newHTML(title,text,dimensions) {

	HTMLstring='<html>\n';
	HTMLstring+='<head>\n';
	HTMLstring+='<title>' + title + '</title>\n';
	HTMLstring+='</head\>\n';
	HTMLstring+='<body>\n';

	HTMLstring+=text;

	HTMLstring+='</body style="font-family: helvetica;">\n';
	HTMLstring+='</html>';
	
	if (dimensions == "") {
		// default
		dimensions="width=640,height=600"
	}

	newwindow=window.open('','website',dimensions + ",statusbar=yes,scrollbars=yes,resizable=yes");
	newdocument=newwindow.document;
	newdocument.write(HTMLstring);
	newdocument.close();
}
/* Popups (uses website's style sheet) */
function popup(title,text,dimensions) {

	HTMLstring='<html>\n';
	HTMLstring+='<head>\n';
	HTMLstring+='<title>' + title + '</title>\n';
	HTMLstring+='<link rel="stylesheet" href="/style.css">\n';
	HTMLstring+='</head\>\n\n';
	HTMLstring+='<body>\n\n';
	HTMLstring+='<div style="background: #ffffff; margin: 10px; padding: 5px;" class="shadow">\n\n';

	HTMLstring+='<h3 style="margin: 0px;">' + title + '</h3>\n\n';
	HTMLstring+=text + '\n\n';

	HTMLstring+='</div>\n\n';
	HTMLstring+='</body>\n';
	HTMLstring+='</html>';
	
	if (dimensions == "") {
		// default
		dimensions="width=400,height=300"
	}

	newwindow=window.open('','popup',dimensions + ",statusbar=yes,scrollbars=yes,resizable=yes");
	newdocument=newwindow.document;
	newdocument.write(HTMLstring);
	newdocument.close();
}

function formHandler(form){
	var URL = document.form.ymenu.options[document.form.ymenu.selectedIndex].value;
	window.location.href = URL;
}

/*http://www.sitepoint.com/article/standards-compliant-world*/
function externalLinks() { 
	if (!document.getElementsByTagName) return; 
 		var anchors = document.getElementsByTagName("a"); 
 		for (var i=0; i<anchors.length; i++) { 
  			var anchor = anchors[i]; 
   			if (anchor.getAttribute("href") && 
       				anchor.getAttribute("rel") == "external") 
     				anchor.target = "_blank"; 
 	} 
} 
window.onload = externalLinks;

/* Modified from http://www.webmasterworld.com/forum91/441.htm (12:54 pm on June 10, 2003 (utc 0)) 
The following three functions (showhide, showDiv & hideDiv) go together */

function showhide(pass,element) {
	if (element !="") {
		var divs = document.getElementsByTagName(element);
	} else {
		var divs = document.getElementsByTagName("div");
	}
	
	for(i=0;i<divs.length;i++) {
		if(divs[i].id.match(pass)) {//if they 'see' divs
			if (document.getElementById) { // DOM3 = IE5, NS6
				if (divs[i].style.visibility=="hidden" || divs[i].style.visibility=="") {
					showDiv(pass,divs);
				} else {
					hideDiv(pass,divs);
				}
			} else if (document.layers) { // Netscape 4
				if (document.layers[divs[i]].display=="hidden" || document.layers[divs[i]].display=="") {
					showDiv(pass,divs);
				} else {
					hideDiv(pass,divs);
				}
			} else { // IE 4
				if (document.all.hideshow.divs[i].visibility=="hidden" || document.all.hideshow.divs[i].visibility=="") {
					showDiv(pass,divs);
				} else {
					hideDiv(pass,divs);
				}
			}
		}
	}
}

function hideDiv(pass,divs) {
	for(i=0;i<divs.length;i++) {
		if(divs[i].id.match(pass)) {//if they 'see' divs
			if (document.getElementById) { // DOM3 = IE5, NS6
				divs[i].style.visibility="hidden";// show/hide
				divs[i].style.display="none";
			} else if (document.layers) { // Netscape 4
				document.layers[divs[i]].display = 'hidden';
			} else { // IE 4
				document.all.hideshow.divs[i].visibility = 'hidden';
				//document.all.hideshow.divs[i].display = 'none';
			}
		}
	}
	
	var targetImg = eval('plus' + pass);
	targetImg.src='/images/plus.gif';
}

function showDiv(pass,divs) {
	for(i=0;i<divs.length;i++) {
		if(divs[i].id.match(pass)) {
			if (document.getElementById) {
				divs[i].style.visibility="visible";
				divs[i].style.display="block";
			} else if (document.layers) { // Netscape 4
				document.layers[divs[i]].display = 'visible';
			} else { // IE 4
				document.all.hideshow.divs[i].visibility = 'visible';
				document.all.hideshow.divs[i].display = 'block';
			}
		}
	}
	
	var targetImg = eval('plus' + pass);
	targetImg.src='/images/minus.gif';
}

function check_today(arr,display) {
// Get date from form menu
	// To make this function more functional, check to see if date menu is an array
	if (arr != "") {
		var form_date=theform.m[arr].options[theform.m[arr].selectedIndex].value + "/" + theform.d[arr].options[theform.d[arr].selectedIndex].value + "/" + theform.y[arr].options[theform.y[arr].selectedIndex].value;
	} else {
		var form_date=theform.m.options[theform.m.selectedIndex].value + "/" + theform.d.options[theform.d.selectedIndex].value + "/" + theform.y.options[theform.y.selectedIndex].value;
	}
	// JavaScript date
	var today=new Date()
		var js_date=(today.getMonth()+1) + "/" +  today.getDate() + "/" + today.getFullYear();
	
	// For testing...
	//alert(form_date + " " + js_date);

	// If the dates match, then user is trying to assign today's date, either alert them or make them confirm this is what they want to do.
	if (form_date == js_date) {
		if (display == "") {
			alert("This will expire today!")
		} else {
			if (confirm("Are you sure you want this to expire today?") == true) {
				return true;
			} else {
				return false;
			}
		}
	}
}

function replaceChar(out,add,element) {
	if (out == "" || add == "") {
	// Replace commas by default
		out = ","; // replace this
		add = "&#44;"; // with this
	}

	//temp = "" + entry; // temporary holder
	temp=document.getElementById(element).value;

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	document.getElementById(element).value=temp;
}

function checkAll(theForm,cName,check) {
//http://ben.liquidpulse.net/code/javascript/forms/checking_multiple_checkboxes_in_javascript/
    	for (i=0,n=theForm.elements.length;i<n;i++) {
        	if (theForm.elements[i].className.indexOf(cName) !=-1) {
            		theForm.elements[i].checked = check;
            	}
	}
} 
