// stdinclude.js

// Global Variables
paramString = window.location.search;

// functions to handle parameters
function getParamVal(){
	// NOTE: This routine will not work correctly for
	// cases where a paramString contains more than
	// one name=value item for the SAME name. This routine
	// will return only the first instance. On the other hand,
	// if the paramString contains 'nam1=val1,val2&nam2=val2'
	// getParamVal('nam1') will return the value 'val1,val2'.
 	// Treat the search string as case insensitive, but
	// return the value in the original case, of course.
	var pString;
	var pName;
 	if (arguments.length < 1) { return null; }
 	else if (arguments.length == 1) {
 		pString = paramString;
 		if (pString == null) { return null;}
 		pName = arguments[0];
 	}
 	else {
 		pString = arguments[0]; pName = arguments[1];
 	}

 	var pStringU = pString.toUpperCase();
 	var pNameU = pName.toUpperCase();
 	var pNameLoc = pStringU.indexOf(pNameU);
 	if (pNameLoc == -1) {
 		return (null);
 	}
 	else {
 		var nxtPLoc = pStringU.indexOf('&',pNameLoc+1);
 		if (nxtPLoc == -1) {
 			nxtPLoc = pStringU.length ;
       		}
 		var pValLoc = pStringU.indexOf('=',pNameLoc+1);
		if (pValLoc == -1) {return null;}
		return (pString.substring(pValLoc+1,nxtPLoc));
 	}
}
function replace(inStr, oldStr, newStr){
	// replace all occurences of oldStr in inStr with newStr
	// return result unless some bad parameters in which case return inStr
	var retStr='';
	var start=-1;
	var len=0;
	if (newStr == null) { newStr = ''; }
	if (inStr == null || oldStr == null || (start=inStr.indexOf(oldStr))==-1) {
		return inStr ;
	}
	len=oldStr.length;
	// grab first part of inStr
	retStr = inStr.substring(0,start);
	// add the replacement string
	retStr += newStr;
	var oldstart=start;
	while ((start=inStr.indexOf(oldStr,oldstart+1)) != -1){
		// grab next segment of inStr
		retStr += inStr.substring(oldstart+len,start);
		// add the replacement string
		retStr += newStr;
		// prepare for loop
		oldstart=start;
	}
	if (inStr.length > (oldstart+len)) {
		// we didn't end with oldStr
		retStr += inStr.substring(oldstart+len);
	}
	return retStr;
}

function packFields(formObj){
	// take all form fields and return them as a single string with name=value pairs
	// note that the returned string has NOT been url encoded! since quotes have not been escaped
	// be careful about tossing into a javascript variable!
	var retStr="";
	var tmp=null;
	for (var i=0;i<formObj.elements.length;i++){
		if ((tmp=fieldType(formObj.elements[i])) != null) {
			// add ampersand on later iterations only
			if (retStr != "") { retStr +="&" ; }
			retStr += formObj.elements[i].name + "=" ;
			if (tmp.toLowerCase() == 'text') {
				retStr += getTextVal(formObj.elements[i]) ;
			}
			else if (tmp.toLowerCase() == 'hidden') {
				retStr += getTextVal(formObj.elements[i]) ;
			}
			else if (tmp.toLowerCase() == 'textarea') {
				retStr += getTextVal(formObj.elements[i]) ;
			}
			else if (tmp.toLowerCase() == 'radio') {
				retStr += getRadioVal(formObj.elements[i]) ;
			}
			else if (tmp.toLowerCase() == 'checkbox') {
				retStr += getCheckVal(formObj.elements[i]) ;
			}
			else if (tmp.toLowerCase() == 'select-one') {
				retStr += getSelectVal(formObj.elements[i]) ;
			}
			else if (tmp.toLowerCase() == 'select-multiple') {
				retStr += getSelectVal(formObj.elements[i], true) ;
			}
		}
	}
	return retStr ;
}
function fieldType(fObj){
	var retVal = null;
	var validfields = 'textareahiddentextradiocheckboxselect-oneselect-multiple';
	if (fObj != null && validfields.indexOf(fObj.type) >=0) { retVal=fObj.type; }
	return retVal;
}

function getRadioVal(rObj){
	var retVal=null;
	if (rObj!=null){
		for (var i=0; i<rObj.length; i++) {
			if (rObj[i].checked) {
				retVal = rObj[i].value;
				break;
			}
		}
	}
	return retVal;
}
function getCheckVal(cObj){
	// return comma delimited list of check box values
	var retVal=null;
	if (cObj!=null){
		for (var i=0;i<cObj.length;i++){
			if (cObj.checked) {
				retVal += ',' + cObj.value ;
			}
		}
		retVal = retVal.substring(1);
	}
	return retVal;
}
function getSelectVal(sObj){
	// argument[1] is optional "multiselect" boolean flag
	// if true: return comma delimited list of selected values
	// else: return single selected value
	var retVal=null;
	if (sObj!=null && sObj.selectedIndex >= 0){
		if (arguments.length < 2 || arguments[1]==false){
			retVal = sObj.options[sObj.selectedIndex].value;
		}
		else {
			for (var i=0;i<sObj.length;i++){
				if (sObj.options[i].selected) {
					retVal += ',' + sObj.options[i].value ;
				}
			}
			retVal = retVal.substring(1);
		}
	}
	return retVal;
}
function getTextVal(tObj){
	var retVal=null;
	if (tObj!=null){
		retVal = tObj.value;
	}
	return retVal;
}
function quote2bar(instr) {
	var pos = -1;
	var cin = "'"; // replace this
	var cout = "_"; // with this
	if (arguments.length>2){
		cin=arguments[1]; // or replce this
		cout=arguments[2]; // with this
	}
	var temp = "" + instr; // temporary holder

	while (temp.indexOf(cin)>-1) {
		pos= temp.indexOf(cin);
		temp = "" + (temp.substring(0, pos) + cout + temp.substring((pos + cin.length), temp.length));
	}
	return (temp);
}
function escapeQuote(inStr){
	return replace(inStr,"'","\\'");
}
function escapeDQuote(inStr){
	return replace(inStr,'"','\\"');
}
function unescapeQuote(inStr){
	// be careful what you do with the returned value!
	// it has a single quote in it!
	return replace(inStr,"\\'","'");
}
function unescapeDQuote(inStr){
	// be careful what you do with the returned value!
	// it has a single quote in it!
	return replace(inStr,'\\"','"');
}
function urlEncode(inStr){
	return inStr;
}
function urlDecode(inStr){
	return inStr;
}
function trim(inStr){
	if (inStr==null) { return null;}
	var retStr="";
	// trim the leading spaces
	for (var i=0;i<inStr.length;i++){
		if (inStr.charAt(i) != " " ) { break;}
		else { continue; }
	}
	if (i==inStr.length) { return null; }
	retStr = inStr.substring(i);
	// optimize usual case
	if (retStr.charAt(retStr.length-1)!=" "){
		return retStr;
	}

	// trim the trailing spaces
	for (var i=(inStr.length-1);i>0;i--){
		if (inStr.charAt(i) != " " ) { break;}
		else { continue; }
	}
	return (retStr.substring(0,i+1));
}
// can be used to validate certain form inputs
// these are VERY weak tests!!
function hasEmailAddress(inStr){
	var retVal=true;
	if (inStr == null || inStr.indexOf('@')==-1 || inStr.indexOf('.')==-1) {
		if (arguments.length==0 || arguments[0]!='silent') {
			alert('Please enter the missing Email address.');
		}
	   	retVal = false;
	}
	return retVal;
}
function hasName(inStr){
	var alphabet="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var retVal=false;
	var i=0;
	while (inStr != null && i<inStr.length){
		if (alphabet.indexOf(inStr.charAt(i++))!= -1) {
			// something was entered
			retVal= true;
			break;
		}
	}
	if (retVal==false) {
		alert('Please enter your name.');
	}
	return retVal;
}

function okclient(){
	// support IE 5.5+ on PC and Mac
	// support NS 6.0+ on Mac only
	// ADD SOMETHING FOR AOL SUPPORT?
	// no others supported
	// if MJLUserCheck cookie exists, return true
	// we rely on routines in user.js
	var MJLUserCheck = getCookie("JBUserCheck");
	if (MJLUserCheck!=null){ return true; }
	setCookie("JBUserCheck","true");
	if (ie55up()) { return true; }
	else if (mac() && ns6up()) { return true;}
	else { return false;}
}
function mac(){
	// detect MAC platform
	if (navigator.platform.indexOf('Mac') != -1){
		return true;
	}
	else {
		return false;
	}
}
function aol(){
	var agt = navigator.userAgent.toLowerCase();
	if (agt.indexOf('aol') == -1) { return false; }
	else { return  true; }
}
function ns6up(){
	//detect Netscape 6.0
	if (navigator.appName=="Netscape"&&parseFloat(navigator.appVersion)>=5.0){
		return true;
	}
	else {
		return false;
	}
}
function ie55up(){
	//Detect IE5.5+
	var version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		var temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	//Note: NON IE browser has version==0
	if (version>=5.5) {
		return true;
	}
	else {
		return false;
	}
}
// general function to open new window
function openwin(u,w,p){
	if (u==null) { return ;}
	if (w == null) { w="POPWIN" ;}
	if (p == null) {
		p="height=600,width=750,directories=no,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,status=no,location=no";
	}
	var newwin = window.open(u, w, p);
}

function getPrnFile () {
	var thisURL = document.URL;
	var i = thisURL.indexOf('.ht');
	var prnURL = thisURL.substring( 0, i ) + '_Prn' + thisURL.substring(i, thisURL.length);
	return prnURL;
}

function printarticle2(){
    	if (typeof(printerfile)=='undefined' || printerfile==null) {
		window.print();
	}
	else {
		var locStr = printerfile ;
		var winprops="height=600,width=750,directories=no,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,status=no,location=no";
		var newwin = window.open(locStr, "PRINTARTWIN", winprops);
	}

	//window.print();
}

function findsimilar(){
	var win=null;
	var fsLimit=4;

    //alert('in fs');

	//var locStr = '/find/find_similar.html?docownid=' + thisTopicId + '&docname=' + thisTopicName + '&fs=' +
	//    findSimilar.replace("&#039;","\'") + '&targetwin=self&limits='+fsLimit+'' ;
    var locStr = 'http://www.jbooks.com:8080/find/find_similar.html?docownid=' + thisTopicId.replace("&#039;","\'") + '&docname=' + thisTopicName.replace("&#039;","\'") + '&fs=' +
	    findSimilar.replace("&#039;","\'") + '&targetwin=self&limits='+fsLimit+'' ;
	//var locStr = '/fs/findsimilar.jsp?docownid=' + thisTopicId + '&docname=' + thisTopicName + '&fs=' +
	//    findSimilar.replace("&#039;","\'") + '&targetwin=self&limits='+fsLimit+'' ;

    //alert("locStr: " + locStr);


	//alert("locStr = " + locStr);

	win = window.open(locStr,"FindSimilar", "height=500,width=550,directories=no,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,status=no,location=no");
}

function gokey(){

    var kw = replace(window.document.form1.keyword.value, "\"","");
    kw = replace(window.document.form1.keyword.value, "'","");
    if (trim(kw) == "") alert("Please enter Key Word(s).");
    else
    win = window.open("/fs/searchkey.jsp?kw=" + kw, "Key", "menubar=mo,toolbar=no,scrollbars,resizable=no,height=500,width=500")
}
function surfto(form) {
         var myindex=form.dest.selectedIndex;
         location=form.dest.options[myindex].value;
}
