var DHTML = (document.getElementById || document.all || document.layers);
var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

var invalid_zip = 0;

function smallwindow(url)
{
  window.open(url, '_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=800');
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

function trimnondigits(str)
{
  return str.replace(/[^\d]+/g, '');
}




function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocusId(valfield)
{
  if (!document.getElementById) 
    return true;  
  var elem = document.getElementById(valfield);
  global_valfield = elem;
  setTimeout( 'setFocusDelayed()', 100 );
}


function setfocus(valfield)
{
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function setfldclass(fld, msgtype)
{
	var elem = document.getElementById(fld);
	elem.className = msgtype; 
}

function msgclass(fld,     
             msgtype) 
{
  var elem = document.getElementById(fld);
  elem.className = msgtype;
  return;
}

function msg(fld,     
             msgtype, 
             message) 
{
 
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   
}

function pushtofield(fld, fld2id)
{
  if (!document.getElementById) 
    return true;  
  var fld2 = document.getElementById(fld2id);
  if (emptyString.test(fld2.value)) 
    fld2.value = fld.value;    
}



var proceed = 2;  

function commonCheck    (valfield,   
                         infofield,  
                         required)   
{
  if (!document.getElementById) 
    return true;  
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  
  if (elem.firstChild.nodeType != node_text) return true;  

  if (emptyString.test(valfield.value)) 
  {
    if (required) 
	{
      msg (infofield, "error", "(required)");  
	  valfield.className = 'fielderror';
      return false;
    }
    else 
	{
      msg (infofield, "warn", ""); 
      return true;  
    }
  }
  return proceed;
}


function validateCoverage(fields, boxid, infofield, oldclass) {
	var found = 0;
	while(field = fields.pop()) {
		if(field.checked == true) {
			found++;
		}
	}

	var boxelem = document.getElementById(boxid);
	if(!found) {
		boxelem.className = 'fielderror';
		msg(infofield, 'error', '(check at least one)');
		return false;
	}
	boxelem.className = '';
	msg(infofield, 'warn', '');
	return true;
}


function validateFirstname(valfield, infofield, oldclass) {
	var stat = commonCheck (valfield, infofield, true);
	if(stat != proceed)
		return stat;

	var value = valfield.value.replace(/[^a-zA-Z]*/g, '');
	if(value.length < 2) {
		msg(infofield, 'error', '(2 letters required)');
		valfield.className = 'fielderror';
		return false;
	}
	valfield.className = oldclass;
	msg (infofield, "warn", "");  
	return true;
}


function validateLastname(valfield, infofield, oldclass) {
	return validateFirstname(valfield, infofield, oldclass);
}


function validatePresent(valfield,  
                         infofield, oldclass ) 
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;
	valfield.className = oldclass;
  msg (infofield, "warn", "");  
  return true;
}

function validateCond2Present(condition, valfield,  
                         infofield, oldclass ) 
{
	if(!condition) 
	{
		valfield.className = oldclass;
		msg (infofield, "warn", "");
		return true;
	}
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;
	valfield.className = oldclass;
  msg (infofield, "warn", "");  
  return true;
}

function validateCond2SelectPresent(condition, valfield, infofield, boxid, oldclass ) 
{
	var elem = document.getElementById(boxid);
	if(!condition)
	{
		if (emptyString.test(valfield.options[valfield.selectedIndex].value)) 
		{
			elem.className = 'fielderror';
	  		msg (infofield, "error", "(required)");
      		return false;
    	}
    	else 
		{
			elem.className = oldclass;
			msg (infofield, "warn", "");
      		return true;  
    	}
	}
	else
	{
	 	elem.className = oldclass;
		msg (infofield, "warn", "");
      	return true;
	}   
}




function validateCond2SelectDOBPresent(condition, valfield, valfield2, valfield3, boxid, boxid2, boxid3, infofield, oldclass, validateall) 
{
	var errs=0;
	var returnval=true;
	var elem = document.getElementById(boxid);
	var elem2 = document.getElementById(boxid2);
	var elem3 = document.getElementById(boxid3);
	var leap = valfield3.options[valfield3.selectedIndex].value % 4 ? 28 : 29;
	
	if ((!condition) && (emptyString.test(valfield.options[valfield.selectedIndex].value) || 
		emptyString.test(valfield2.options[valfield2.selectedIndex].value) ||
		emptyString.test(valfield3.options[valfield3.selectedIndex].value) ||
		(validateall &&
		((valfield.options[valfield.selectedIndex].value=='02' && (valfield2.options[valfield2.selectedIndex].value > leap )) ||
		((valfield.options[valfield.selectedIndex].value=='04' ||  valfield.options[valfield.selectedIndex].value=='06'|| valfield.options[valfield.selectedIndex].value=='09'|| valfield.options[valfield.selectedIndex].value=='11')
			 && (valfield2.options[valfield2.selectedIndex].value > 30 )))))
		)
	{
		
		
		if(validateall)
		  {
		  	if (emptyString.test(valfield2.options[valfield2.selectedIndex].value))
			{
				elem2.className = 'fielderror';
				returnval=false;
			}
			else elem2.className = oldclass;	
			if (emptyString.test(valfield3.options[valfield3.selectedIndex].value)) 
			{
				elem3.className = 'fielderror';
				returnval=false;
			}	
			else elem3.className = oldclass;	
		  }
		if (emptyString.test(valfield.options[valfield.selectedIndex].value)) 
		{
		  elem.className = 'fielderror';
	      return false;
	    }
		 else
		{
		  elem.className = oldclass;
	        
	    }
		if((validateall &&
		((valfield.options[valfield.selectedIndex].value=='02' && (valfield2.options[valfield2.selectedIndex].value > leap )) ||
		((valfield.options[valfield.selectedIndex].value=='04' ||  valfield.options[valfield.selectedIndex].value=='06'|| valfield.options[valfield.selectedIndex].value=='09'|| valfield.options[valfield.selectedIndex].value=='11')
			 && (valfield2.options[valfield2.selectedIndex].value > 30 ))))
			)
			 {
			 	msg (infofield, "error", "invalid date");
				returnval=false;
			 }
			 else
			 {
				 msg (infofield, "error", "(required)");
			 }
		return returnval;	
	} 
	else
	{
		msg (infofield, "warn", "");
		elem.className = oldclass;
		if(validateall)
		{
			elem2.className = oldclass;
			elem3.className = oldclass;
		}	
      	return true;
	}
    
}











function validateCondPresent(condfield, valfield,  
                         infofield, oldclass ) 
{
	if(condfield[0].checked) 
	{
		valfield.className = oldclass;
		msg (infofield, "warn", "");
		return true;
	}
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;
	valfield.className = oldclass;
  msg (infofield, "warn", "");  
  return true;
}

function validateCondRvPresent(condfield, valfield,  
                         infofield, oldclass ) 
{
	if(condfield[1].checked) 
	{
		valfield.className = oldclass;
		msg (infofield, "warn", "");
		return true;
	}
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;
	valfield.className = oldclass;
  msg (infofield, "warn", "");  
  return true;
}

function validate3Checked(v1, v2, v3, boxid, infofield, oldclass ) 
{
	var boxelem = document.getElementById(boxid);
	 if (v1.checked || v2.checked || v3.checked) 
	{
	  boxelem.className = '';
	  setfldclass(infofield, oldclass);
      return true;  
    }
    else 
	{
	  boxelem.className = 'fielderror';
	  setfldclass(infofield, 'error');
      return false;
    }
}
function validate4Checked(v1, v2, v3, v4, boxid, infofield, oldclass ) 
{
	var boxelem = document.getElementById(boxid);
	 if (v1.checked || v2.checked || v3.checked || v4.checked) 
	{
	  boxelem.className = '';
	  setfldclass(infofield, oldclass);
      return true;  
    }
    else 
	{
	  boxelem.className = 'fielderror';
	  setfldclass(infofield, 'error');
      return false;
    }
}

function validatePurePresent(valfield, oldclass) 
{
    if (emptyString.test(valfield.value)) 
	{
	  valfield.className = 'fielderror';
      return false;
    }
    else 
	{
	  valfield.className = oldclass;
      return true;  
    }
}

function validateSelectPresent(valfield, infofield, boxid, oldclass) 
{
	var elem = document.getElementById(boxid);
	if (emptyString.test(valfield.options[valfield.selectedIndex].value)) 
	{
		
		elem.className = 'fielderror';
	  msg (infofield, "error", "(required)");
      return false;
    }
    else 
	{
		elem.className = oldclass;
	  msg (infofield, "warn", "");
      return true;  
    }   
}

function validateRadio(radiofield, boxid ) 
{
	var elem = document.getElementById(boxid);
	if(radiofield[0].checked || radiofield[1].checked) 
	{
		elem.className = '';
		return true;
	}
  	elem.className = 'fielderror'; 
  	return false;
}

function validateRadio2(radiofield, boxid ) 
{
	var elem = document.getElementById(boxid);
	if(radiofield[0].checked || radiofield[1].checked || radiofield[2].checked || radiofield[3].checked) 
	{
		elem.className = '';
		return true;
	}
  	elem.className = 'fielderror'; 
  	return false;
}

function validateDOB(valfield, valfield2, valfield3, boxid, boxid2, boxid3, infofield, oldclass, validateall) 
{
	var errs=0;
	var returnval=true;
	var elem = document.getElementById(boxid);
	var elem2 = document.getElementById(boxid2);
	var elem3 = document.getElementById(boxid3);
	var leap = valfield3.options[valfield3.selectedIndex].value % 4 ? 28 : 29;
	
	if (emptyString.test(valfield.options[valfield.selectedIndex].value) || 
		emptyString.test(valfield2.options[valfield2.selectedIndex].value) ||
		emptyString.test(valfield3.options[valfield3.selectedIndex].value) ||
		(validateall &&
		((valfield.options[valfield.selectedIndex].value=='02' && (valfield2.options[valfield2.selectedIndex].value > leap )) ||
		((valfield.options[valfield.selectedIndex].value=='04' ||  valfield.options[valfield.selectedIndex].value=='06'|| valfield.options[valfield.selectedIndex].value=='09'|| valfield.options[valfield.selectedIndex].value=='11')
			 && (valfield2.options[valfield2.selectedIndex].value > 30 ))))
		)
	{
		
		
		if(validateall)
		  {
		  	if (emptyString.test(valfield2.options[valfield2.selectedIndex].value))
			{
				elem2.className = 'fielderror';
				returnval=false;
			}
			else elem2.className = oldclass;	
			if (emptyString.test(valfield3.options[valfield3.selectedIndex].value)) 
			{
				elem3.className = 'fielderror';
				returnval=false;
			}	
			else elem3.className = oldclass;	
		  }
		if (emptyString.test(valfield.options[valfield.selectedIndex].value)) 
		{
		  elem.className = 'fielderror';
	      return false;
	    }
		 else
		{
		  elem.className = oldclass;
	        
	    }
		if((validateall &&
		((valfield.options[valfield.selectedIndex].value=='02' && (valfield2.options[valfield2.selectedIndex].value > leap )) ||
		((valfield.options[valfield.selectedIndex].value=='04' ||  valfield.options[valfield.selectedIndex].value=='06'|| valfield.options[valfield.selectedIndex].value=='09'|| valfield.options[valfield.selectedIndex].value=='11')
			 && (valfield2.options[valfield2.selectedIndex].value > 30 ))))
			)
			 {
			 	msg (infofield, "error", "invalid date");
				returnval=false;
			 }
			 else
			 {
				 msg (infofield, "error", "(required)");
			 }
		return returnval;	
	} 
	else
	{
		msg (infofield, "warn", "");
		elem.className = oldclass;
		if(validateall)
		{
			elem2.className = oldclass;
			elem3.className = oldclass;
		}	
      	return true;
	}
    
}



function validateEmail  (valfield,   
                         infofield, 
                         required, oldclass)   
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  
  var email = /^[^\s@]+@[^\s@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "error", "(not a valid e-mail address)");
	valfield.className = 'fielderror';
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) { 
    msg (infofield, "warn", "(unusual e-mail address - check if correct)");
  } else {
    msg (infofield, "warn", "");
	valfield.className = oldclass;
  return true;
  }
}

function validateAddress    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         oldclass)   // true if required
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var addRE = /[^\s]+\s+[^\s]+/ ;
  if (!addRE.test(tfld)) {
    valfield.className = 'fielderror';
    msg (infofield, "error", "invalid address");
    setfocus(valfield);
    return false;
  }
  
  valfield.className = oldclass;
  msg (infofield, "warn", "");
  return true;
}



function validateNum    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         oldclass, required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var ageRE = /^[0-9]{1,5}$/
  if (!ageRE.test(tfld)) {
  valfield.className = 'fielderror';
    msg (infofield, "error", "must be a number");
    setfocus(valfield);
    return false;
  }
	valfield.className = oldclass;
  msg (infofield, "warn", "");
  return true;
}

function validateTelnr  (valfield,   
                         infofield, 
                         required, oldclass)  
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat == false) return stat;

  
  
  var tfld = trimnondigits(valfield.value);  
  
  tfld = tfld.replace(/^1/g, '');
  
  // xyz1112222 or xyz22221234
  var telnr = /^(\d){3}(?:(\d)\2{2}(?:(\d)\3{3}|1234)|5551212)/
  if (telnr.test(tfld)) {
    msg (infofield, "error", "(not a valid phone number)");
	valfield.className = 'fielderror';
    return false;
  }

  if ((tfld.length<10 && (required==true)) || (tfld.length<10 && tfld.length>0) ) {
    msg (infofield, "error", "(missing digits - too short)");
	valfield.className = 'fielderror';
    return false;
  }

  if (tfld.length>10) {
    msg (infofield, "error", "(" + tfld.length + " digits - too long)");
	valfield.className = 'fielderror';
  } else { 
      msg (infofield, "warn", "");
	  valfield.className = oldclass;
  }
  return true;
}

function validateSic  (valfield,   
                         infofield, 
                         required, oldclass)  
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat == false) return stat;

  var tfld = trimnondigits(valfield.value);  

  if ((tfld.length<4 && (required==true)) || (tfld.length<4 && tfld.length>0) ) {
    msg (infofield, "error", "(" + tfld.length + " digits - too short)");
	valfield.className = 'fielderror';
    return false;
  }

  if (tfld.length>4) {
    msg (infofield, "error", "(" + tfld.length + " digits - too long)");
	valfield.className = 'fielderror';
  } else { 
      msg (infofield, "warn", "");
	  valfield.className = oldclass;
  }
  return true;
}

function validateZip  (valfield,   
                         infofield,  
                         required, oldclass, noajax)   
{
	if(noajax && invalid_zip)
	{
		msg (infofield, "error", "(unknown zip code)");
	    valfield.className = 'fielderror';
		return false;
	}
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var zip = trim(valfield.value);  
  var ziptst = /^[0-9-]+$/  ;
  if (!ziptst.test(zip)) {
    msg (infofield, "error", "(not a valid zip code)");
	valfield.className = 'fielderror';
	document.getElementById('e_citystate').innerHTML = "";
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<zip.length; j++)
    if (zip.charAt(j)>='0' && zip.charAt(j)<='9') numdigits++;

  if (numdigits!=5) {
    msg (infofield, "error", "(5 digit zip code only)");
	valfield.className = 'fielderror';
	document.getElementById('e_citystate').innerHTML = "";
    return false;
  }
  else 
  { 
      msg (infofield, "warn", "");
	  valfield.className = oldclass;
	  if(!noajax)
	  {
	  	sendUrl("rss-1html-2ajax.html", "zip=" + zip);
	   }	
  }
  return true;
}



// toggle a button to simulate radio type buttons
function toggleRButton(elementObj, idRegex) {
	var arraySpans = document.body.getElementsByTagName("DIV");
	for(var i = 0; i < arraySpans.length; i++)
	{
		if(arraySpans[i].id.match(idRegex))
		{
			arraySpans[i].className = 'rbtn_normal';
		}
	}
	elementObj.className = 'rbtn_depressed';
}





function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}
function hide(item)
{
	if (!DHTML) return;
	var x = new getObj(item);
	x.style.visibility = 'hidden';
	x.style.width = '0px';
}
function show(item)
{
	if (!DHTML) return;
	var x = new getObj(item);
	x.style.visibility = 'visible';
	x.style.width = '500px';
}
function show2(item)
{
	if (!DHTML) return;
	var x = new getObj(item);
	x.style.visibility = 'visible';
	x.style.height = '24px';
}



/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone,p1, p2, p3)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp, p1, p2, p3);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function purgeModelTrim(e1, e2)
{
	var element = document.getElementById(e1);
	var element2 = document.getElementById(e2);
	element.innerHTML = '<input onblur="validatePresent(this, \'e_model\', \'formtextfield\');" name="ff_model" class="formtextfield" size="30"  type="text" maxlength="255" value=""/>';
	element2.innerHTML = '<input onblur="validatePresent(this, \'e_trim\', \'formtextfield\');" name="ff_trim" class="formtextfield" size="30"  type="text" maxlength="255" value=""/>';
}

function loadFragmentInToElement(fragment_url, param, element_id)
{
    var element = document.getElementById(element_id);
	var theURL = fragment_url;

	var ajaxConn = new XHConn();
	
	ajaxConn.connect(theURL, "POST", param, processFragment, element_id );

	return false;
}

function processFragment(xmlhttp, element_id)
{
	var element = document.getElementById(element_id);
	 if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
           element.innerHTML = xmlhttp.responseText;
    }
	else element.innerHTML = '';
}


function sendUrl(url, param) {

	//initialize XHConn (if XHConn isn't created successfully, 
	//the client doesnt' support Ajax)
	var ajaxConn = new XHConn();
	
	//post to mypage.php with args foo and baz
	ajaxConn.connect(url, "POST", 
	  param,handleCallback);
	
}

function handleCallback(receiveReq) {

	if (receiveReq.readyState == 4) {
		
		if(receiveReq.responseText)
		{
			var myObject = eval('(' + receiveReq.responseText + ')');
			if(myObject.do_zip)
			{
				document.getElementById('e_citystate').innerHTML = myObject.zip;
				if(myObject.invalid_zip)
				{
					invalid_zip = 1;
					validateZip(document.forms[1].ff_zip, 'e_zip', true, '', true);
				}
				else
				{
					invalid_zip = 0;
					validateZip(document.forms[1].ff_zip, 'e_zip', true, '', true);
				}
			}
			
		}		
	}
}


function dyn_addDependent()
{
	var ni = document.getElementById('dyn_dependents');
	var numi = document.getElementById('ff_depcount');
	var num = (document.getElementById("ff_depcount").value -1)+ 2;
	show2('dyn_heading');
	numi.value = num;
	var divIdName = "dyn_dependents"+num;
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var newbit = dynbit_dependent;
	var bitremove = "<a href=\"javascript:;\" onclick=\"dyn_removeDependent(\'"+divIdName+"\')\">[X]</a>";
	newbit = newbit.replace(/GBGBINDEX/gi, num);
	newbit = newbit.replace(/GBGBREMOVE/gi, bitremove);
	var relation = "Child";
	if(num == 1)
	{
		relation = "Spouse";
	}
	newbit = newbit.replace(/GBGBRELATIONSHIP/gi, relation);
	newdiv.innerHTML = newbit;
	ni.appendChild(newdiv);
	var anchor = document.getElementById('dyn_addlink');
	anchor.innerHTML = "Add another person";
}

function dyn_removeDependent(divNum)
{
	var d = document.getElementById('dyn_dependents');
	var olddiv = document.getElementById(divNum);
	d.removeChild(olddiv);
}


function getform(thisform)
{
    var formdata = "";
    for (i=0; i < thisform.length; i++)
    {
         if(thisform.elements[i].type == "text"){ //Handle Textbox's
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "textarea"){ //Handle textareas
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "checkbox"){ //Handle checkbox's
                 formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
         }else if(thisform.elements[i].type == "radio"){ //Handle Radio buttons
                  if(thisform.elements[i].checked==true){
                     formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
                  }
         }else{
                  //finally, this should theoretically this is a select box.
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }
    }
       sendUrl('rss-1html-2ajax-3form.html', formdata);
}

function getform2(obj) {
      var getstr = "?";
      for (i=0; i<obj.childNodes.length; i++) {
         if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "hidden") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "text") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "checkbox") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               } else {
                  getstr += obj.childNodes[i].name + "=&";
               }
            }
            if (obj.childNodes[i].type == "radio") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               }
            }
         }   
         if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         
     }
     alert('rss-1html-2ajax-3form.html'+ getstr);
}


// namespace
var gb;
if(typeof(gb) == 'undefined') {
	gb = {};
}


// Create a function with o (object) and method closures suitable for
// registering callbacks on events.
gb.Callback = function(o, method) {
	return function() {
		return o[method].apply(o, arguments);
	};
}


// commmon parent node
gb.getParentNode = function() {
	if(!arguments.length) {
		return null
	} else if(arguments.length == 1) {
		return arguments[0].parentNode;
	} else if(arguments.length == 2) {
		// 2 digit counting algorithm exploiting the termination
		// condition that eventually parentNode becomes null
		for(var node1 = arguments[1]; node1; node1 = node1.parentNode) {
			for(var node0 = arguments[0]; node0; node0 = node0.parentNode) {
				if(node0 == node1) {
					return node0;
				}
			}
		}
	} else {
		var tail = arguments;
		return gb.getParentNode(gb.getParentNode(tail.pop(), tail.pop()), tail);
	}
}


// Validator plug-in method to show the user result of validation
gb.showHqe = function(names, success, message) {
	for(var i = 0; i < names.length; ++i) {
		var name = names[i];
		var control = this.controls[name][2];
		control.className = success ? this.controls[name][1] : 'fielderror';
	}
	return success;
}


gb.showId = function(names, success, message) {
	for(var i = 0; i < names.length; ++i) {
		var name = names[i];

		var control = this.controls[name][2];
		control.className = success ? this.controls[name][1] : 'fielderror';

		var span = control.parentNode.getElementsByTagName('span')[0];
		if(span) {
			span.className = success ? '' : 'error';
			span.firstChild.nodeValue = '(' + (success ? 'required' : message) + ')';
		}
	}
	return success;
}


// Validator implements an interface to make it easy to check forms, and
// guide user to fill out forms.
gb.Validator = function(show, test_mode) {
	// name to [controls, original className, span] mapping
	this.controls = {};

	// name to [names, events, predicate, message] mapping
	this.rules = {};

	this.show = show;
	this.test_mode = !!test_mode;
}


// Specific which events the predicate should be considered, and optional
// message on error (when predicate is false).
gb.Validator.prototype.addRule = function(events, predicate, message) {
	// support perl regex notation
	predicate = predicate.replace(/\s*=~\s*(\/[^\/]*\/[igm]*)/g, ".search($1) != -1");
	predicate = predicate.replace(/\s*!~\s*(\/[^\/]*\/[igm]*)/g, ".search($1) == -1");
	
	// assume names start with ff_
	var names = predicate.match(/ff_[a-z0-9]+/g);
	var names2 = {};
	for(var i = 0; i < names.length; ++i) {
		var name = names[i];
		if(i > 0 && names[i-1] == name) {
			continue;
		}

		// easy way to get distinct set of names
		names2[name] = 1;

		if(!this.rules[name]) {
			this.rules[name] = [];
		}
	}

	for(var name in names2) {
		this.rules[name].push([names, events, predicate, message]);
	}
}


// Replace with DOM 2 Event model when browser support permits
gb.Validator.prototype.addEvents = function() {
	for(var name in this.rules) {
		var controls = document.getElementsByName(name);
		var span;
		if(controls[0].tagName == 'SELECT') {
			// Work-around for IE
			span = controls[0].parentNode;
		} else if(controls[0].type == 'radio') {
			// assume single type of control per name
			var controlsArray = [];
			for(var i = 0; i < controls.length; ++i) {
				controlsArray.push(controls[i]);
			}
			span = gb.getParentNode.apply(null, controlsArray);
		} else {
			span = controls[0];
		}
		// IE does not display border around SELECT, and at least
		// with the hqe stylesheet on firefox I did not see any borders
		// around tbody and tr.
		while(span && span.tagName in {'SELECT': 1, 'TBODY': 1, 'TR': 1}) {
			span = span.parentNode;
		}

		this.controls[name] = [ controls, controls[0].className, span ];
		for(var i = 0; i < this.rules[name].length; ++i) {
			var rule = this.rules[name][i];
			var events = rule[1];
			for(var j = 0; j < controls.length; ++j) {
				for(var k = 0; k < events.length; ++k) {
					controls[j][events[k]] = gb.Callback(this, 'checkControlEvent');
				}
			}
		}
	}

	if(this.test_mode) {
		return;
	}	

	// assume single form and that the first registered event is for that form
	for(var name in this.rules) {
		document.getElementsByName(name)[0].form.onsubmit = gb.Callback(this, "checkAllControls");
		break;
	}
}


// onsubmit
gb.Validator.prototype.checkAllControls = function() {
	var failures = 0;
	for(var name in this.rules) {
		failures += !this.checkControl(name);
	}

	if(failures) {
		alert('There ' + (failures == 1 ? 'is a field' : 'are fields') + ' which need correction before sending');
	}

	return failures ? false : true;
}


gb.Validator.prototype.checkControl = function(name) {
	for(var i = 0; i < this.rules[name].length; ++i) {
		var rule = this.rules[name][i];
		var names = rule[0];
		var values = {};
		for(var j = 0; j < names.length; ++j) {
			var name2 = names[j];
			if(values[name2] == undefined) {
				var controls = this.controls[name2][0];
				for(var k = 0; k < controls.length; ++k) {
					var control = controls[k];
					if(control.type != 'radio' || control.checked) {
						values[name2] = control.value;
					}
				}
				if(values[name2] == undefined) {
					values[name2] = '';
				}
			}
		}
		
		var predicate = rule[2];
		var message = rule[3]
		with(values) {
			var success = 0;
			try {
				success = this.show(names, eval(predicate), message);
			} catch(error) {
				alert(error + ': ' + predicate);
			}
			if(!success)
				break;
		}
	}
	
	return success ? true : false;
}


gb.Validator.prototype.checkControlEvent = function(e) {
	// IE work-around
	var e = e || window.event;
	if(!e.target)
		e.target = e.srcElement;

	e.cancelBubble = true;
	if(e.stopPropagation) 
		e.stopPropagation();

	this.checkControl(e.target.name);
}


// Work-around for IE 5, 5.5, and 6 memory leak
// (http://support.microsoft.com/kb/830555)
gb.Validator.prototype.removeEvents = function() {
	for(var name in this.rules) {
		var control = this.controls[name][0];
		for(var i = 0; i < this.rules[name].length; ++i) {
			var rule = this.rules[name][i];
			var events = rule[1];
			for(var j = 0; j < events.length; ++j) {
				control[events[j]] = '';
			}
		}
	}
	
	// assume single form and that the first registered event is for that form
	for(var name in this.rules) {
		document.getElementsByName(name)[0].form.onsubmit = '';
		break;
	}
}


// Visitor pattern using function chaining
gb.Visitor = function(e, f, override) {
	if(!e || override) {
		return f;
	} else {
		return function() { e(); f(); }
	}
}
