
// Copyright (c) 2007 Factotem Inc
// 02/06/2008 VdP rev 1.4
//

function setLogo(input) {
	if (input == "nologo") {
		document.getElementById("previewLogo").style.display = "none";
		document.getElementById("previewLogo").style.visibility = "hidden";
		document.getElementById("selectLogoLocation").disabled = true;
		document.getElementById("submitPreviewButton").value = "ProQuest Search";
	} else {
		document.getElementById("previewLogo").style.display = "block";
		document.getElementById("previewLogo").style.visibility = "visible";
		document.getElementById("selectLogoLocation").disabled = false;
		document.getElementById("submitPreviewButton").value = "Search";
	}
} // end setLogo



function setLogoLocation(input) {
	if (input == "above") {
		document.getElementById("previewLogo").style.display = "block";
	} else {
		document.getElementById("previewLogo").style.display = "inline";
	}
} // end setLogoLocation



function setButtonLocation(input) {
	if (input == "left") {
		document.getElementById("previewButton").style.display = "inline";
	} else {
		document.getElementById("previewButton").style.display = "block";
	}
} // end setButtonLocation



function setBrdColor(input) {
	if (input.length == 7) {
		document.getElementById("previewBox").style.border = "1px solid " + input + "";
	}
} // end setBrdColor



function setBgColor(input) {
	if (input.length == 7) {
		document.getElementById("previewBox").style.backgroundColor = "" + input + "";
	}
} // end setBrdColor



var MIN_SEARCH_BOX_LENGTH = 15;
var MAX_SEARCH_BOX_LENGTH = 60;

var DEFAULT_SEARCH_BOX_LENGTH = 30;
var boxLen = DEFAULT_SEARCH_BOX_LENGTH;

function setSearchBoxLength() {
	var x = parseInt(document.getElementById("inputTextBox").value);
	if (x >= MIN_SEARCH_BOX_LENGTH && x <= MAX_SEARCH_BOX_LENGTH) {
			boxLen = x;
	}
	document.getElementById("inputPreviewBox").size = boxLen;
	
	// Used color picker script to overcome quirk in Firefox with resizing floating divs
	showColorPicker(document.forms[0].colorPicker1,document.forms[0].inputBgColor);
	color_picker_div.style.display='none';
} // end setSearchBoxLength



function createWidget() {
	// Search paramters.
	var databases = "&DBId=" + encodeURIComponent(getAndValidateDatabases());	
	var fulltext = "";
	if(document.forms[0].checkFullText.checked) fulltext = "&ShowFT=1";
	var PeerReviewd = "";
	if(document.forms[0].checkPeerReviewed.checked) PeerReviewd = "&PeerReviewd=1";
	var proxy = trim(document.forms[0].inputProxy.value);
	var terms = "&SQ=" + getAndValidateSearchTerms();
	if(terms != "&SQ=") terms += "+";
	//
	// Search box style.
	// Logo: nologo | pqlogo
	var logo = document.forms[0].selectLogo.options[document.forms[0].selectLogo.selectedIndex].value;
	// Logo location: above | right
	var locationLogo = document.forms[0].selectLogoLocation.options[document.forms[0].selectLogoLocation.selectedIndex].value;
	var logoHtml = "";
	var searchBtnVal = "ProQuest Search";
	if(logo == 'pqlogo'){
		searchBtnVal = "Search";
		if (locationLogo == "above") {
			logoStyleDisplay = "block";	
		}else{
			logoStyleDisplay = "inline";	
		}
		logoHtml = '<div style="display:' + logoStyleDisplay 
			+ '"><a href="http://proquest.umi.com/login"><img src="http://proquest.umi.com/images/common/logo_proquest_widget.gif" alt="ProQuest" border="0"/></a>&nbsp;</div>'
	}
	// Search button location: below | left
	var locationButton = document.forms[0].selectButtonLocation.options[document.forms[0].selectButtonLocation.selectedIndex].value;
	if (locationButton == "left") {
		buttonStyleDisplay = "inline";	
	}else{
		buttonStyleDisplay = "block";	
	}
	// 
	var colorBorder = document.forms[0].inputBrdColor.value;
	var colorBackground = document.forms[0].inputBgColor.value;
	var searchBoxStyle = "border: 1px solid " + colorBorder 
		+ "; background-color:" + colorBackground 
		+ ";margin: 0 1.00em 1.00em 0; padding: 1.00em 0.75em;";
	//
	var length = parseInt(document.forms[0].inputTextBox.value);
		if (length < MIN_SEARCH_BOX_LENGTH) {
			length = MIN_SEARCH_BOX_LENGTH;
		}
		if (length > MAX_SEARCH_BOX_LENGTH) {
			length = MAX_SEARCH_BOX_LENGTH;
		}
	//
	var url = proxy + "http://proquest.umi.com/pqdweb?RQT=305&FT=1" + databases + fulltext + PeerReviewd + terms;
	var widgetCode = "<!-- ProQuest Search Widget v1.4 02062008\n"
	    + 'This Search Widget was developed by ProQuest for use with ProQuest databases. \n'
		+ 'Access to ProQuest databases is limited to authenticated users only.\n'
		+ 'To create a ProQuest Search Widget visit: http://widgets.proquest.com .  --> \n'
		+ '<script type="text/javascript">\n'
		+ 'function proQuestSearchGo(){\n'
		+ 'var url="' + url + '";\n'
		+ 'var searchInputEl = document.getElementById("proQuestSearchInput");\n'
		+ 'document.location=url + encodeURIComponent(searchInputEl.value);\n'
		+ '}'
		+ '</script>\n'
		+ '<!-- Javascript ENTER key trapper --> \n'
		+ '<script type="text/javascript"> \n'
		+ 'function handleKeyPress(e,form){ \n'
		+ 'var key=e.keyCode || e.which; \n'
   		+ '   if (key==13){ \n'
        + '   	proQuestSearchGo(); \n'
    	+ '	  }	\n'
		+ '} \n'
		+ '</script> \n'
		+ '<div>\n'
		+ '   <div id="proQuestSearchBox" style="' + searchBoxStyle + '">\n'
		+ '      ' + logoHtml + '\n'
		+ '  <div id="enterText" style="position: absolute; left: -1000em; width: 20em;">Enter your search terms:</div> \n'
		+ '      <input type="text" id="proQuestSearchInput" size="' + length + '" onKeyPress="handleKeyPress(event,this.form)" />\n'
		+ '      <div style="display:' + buttonStyleDisplay + '"><input type="button"  value="' + searchBtnVal + '" onclick="proQuestSearchGo()"/></div>\n'
		+ '   </div>\n'
		+ '</div>\n'
		+ "<!-- /ProQuest Search Widget -->\n";
		
	document.forms[0].textareaCode.value = widgetCode;
	document.location = "#aWidgetCode";
	Fat.fade_all();
	
} // end createWidget


function getAndValidateDatabases(){
	if((dbInput = document.forms[0].inputDatabases.value) == "") return "-1";
	var dbInputAry = dbInput.split(",");
	var dbList = "";
	for(var i=0;i<dbInputAry.length; i++) {
		if(i==15){
			alert("WARNING: only 15 databases allowed, the list was truncated.");
			break;
		}
		var db = trim(dbInputAry[i]);
		if(isInteger(db)){
			if(dbList != "") dbList += ",";
			dbList += db;
		}else{
			alert("WARNING: invalid databases ID " + db + " skipped.");
		}
	}
	return dbList;
}// getAndValidateDatabases

function getAndValidateSearchTerms(){
	if((termInput = document.forms[0].inputTerms.value) == "") return "";
	var termInputAry = termInput.split(",");
	var termList = "";
	for(var i=0;i<termInputAry.length; i++) {
		var term = encodeURIComponent(trim(termInputAry[i]));
		if(term != ""){
			if(termList != "") termList += "+";
			termList += term;
		}
	}
	return termList;
}

function resetForm() {
	document.forms[0].reset();
	setLogo("pqlogo");
	setLogoLocation("above");
	setButtonLocation("left");
	setBrdColor("#666666");
	setBgColor("#FFFFFF");
	setSearchBoxLength();
} // end resetForm



/* This script and many more are available free online at
 * The JavaScript Source!! http://javascript.internet.com
 * Created by: Mark O'Sullivan :: http://lussumo.com/
 * Jeff Larson :: http://www.jeffothy.com/
 * Mark Percival :: http://webchicanery.com/
  */
function copyToClipboard(text2copy) {
	if (window.clipboardData) {
		window.clipboardData.setData("Text",text2copy);
	} else {
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
	document.getElementById(flashcopier).innerHTML = '';
	var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
	document.getElementById(flashcopier).innerHTML = divinfo;
	}
	alert("Widget code has been copied to your clipboard.");
} // end copyToClipboard


function trim(st) {
	return st.replace(/^\s+|\s+$/g,"");
}



// WWW: http://www.mattkruse.com/
function isInteger(val){
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
}
function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}
function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
}


