/*
**
**  Custom script for CapEdge 2.0, Modified from PageHeaderJS
**
Todo:
Merge with PageHeaderJS by dedicating checksearch to Notes and checksearchcapedge to CapEdge
Get checksearchcapedge to work with Notes version of trim or convert Notes version to trimString
Get checksearchcapedge to work with Notes version of checkinvalid
*/
/*
**  added var sWebDBName to match what is happening in main kemet.com web page
*/
var sWebDbName= 'http://www.kemet.com/kemet/web/homepage/kechome.nsf/';

function locatelink(tolink){
	location = tolink
}
function SearchOnWords() {
//	var doc=document.forms['headerwordsearch'];
	var doc=document.forms['headersearch'];
	var tmpSearch = doc.wordtosearch.value;
	if(tmpSearch == "" )  {
		//location='http://www.kemet.com/kemet/web/homepage/kechome.nsf/vabypagename/searchins';
		//alert('Please enter a word or term to search');
		doc.wordtosearch.focus();
	} else {
		tmpSearch = trimString(tmpSearch)
		//alert('searching on words ' + tmpSearch );
		sitesearch(tmpSearch)
	}
}

/*
function trim(inputstringTrim) {
*/
function trimString(inputstringTrim) {
   fixedTrim = "";
   lastCh = " ";
   for (x=0; x < inputstringTrim.length; x++) {
      ch = inputstringTrim.charAt(x);
      if ((ch != " ") || (lastCh != " ")) {
         fixedTrim += ch; 
      }
      lastCh = ch;
   }
   if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
      fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1);
   }
   return fixedTrim;
}

function sitesearch(searchfield) {
	location=sWebDbName+ 'vaWebSearchView?SearchView&Query=' + escape(searchfield) +
                     '&SearchOrder=1&SearchMax=20&SearchWV=FALSE&SearchThesaurus=TRUE';
}

function checksearch(searchfield, searchtype) {
//   for (i=0;i<searchtype.length;i++){
//      if (searchtype[i].checked==true)
//         searchtype=searchtype[i];
//   }

   tmpSearch = trimString(searchfield.value)
   if(tmpSearch == "Enter a Part Number") {
      location=sWebDbName+ 'weben/products';
      return false;
   }
   if(tmpSearch == "" | tmpSearch == "Keyword(s)" | tmpSearch == "Know your Part Number")  {
      location=sWebDbName+ 'weben/searchins';
      return false;
   }
   //New multi-contextual directing part search to CapEdge (fjt - 02/18/2008)
//  if(searchtype.value=='part number, cross, or description') {
  if(searchtype=='part number, cross, or description') {
   location='http://capacitoredge.kemet.com/capedge2/capreview.jsp?mode=parse&sleuthInput=' + tmpSearch;
   return false
   }
   
   if(checkinvalid(searchfield)) {
        sitesearch(searchfield.value);
   } else {
        location=sWebDbName+ 'weben/searchinvalid';
   }
    return false;
}

function checksearchcapedge(searchfield, searchtype) {
sWebDbName = "http://dmz01-web04.kemet.com/kemet/web/homepage/kechome.nsf/";

   tmpSearch = trim(searchfield.value)
   if(tmpSearch == "Enter a Part Number") {
      location=sWebDbName+ 'weben/products';
      return false;
   }
   if(tmpSearch == "" | tmpSearch == "Keyword(s)" | tmpSearch == "Know your Part Number")  {
      location=sWebDbName+ 'weben/searchins';
      return false;
   }
   //New multi-contextual directing part search to CapEdge (fjt - 02/18/2008)
//   if(searchtype.value=='part number, cross, or description') {
  if(searchtype=='part number, cross, or description') {
   location='http://capacitoredge.kemet.com/capedge2/capreview.jsp?mode=parse&sleuthInput=' + tmpSearch;
   return false
   }
   
   if(checkinvalid(searchfield)) {
        sitesearch(searchfield.value);
   } else {
        location=sWebDbName+ 'weben/searchinvalid';
   }
    return false;
}

function fcs(obj,val,nVal){if(obj.value==val){obj.value=nVal;}}

function locatelink(tolink){
	location = tolink
}

function checkinvalid(searchfield) {

   tmpSearch = searchfield.value;
 /* 
   tmpSearch = searchfield;
   */ 
   var regExpOK = /\Wfield\W|\Wtopic\W|\Wtermweight\W|\Wexactcase\W|\Wcontains\W|\Wsentence\W|\Wparagraph\W/i;
   var regExp1 = /\bfield\b|\btopic\b|\btermweight\b|\bexactcase\b|\bcontains\b|\bsentence\b|\bparagraph\b/i;
   var regExp2 =  /[{}#%&=\<\>\[\]]/; // invalid values
   var regExpNeedQt = /[()]/;  // need quotes
   var regExpTestQt = /^"\b/;  // test for quote at the beginning
   if(typeof regExp1.source != 'undefined' ) { // check to make sure regexp work
     if (regExpOK.test(tmpSearch)) {
        // skip words if quotes around them
     } else if (regExp1.test(tmpSearch) || regExp2.test(tmpSearch)) {
        return false;
     }
     if (regExpTestQt.test(tmpSearch)) {
        // continue, quote found at the beginning
     } else if (regExpNeedQt.test(tmpSearch)) {
/*
       searchfield = '"' + tmpSearch + '"';
*/
       searchfield.value = '"' + tmpSearch + '"';
}
   }
   return true; // return true if not already exited at this point
   
}


function checkKEsearch() {
	defaultloc = 'http://www.kemet.com/kemet/web/homepage/kfbk3.nsf/vaFeedbackFAQ?OpenView&Start=1&Count=10&t=KEHomeTechSearch';
	family = document.forms['kewordform'].prodfam
	kewords = document.forms['kewordform'].kewords
	for (i=0; i < family.length; ++i) {
		if (family.options[i].selected) {
			ProdSelText = family.options[i].text;
			break;
		}
	}
	kesearch = trimString(kewords.value)
	if(kesearch == "Keyword(s)")  {
		location=defaultloc;
		return false;
	}
	if (ProdSelText == 'All Products' & kesearch == "") {
		location=defaultloc;
		return false;
	}
	if (kesearch != "" & !checkinvalid(kewords)) {
		location='http://www.kemet.com/kemet/web/homepage/kechome.nsf/vabypagename/searchinvalid';
		return false;
	}
	qString1 = "";
	qString2 = "";
	if (ProdSelText != 'All Products') {
		qString1 = '(FIELD+feedbackProduct+contains+' + ProdSelText + ')';
	}
	if (kesearch != "") {
		qString2 = kesearch;
	}
	if (qString1 != "" & qString2 != "") {
		qString = qString1 + "+AND+" + qString2
	} else if (qString2 == "") {
		qString = qString1
	} else {
		qString = qString2;
	}
	var re = / /g
	qString = qString.replace(re,"+");
	location='http://www.kemet.com/kemet/web/homepage/kfbk3.nsf/vaFeedbackFAQ?Searchview&Query=' + qString +  '&Start=1&Count=10&SearchOrder=1&SearchThesaurus=TRUE&t=KEHomeTechSearch';
	return false;
}

function openHelpWindow(url, theWidth, theHeight, putMenu) {
	myHelp = window.open(url,"helpWin",'toolbar=0,location=0,directories=0,status=0,menubar=' + putMenu + ',scrollbars=1,resizable=0,width=' + theWidth + ',height=' + theHeight + ',screenX=0,screenY=0')
}

function getItem(id) {
 // IE5+, Mozilla, NS5
 if (document.getElementById)
   return document.getElementById(id);
// IE4
if (document.all)
   return document.all.item(id);
// NS4
   return eval("document."+ id + ".document." + id);
}

function replaceSubstring(sourceList,fromList,toList) {
    for (var i=0; i<sourceList.length; i++) { 
        if (sourceList.substring(i,i+fromList.length) == fromList) { 
        sourceList = sourceList.substring(0,i)+toList+sourceList.substring(i+fromList.length,sourceList.length)
        } 
    } 
    return sourceList
}

// cas - 20090806 - new script to check Search Boxes and replace text or not
//                                 depending on what actions were taken
function checkSearchBox(sForm, sFunc) {
	//  Set default Search Bar Text 
	var defPNText =  'Search here for KEMET PN, Competitor PN, Partial PN, or Part Description';
	var defKWText = 'Search here for General Keywords, Part Series, or Corporate Information';
	// used on load of form to set both Search Box areas to the default text and color
	if (sForm == 'both' && sFunc == 'load') {
		document.headersearchform.elements['wordtosearch'].blur();
		document.headersearchform2.elements['wordtosearch'].blur();
		document.headersearchform.elements['wordtosearch'].value = defPNText;
		document.headersearchform2.elements['wordtosearch'].value = defKWText;
		document.headersearchform.elements['wordtosearch'].className = 'globalNavSearch';
		document.headersearchform2.elements['wordtosearch'].className = 'globalNavSearch';
		}
	
	if (sFunc == "focus" && sForm == 'headersearchform') {
		if (document.forms[sForm].wordtosearch.value == defPNText) {
			document.forms[sForm].wordtosearch.value = '';
			document.forms[sForm].wordtosearch.className = 'globalNavSearchFocus';
			}
		}	
				
	if (sFunc == "focus" && sForm == 'headersearchform2')  {
		if (document.forms[sForm].wordtosearch.value == defKWText) {
			document.forms[sForm].wordtosearch.value = '';
			document.forms[sForm].wordtosearch.className = 'globalNavSearchFocus';
			}
		}
		
	if (sFunc == 'blur' && sForm == 'headersearchform') {
		if (document.forms[sForm].wordtosearch.value == '') {
			document.forms[sForm].elements['wordtosearch'].value = defPNText;
			document.forms[sForm].wordtosearch.className = 'globalNavSearch';
			}
		}
		
	if (sFunc == 'blur' && sForm == 'headersearchform2') {
		if (document.forms[sForm].wordtosearch.value == '') {
			document.forms[sForm].elements['wordtosearch'].value = defKWText;
			document.forms[sForm].wordtosearch.className = 'globalNavSearch';
			}
		}
}
