function loadPage(url) {
	var newwin = window.open(url ,'dagm_Pop', 'alwaysRaised,toolbar=no,scrollbars=yes,top=200,left=200,height=400,width=390,resizable=yes');
	newwin.focus();
}

function clearValue(fe) {

	 if (document.defaultView != null) {
		  var color = document.defaultView.getComputedStyle(fe,'').getPropertyValue("color");	
		  
		  if (color == 'rgb(170, 170, 170)') {
			fe.value = '';	
		  }
	 } 
	 else if(fe.style.color=='#aaa'){
	 	fe.value = '';
	 }
	 fe.style.color = 'black';
}


function wasModified(elem) {
	 var color = elem.style.color;
	// alert("color: " + color);
	 if (color != '' && color != 'rgb(170, 170, 170)' && color != '#aaa') {
		  return true;
	 }
	 return false;
}


function isblank (s) {
	 if (s == null) return true;

	 for (var i = 0; i < s.length; i++) {
		  var c = s.charAt(i);
		  if (( c != ' ')
				&& (c != '\n')
				&& (c != '\t')) return false;
	 }
	 return true;
}

function toggleDisplay (str) {
   if (ts_getElement(str) != null) {
      var sty = ts_getElementStyle(str);
      if (sty.display == 'none') {
         sty.display = '';
      } else {
         sty.display = 'none';
      }
   }
}
function toggleDisplayReturn (str) {
   if (ts_getElement(str) != null) {
      var sty = ts_getElementStyle(str);
      if (sty.display == 'none') {
         sty.display = '';
		 return true;
      } else {
         sty.display = 'none';
		 return false;
      }
   }
}

function ts_getElement(elemID) {
	 var elem = null;
	 
	 if (document.layers != null) {
		  elem = eval('document.layers["' + elemID + '"]');
	 } else if (document.all != null) {
		  elem = eval('document.all.' + elemID + '');
	 } else {
		  elem = document.getElementById(elemID);
	 }
	 return elem;
}

function ts_getElementStyle(elemID) {
//	alert(elemID);
	 if (document.layers != null) {
	 	if(eval('document.layers["' + elemID + '"]') != null){
		  	return eval('document.layers["' + elemID + '"].style');
		}
	 } else if (document.all != null) {
	 	if(eval('document.all.' + elemID ) != null){
		  	return eval('document.all.' + elemID + '.style');
		}
	 } else {
	 	if(document.getElementById(elemID) != null){
		  	return document.getElementById(elemID).style;
		}
	 }
}

function ts_getWindowElement (winname, elemID){
	var elem = null;
	 if (winname.document.layers != null) {
	 //alert('has layers');
		  elem = eval('winname.document.layers["' + elemID + '"]');
	 } else if (winname.document.all != null) {
	 //alert('all');
		  elem = eval('winname.document.all.' + elemID + '');
	 } else {
	 //alert(elemID);
		  elem = winname.document.getElementById(elemID);
	 }
	 return elem;
}

function procPosX(elem) {
    var pos = 0;
    if (elem.offsetParent) {
        while (elem.offsetParent) {
            pos += elem.offsetLeft
            elem = elem.offsetParent;
        }
    } else if (elem.x) {
        pos += elem.x;
	 }
    return pos;
}


function procPosY(elem) {
    var pos = 0;
    if (elem.offsetParent) {
        while (elem.offsetParent) {
            pos += elem.offsetTop
            elem = elem.offsetParent;
        }
    } else if (elem.y) {
        pos += elem.y;
	 }
    return pos;
}

function procCommaStr(str) {
	 var o = str.split(',');
	 return o;
}


// this is unnecessary, but maybe used
function helpClicked (elem) {
	var open = toggleDisplayReturn('help');
	var sty = ts_getElementStyle('helpit');
	if (open) {
		sty.backgroundColor = '#f1f1f1';
	} else {
		sty.backgroundColor = '';
	}
}
// use this instead
function helpClicked () {
	var open = toggleDisplayReturn('help');
	var sty = ts_getElementStyle('helpit');
	if (open) {
		sty.backgroundColor = '#f1f1f1';
	} else {
		sty.backgroundColor = '';
	}
}

function hideElem(elemName){
    ts_getElementStyle(elemName).visibility="hidden";
}

function validZip (zip){
	var zipregex = /^[0-9]{5,5}$/;
	if(zip.match(zipregex) != null){
		return true;
	}
	return false;
}


// *** Following code: Drop-in content box- By Dynamic Drive ***
// For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
// This credit MUST stay intact for use

var ie=document.all
var dom=document.getElementById
var ns4=document.layers
var calunits=document.layers? "" : "px"

var bouncelimit=8 //(must be divisible by 8)
var direction="up"

function initMsgBox(){
if (!dom&&!ie&&!ns4)
return
crossobj=(dom)?document.getElementById("dropin").style : ie? document.all.dropin : document.dropin
scroll_top=(ie)? truebody().scrollTop : window.pageYOffset
crossobj.top=scroll_top-250+calunits
crossobj.visibility=(dom||ie)? "visible" : "show"
dropstart=setInterval("dropin()",50)
}

function dropin(){
scroll_top=(ie)? truebody().scrollTop : window.pageYOffset
if (parseInt(crossobj.top)<100+scroll_top)
crossobj.top=parseInt(crossobj.top)+40+calunits
else{
clearInterval(dropstart)
bouncestart=setInterval("bouncein()",50)
}
}

function bouncein(){
crossobj.top=parseInt(crossobj.top)-bouncelimit+calunits
if (bouncelimit<0)
bouncelimit+=8
bouncelimit=bouncelimit*-1
if (bouncelimit==0){
clearInterval(bouncestart)
}
}

function dismissbox(){
if (window.bouncestart) clearInterval(bouncestart)
crossobj.visibility="hidden"
return false;
}

function truebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function get_cookie(Name) {
var search = Name + "="
var returnvalue = ""
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function dropornot(){
if (get_cookie("droppedin")==""){
window.onload=initMsgBox
document.cookie="droppedin=yes"
}
}
//dropornot()
			
		
   

		

function submitHome() {
	var msg = '';
	if(!shouldSubmit){ // this interacts with the srcipt in CatField.script
		// set the value for the category to the highlighted
		setItem();
		_elem.blur();
		return false;
	}
	var f = document.forms['listform'];
	
	var catnameVal = f.catf.value;
	catnameVal = trimCatname(catnameVal);
	if(f.catno.value=='-1' || f.catno.value==''){
		for(i=0; i<cats.length; i++){
			if(catnameVal.toLowerCase() == cats[i][1].toLowerCase()){
				f.catno.value = cats[i][0];
				catnameVal = cats[i][1];
				f.catf.value = catnameVal;
				break;
			}
		}
	}
	
	if(f.catname.value==''){
		f.catname.value=catnameVal;
	}
	 if ((isblank(f.catno.value) && f.catname.value=='') || !wasModified(f.catf) || isblank(f.catf.value)) {
	 	//	alert(f.catno.value + "--" + f.catname.value);
		  msg = "Please tell us what you want";
	 }
	 else if (!wasModified(f.zip) || isblank(f.zip.value)) {
		  msg = "ZIP code is required";
	 }

	 if (!wasModified(f.details)) {
		  f.details.value = '';
	 }
	 if(msg!=''){
	 	alert(msg);
	 	return false;
	 }
	 else {
	 	f.submit();
		 return true;
	 }
}



function submitListingsConfirm(f) {
	 if (!f.terms.checked) {
		  alert("You have to accept the Terms and Conditions to confirm your listings.");
		  return false;
	 }
	 return true;
}

function submitRequestConfirm(f) {
	 var msg = '';
	 if (f.captcha != null && f.captcha.value == '') {
		  msg += 'Please enter the word as shown in the image.\n';
	 }
	 if (!f.terms.checked) {
		  msg += 'You have to accept the Terms and Conditions to confirm your request.';
	 }
	 if (msg == '') {
		  return true;
	 } else {
		  alert(msg);
		  return false;
	 }
}

function submitOk(f) {
	 var bool = true;
	 if (f.deact.checked) {
		  bool = confirm("While we will do our best to let the Vendors know that your request has been deactivated, if your request has already been matched you may continue to be contacted by Vendors");
	 }
	 return bool;
}

		

   


dojo.require("dojo.event");

var ESC=27;
var TAB=9;
var ALT=18;

var UP=38;
var DOWN=40;
var LEFT=37;
var RIGHT=39;

var ENTER=13;

var _to;
var _ids, _names;

var _catvlue;
var _acdiv, _acdivBg;

var _sel = -1;
var _prevsel = -1;

var _elem = null;

var keep = false;
var iHeight = 16;

var boxHeight = 200;
var boxWidth = 340;

var shouldSubmit = true;
var disposed = false;

function handleOnFocus(evt){
	 setElement(evt);
	 initBox();
	 /* for changing the category after coming back from the request confirm page and 
	 entering custom category name:
	 */

	var f = document.forms['listform'];
	
	if(f != null && f.catno != null){
		//alert(f);
		f.catno.value='';
	}

	// all set
	 _to = setTimeout('suggestComplete()', 30);
}

function handleOnBlur(evt){
	if(!keep && !disposed){
		disposeBox();
	}
	
	var lstfm = document.forms.listform;
	var selfm = document.forms.selForm;
	if(lstfm != null && lstfm.catf != null){
		lstfm.catname.value=lstfm.catf.value;
	}
	else if(selfm != null){
		lstfm.catname.value=selfm.catf.value;
	}
	
}

function disposeBox() {
	// category aliases
	if(_elem == null || _elem.value == null){
		return; // don't do anything if those are null. They can be null
		// if there's any redundant calls to this method - happening with
		// IE
	}


	// setElement(evt);
	unknownCat();

	 // rm box and reset vars
	 removeBox();
	 if (_to) {
		  clearTimeout(_to);
	 }
	 disposed = true;
}


// evt = (evt) ? evt : ((window.event) ? window.event : "");

function handleOnKeyDown(evt){
	 setElement(evt);

	 var pK = document.all? evt.keyCode:evt.which; 
	 if (pK == ENTER) {
		  setItem();
		  _elem.blur();
		  removeBox();
		  //document.forms.listform.zip.focus();
	 } else if (pK == TAB) {
		  setItem();
		  removeBox();
		 // document.forms.listform.zip.focus();
	 } else {
		  // var pKs = String.fromCharCode(pK).toLowerCase();
		  if (!specialKey(pK)) {
				handleOnFocus(evt);
		  }
	 }
}



function _acMouseOut(obj) {
}

function _acMouseDown(obj) {
	 setItem(getSelectedIndex(obj));
	 disposeBox();
	// document.forms.listform.zip.focus();
}

function _acMouseOver(obj) {
	 _sel = getSelectedIndex(obj);
	 selectItem(_sel);
}


function unknownCat() {
	 var haveCat = false;
	 var vlue = _elem.value;
	 for (var j = 0; j < cats.length; j++) {
		  var str = cats[j][1];
		  var strlc = str.toLowerCase();
		  vlue = vlue.toLowerCase();
		  if (vlue == strlc) {
				haveCat = true;
		  }
	 }
	 if (!haveCat) {
		  document.forms.listform.catno.value = -1;
		  document.forms.listform.catname.value = vlue;
//		  var msgsty = ts_getElementStyle("unknownCat");
//		  msgsty.visibility = 'visible';
	 }
}

var _divB = "<div class='acboxItem' "
	 + "onmousedown='_acMouseDown(this)' "
	 + "onmouseover='_acMouseOver(this)' "
	 + "onmouseout='_acMouseOut(this)'>";
var _divE = "</div>";
var _spanB = "<span class='acboxItemLabel'>";
var _spanE = "</span>";

function suggestComplete() {
	if(_elem == null || _elem.value == null){
		removeCatGrpMsg();
		return;
	}
	 var vlue = _elem.value;
	 var eid = _elem.id;

	 if (vlue.length < 3 || vlue == _catvlue) {
		  _catvlue = null;
		  removeBox();
		  return;
	 }
	
	 removeCatGrpMsg();
	 var html = '';
	 if (_acdiv != null) {
	 
		  var k = 0;
		  for (var j = 0; j < cats.length; j++) {
				var str = cats[j][1];
				var strlc = str.toLowerCase();
				vlue = vlue.toLowerCase();
				var i = strlc.indexOf(vlue.toLowerCase());

				if (i == 0) {
					 var id = cats[j][0];
					 _ids[k] = id;
					 _names[k] = str;
					 k++;
					 
					 html += _divB + _spanB + str + _spanE + _divE;
				}
		  }
		  if (k > 10) {
				_acdiv.style.height = boxHeight + 'px';
				_acdiv.style.overflow = 'auto';
		  } else {
				_acdiv.style.height = (k*iHeight + 2)+'px';
		  }
		  _acdiv.innerHTML = html;

		  _acdivBg.style.top = _acdiv.style.top;
		  _acdivBg.style.left = _acdiv.style.left;
		  _acdivBg.style.height = _acdiv.style.height;
		  _acdivBg.style.width = boxWidth + 'px';
		  _acdivBg.style.zIndex = _acdiv.style.zIndex - 1;
		  _acdivBg.style.display = 'inline';


	 }
	 _catvlue = vlue;
	 disposed = false;
}


function initBox () {
	 if (_acdiv == null) {
		  _acdiv = ts_getElement('acbox');
		  _acdivBg = ts_getElement('acboxBg');

		  var y = procPosY(_elem) + _elem.offsetHeight + 1;
		  var x = procPosX(_elem);

		  _acdiv.style.top = y + 'px';
		  _acdiv.style.left = x + 'px';
		  _acdiv.style.width = '340px';
		  _acdiv.style.height = 'auto';
		  _acdiv.style.paddingLeft = '10px';
		  _acdiv.style.paddingRight = '10px';
		  _acdiv.style.display = 'inline';

		  _ids = new Array();
		  _names = new Array();
		  var msgsty = ts_getElementStyle("unknownCat");
		  msgsty.visibility = 'hidden';
	 }
	 disposed = false;
}

function removeCatGrpMsg(){
	if(document.forms['relcats']!=null){
		document.forms['relcats'].reltxt.value = '';
		txt = '';
		elem = ts_getElement('catamsg');
		elem.innerHTML = txt;
	}
}

function removeBox() {
    if (_acdiv != null) {
		  _acdiv.style.display = 'none';
		  _acdiv.innerHTML = "";

		  _acdivBg.style.display = 'none';
	 }
	 _acdiv = null;
	 _acdivBg = null;

	 resetVars();
	 disposed = true;
}

function resetVars() {
    _ids = null;
    _names = null;
	 _elem = null;
	 _sel = -1;
	 _prevsel = -1;
}



function getSelectedIndex(obj) {
    var sel = 0;
	 var s = _acdiv.childNodes.length;
    for (var i = 0; i < s; i++) {
        if (obj == _acdiv.childNodes[i]) {
            sel = i;
            break;
        }
    }

    return sel;
}

function selectItem(itno) {
	 var nl = _acdiv.childNodes.length;
    if (nl > 0) {
        if (_prevsel >= 0) {
            var prev = _acdiv.childNodes[_prevsel];
            prev.className = "acboxItem";
        }

        if (itno >= 0) {
            if (nl > 0) {
                var cur = _acdiv.childNodes[itno];
                cur.className = "acboxItemHi";
            }
				_sel =_prevsel = itno;
        }
    }
}

function setElement(evt) {
	 _elem = (evt.target) ? evt.target : evt.srcElement;
}

var relatedCatsData = [];

function setItem() {
	 if (_sel >= 0) {
	 	var catno = _ids[_sel];
		  document.forms.listform.catno.value = catno;
		  document.forms.listform.catname.value = _names[_sel];

		  _elem.value = _names[_sel];
		  shouldSubmit = true;
		  
		  var txt = '';
		  var elem = '';
		  
		  if (ts_getElement('catamsg') != null) {
				relatedCatsData = getGroupedCats(catno);
				var relatedTxt = '';
				var related = [];
				
				if(relatedCatsData.length > 0){
					related = relatedCatsData[0];
				}
				
				for (var k = 0; k < related.length; k++) {
					 if (k > 0) {
						  relatedTxt += ', ';
					 }
					 relatedTxt += related[k];
				}
				if(related.length>0){
					document.forms['relcats'].reltxt.value = relatedTxt;
					txt = 'By choosing this category you will also be listed in ' + related.length + ' related categories (<a href="javascript:showRelatedCats(\'' + relatedTxt + '\');">see all</a>) at no extra charge.';
				
					elem = ts_getElement('catamsg');
					elem.innerHTML = txt;
				}
				else{
					removeCatGrpMsg();
				}
		  }
	 }
}

var ie4 = (document.all) ? 1 : 0;

function showRelatedCats(relTxt){
	var newwin = window.open('relcats.html', 'RelatedCats', 'width=390, height=185, toolbars=no, resizable=yes, scrollbars=yes');
	newwin.focus();
	
}

function hideGrpBox(){
	var ele = ts_getElement("clickbox");
	ele.style.display=none;
}

function setWintxt() {
	var txtele = ts_getWindowElement(winn, relcatEle);
	//alert("Got the element: " + txtele);

	txtele.setRelCatText(relTxt);
}

function trimCatname(cname){
	cname = dojo.string.trim(cname);
	cname = cname.replace(/^A\s+|the\s+|An\s+/ig, '');
	cname = dojo.string.trim(cname);
	return cname;
}

var grp;

function getGroupedCats(catno) {
	var cat = null;
	for (var j = 0; j < cats.length; j++) {
		  var ocat = cats[j][0];
		  if (ocat == catno) {
		  	 cat = cats[j];
		  	 break;
		  }
	 }
	
	if(cat == null) {
		return;
	}
	
	 grp = cat[2]; //alert("the group: " + grp);
	 var lst = [];
	 var nolst = [];
	 var i = 0;
	 for (var j = 0; j < cats.length; j++) {
		  var ogrp = cats[j][2];
		  if (grp == ogrp) {
				var nme = cats[j][1];
				if (cat[1] == nme) {
					 continue;
				}
				lst[i] = nme;
				nolst[i] = (cats[j][0]);
				i++;
		  }
	 }
	 
	 var relCatsData = [];
	 relCatsData[0] = lst;  // add the array of cat names in the group
	 relCatsData[1] = nolst; // add the array of cat nos in the group
	 return relCatsData;
}


function specialKey(keycode) {
    switch (keycode) {
    case LEFT:
    case UP:
		  _sel--;
        if (_sel < 0) {
           sel = 0;
        }
        selectItem(_sel);
		  return true;
    case RIGHT:
    case DOWN:
        _sel++;
		  if (_sel >= _ids.length) {
				_sel = _ids.length - 1;
		  }
		  selectItem(_sel);
		  return true;
    case ENTER:
		  break;
    case ESC:
        removeBox();
		  return true;
    case TAB:
		  break;
    case ALT:
    default:
	 return false;
    }
}


function initListeners() {
	 var ffield = ts_getElement('catf');
	 dojo.event.connect(ffield, "onblur", "handleOnBlur");
	 dojo.event.connect(ffield, "onfocus", "handleOnFocus");
	 dojo.event.connect(ffield, "onkeydown", "handleOnKeyDown");
}

