/*
***********************************************************
 bhjs.js
 
 Author : Bruce Hearder <bruce@hearder.com>
 Copyright (c) : 2009, all rights reserved
 
* This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

You may contact the author of BHJS by e-mail at:
bruce@hearder.com
*/

var fn_prefix="Contact0";
var debug=false;

function Right(str, num)
{
  return str.substring(str.length-num);  // pull out right num
}


function fillDefaultValues() 
{
  var qs = location.search.substring(1, location.search.length);
  if (debug) alert(qs);
  var args = qs.split("&");
  var vals = new Object();
  var fn='';
  var fv='';
  for (var i=0; i < args.length; i++) 
  {  
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];	
	fn=nameVal[0];
	fv=nameVal[1];
	if (debug) alert("0 "+fn+" , "+fv);
    fillfield(fn, fv);	
  } 
}

function fillfield(fieldName, value) 
{
  if (value == undefined) 
  {
	 if (debug) alert('Value is undefined');  
     return;
  }
  if (debug) alert("1 "+fieldName+" , "+value);
  
  var fv=value;
  var fn=fieldName;
  if (debug) alert("2 "+fn+" , "+fv);
  var r= fieldName.substring(0,fn_prefix.length);
  if (r!=fn_prefix) fn = fn_prefix+fieldName;
  if (debug)alert("3 "+r+" , "+fn);
  
//  alert("Left hand chars of field name are :" +r);
  for (i=0;i<document.forms.length;i++)
  {
	 var thisfrm = document.forms[i];  
     var elm = thisfrm.elements[fn];
	 if (elm!= null && elm.type != undefined)
      {
	   if (debug) alert("Element : "+fn+" is of type : "+elm.type+", tagname = "+elm.tagName);  
	   if (elm.type=='text' || elm.type=='textarea' || elm.type=='button' || elm.type=='hidden')
	    {
	     elm.value = value;
 	    }
	   else if (elm.type=='select-one' )
	    {
	     setSelectedOption(elm,value);
	    }
	   else if (elm.type=='checkbox' )
	    {
			elm.checked=true;
	    }		
	  }
    else /*Most likley a checkbox or a radio button */
     {
		if (debug) alert("4 "+"radiobuttons");
		var j;
		var d;
		d = thisfrm.getElementsByTagName("input");
		for (j=0;j<d.length;j++)
		{
			 if (d[j].type=="radio")
			 {
				 if (debug) if (debug) alert("Name : "+d[j].name);
				 if (d[j].name == fn && d[j].value == value)
				 {
  			       d[j].checked=true;
				 }
			 }
		}
	 }
  }
}  

function setSelectedOption(select, value) {
  var opts = select.options;
  var l = opts.length;
  if (select == null) return;
  for (var i=0; i < l; i++) {
    if (opts[i].value == value) {
      select.selectedIndex = i;
      return true;
    }
  }
  return false;
}