function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function close_facebox()
{
    jQuery(document).trigger('close.facebox');
}

function moreOptionFields() {
    counter_option++;
    var newFields = document.getElementById('option_pan').cloneNode(true);
    newFields.id = 'option_container_'+counter_option;
    newFields.style.display = 'block';
    var newField = newFields.childNodes;
    for (var i=0;i<newField.length;i++) {
                 	var theName = newField[i].name
                 	if (theName)
		                 newField[i].name = theName + counter_option;
                        var theID = newField[i].id
                        if (theID)
		                 newField[i].id = theID + counter_option;
    }
    var insertHere = document.getElementById('writeroot_option');
    insertHere.parentNode.insertBefore(newFields,insertHere);

    document.getElementById('counter_option').value = counter_option;
    document.getElementById('option_top').style.visibility = "visible";
    document.getElementById('option_top').style.display = "block";
}  

function removeOption(obj)
{
    obj.parentNode.removeChild(obj);
    //counter_option--;                

    if (counter_option == 0) {
         document.getElementById('option_top').style.visibility = "hidden";
         document.getElementById('option_top').style.display = "none";
    }

    //document.getElementById('counter_option').value = counter_option;
}         

function numbersonly(field, e, dec)
{
    var key;
    var keychar;

    if (window.event) {
         key = window.event.keyCode;
    } else if (e) {
         key = e.which;
    } else {
         return true;
    }

    keychar = String.fromCharCode(key);

    // control keys
    if ((key==null) || (key==0) || (key==8) || 
       (key==9) || (key==13) || (key==27) ) 
    {
        return true;
        // numbers
    } else if ((("0123456789").indexOf(keychar) > -1)) {
        return true;
        // decimal point jump
    } else if (dec && ((keychar == ".") || (keychar == ",") || (keychar == "-"))) {
        field.form.elements[dec].focus();
        return false;
    } else {
        return false;
    }
}

function CheckEmail(email_address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(email_address);
}

function CheckUserName(username) {
   var reg = /^[0-9A-Za-z]+$/;
   return reg.test(username);
}

function CheckZipCode(zip){
    var reg = /^\d{5}$|^\d{5}-\d{4}$/;
    return reg.test(zip);
}

function CheckPhoneNumber(obj)
{
   var fmtStr="";
   var index = 0;
   var LimitCheck;
      
   var number = obj.value;
   number.replace(/[\(\)\.\-\s,]/g, "");

   LimitCheck = number.length;
   while (index != LimitCheck)
   {
         if (isNaN(parseInt(number.charAt(index))))
         { } else {
             fmtStr = fmtStr + number.charAt(index); 
         }
             index = index + 1;
   }

   if (fmtStr.length == 10)
   {
         fmtStr = "(" + fmtStr.substring(0,3) + ") " + fmtStr.substring(3,6) + "-" + fmtStr.substring(6,10);
   } else {
         fmtStr=number;
         //display_error("United States phone numbers must have exactly ten digits.");
         jQuery.facebox('United States phone numbers must have exactly ten digits.');
         return false;
   }

   obj.value = fmtStr;
   return true;   
}

function openfacts(id)
{
   jQuery.facebox({ ajax: 'opendesc?id='+id });
}

function getcoord() {
      if (GBrowserIsCompatible()) {
        address = document.getElementById('store_address').value+" "+document.getElementById('store_city').value+", "+document.getElementById('store_state').value+" "+document.getElementById('store_zip').value;
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        geocoder = new GClientGeocoder();
      }

      if (geocoder && address != "") {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " is not a valid address.");
            } else {
              document.getElementById('point').value = point;
            }
          }
        );
      }      
}

function autosuggest(srchobj) 
{
      if (srchobj.value != "" && srchobj.value.length > 2) {
            var response = seeksrchterms(srchobj.value, srchobj.name);
            var search_box = document.getElementById('s-box');

            if (document.getElementById('div_q')) {
                var e = document.getElementById('div_q');
            } else {               
                var e = document.createElement('div');
                e.id = "div_q";
                e.name = "div_q";
            }
            
            if (response!= "") {
                  e.innerHTML = response;
                  search_box.appendChild(e);
                  e.style.display="block";
                  e.style.visibility = "visible";                  
            } else {
                  e.style.display="none";
                  e.style.visibility = "hidden";                  
            }
       }
}

function setvalue(val, field)
{
       document.getElementById(field).value = val;
       var search_box = document.getElementById('s-box');

       if (document.getElementById('div_q')) {
            search_box.removeChild(document.getElementById('div_q'));
       }      

       document.getElementById('s-form').submit();
}  

function hidetermpan()
{
       var search_box = document.getElementById('s-box');

       if (document.getElementById('div_q')) {
            search_box.removeChild(document.getElementById('div_q'));
       }
}  

function urlencode (str) {
 
       var hash_map = {}, unicodeStr='', hexEscStr='';
       var ret = (str+'').toString();
    
       var replacer = function (search, replace, str) {
       var tmp_arr = [];
       tmp_arr = str.split(search);
       return tmp_arr.join(replace);
       };
    
       // The hash_map is identical to the one in urldecode.
       hash_map["'"]   = '%27';
       hash_map['(']   = '%28';
       hash_map[')']   = '%29';
       hash_map['*']   = '%2A';
       hash_map['~']   = '%7E';
       hash_map['!']   = '%21';
       hash_map['%20'] = '+';
       hash_map['\u00DC'] = '%DC';
       hash_map['\u00FC'] = '%FC';
       hash_map['\u00C4'] = '%D4';
       hash_map['\u00E4'] = '%E4';
       hash_map['\u00D6'] = '%D6';
       hash_map['\u00F6'] = '%F6';
       hash_map['\u00DF'] = '%DF';
       hash_map['\u20AC'] = '%80';
       hash_map['\u0081'] = '%81';
       hash_map['\u201A'] = '%82';
       hash_map['\u0192'] = '%83';
       hash_map['\u201E'] = '%84';
       hash_map['\u2026'] = '%85';
       hash_map['\u2020'] = '%86';
       hash_map['\u2021'] = '%87';
       hash_map['\u02C6'] = '%88';
       hash_map['\u2030'] = '%89';
       hash_map['\u0160'] = '%8A';
       hash_map['\u2039'] = '%8B';
       hash_map['\u0152'] = '%8C';
       hash_map['\u008D'] = '%8D';
       hash_map['\u017D'] = '%8E';
       hash_map['\u008F'] = '%8F';
       hash_map['\u0090'] = '%90';
       hash_map['\u2018'] = '%91';
       hash_map['\u2019'] = '%92';
       hash_map['\u201C'] = '%93';
       hash_map['\u201D'] = '%94';
       hash_map['\u2022'] = '%95';
       hash_map['\u2013'] = '%96';
       hash_map['\u2014'] = '%97';
       hash_map['\u02DC'] = '%98';
       hash_map['\u2122'] = '%99';
       hash_map['\u0161'] = '%9A';
       hash_map['\u203A'] = '%9B';
       hash_map['\u0153'] = '%9C';
       hash_map['\u009D'] = '%9D';
       hash_map['\u017E'] = '%9E';
       hash_map['\u0178'] = '%9F';
    
       // Begin with encodeURIComponent, which most resembles PHP's encoding functions
       ret = encodeURIComponent(ret);
 
       for (unicodeStr in hash_map) {
           hexEscStr = hash_map[unicodeStr];
           ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
       }
    
       // Uppercase for full PHP compatibility
       return ret.replace(/(\%([a-z0-9]{2}))/g, function (full, m1, m2) {
           return "%"+m2.toUpperCase();
       });
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}


