function highlight_field(fieldid,s){
    fieldElement = id2elem(fieldid);
    if (s==1){
       fieldElement.style.border = "2px solid #f00";
		}else{
       fieldElement.style.border = "2px inset #808080";
		}
}
function id2elem(id) {
	
  if (typeof(id) != 'string') {
		return id;
	}
	if (document.getElementById) {
		id = document.getElementById(id);
	} else if (document.all) {
		id=document.all[id];
	} else {
		id = null;
	}
	return id;
}

function trim(str,character) 
{  
    if(str.charAt(0) == character){  
        str = trim(str.substring(1));
    }
    if (str.charAt(str.length-1) == character){  
        str = trim(str.substring(0,str.length-1));
    }
    return str;
}

function textCounter(field, maxlimit) {
    fieldElem = id2elem(field);
    if (fieldElem.value.length > maxlimit) 
        fieldElem.value = fieldElem.value.substring(0, maxlimit);
}

function validateForm(f){
	var AtSym = $('email').value.indexOf('@')
	var dot = $('email').value.lastIndexOf('.')
	var space = $('email').value.indexOf(' ')
	var length = $('email').value.length
   
	if ($('firstname').value == ""){
    highlight_field('firstname',1);
		$('error').innerHTML = 'Please enter your first name.';
    $('error').style.display = 'block';
    $('firstname').focus();
		return false;
	}else{
    highlight_field('firstname',0);
    $('error').style.display = 'none';
  }
  
  if ($('lastname').value == ""){
    highlight_field('lastname',1);
		$('error').innerHTML = 'Please enter your last name.';
    $('error').style.display = 'block';
    $('lastname').focus();
		return false;
	}else{
    highlight_field('lastname',0);
    $('error').style.display = 'none';
  }

	if ((AtSym < 1 ) ||   // '@' can't be first
    	 (dot <= AtSym +1 ) || //one char between '@' and '.'
	     (dot == length - 1 ) ||  //one char after '.'
	     (space  != -1))  //no spaces allowed
	   {
	    highlight_field('email',1);
		  $('error').innerHTML = 'Please enter a valid email address.';
      $('error').style.display = 'block';
      $('email').focus();
		return false;
	}else{
    highlight_field('email',0);
    $('error').style.display = 'none';
  }
  eval("document.forms."+f+".submit()");
}

function checkForm(f){
      
      //firstname = id2elem('firstname');
      //lastname = id2elem('lastname');
      //email = id2elem('email');
      //friends = id2elem('friends');
      //errorbox = id2elem('errorbox');
     
      var AtSym = $('email').value.indexOf('@');
	 	  var dot = $('email').value.lastIndexOf('.');
		  var space = $('email').value.indexOf(' ');
      var eLength = $('email').value.length;
		  
      //first name---------------------------
      if ($('firstname').value == ''){
				    highlight_field('firstname',1);
            $('errorbox').style.display = "block";
            $('errorbox').innerHTML = "Please enter your first name.";
            $('firstname').focus();
						return false;
			}else{
				    highlight_field('firstname',0);
            $('errorbox').style.display = "none";
			}
		  //last name---------------------------
      if ($('lastname').value == ''){
				    highlight_field('lastname',1);
						$('errorbox').style.display = "block";
            $('errorbox').innerHTML = "Please enter your last name.";
            $('lastname').focus();
						return false;
			}else{
			     highlight_field('lastname',0);
				    	
			}
		  
      //email---------------------------
		  if(($('email').value=='')||     
				   (AtSym < 1 ) ||   
    	     (dot <= AtSym +1 ) || 
	         (dot == eLength - 1 ) ||  
	         (space  != -1)) 
					 {
				       highlight_field('email',1);
               $('errorbox').style.display = "block";
               $('errorbox').innerHTML = "Please enter a valid email address";
               $('email').focus();
               return false;
			}else{
				    highlight_field('email',0);
            $('errorbox').style.display = "none";
			}
        
			//friends---------------------------
      var friendVal = $('friends').value;
      //get rid of trailing commas and spaces.
      friendVal = trim(friendVal," ");
      friendVal = trim(friendVal,",");
      friendVal = trim(friendVal," ");
      var friendArr = friendVal.split(",");
      //make sure all friend addresses are formatted properly
      if(friendArr[0] == ""){ // not filled in, highlight field
          highlight_field('friends',1);
          $('errorbox').style.display = "block";
          $('errorbox').innerHTML = "Please enter at least one address to send to.";
          $('friends').focus();
          return false;
      }else{
          //check each address
          for(i=0;i<friendArr.length;i++){
              address = friendArr[i];
              address = trim(address," ");
              AtSym = address.indexOf('@');
	 	          dot = address.lastIndexOf('.');
		          space = address.indexOf(' ');
		          eLength = address.length;
              if((address == '')||     
				          (AtSym < 1 ) ||   
    	            (dot <= AtSym +1 ) || 
	                (dot == eLength - 1 ) ||  
	                (space  != -1)) 
					        {
                  highlight_field('friends',1);
                  $('errorbox').style.display = "block";
                  $('errorbox').innerHTML = "Please make sure all of your friend addresses are valid.";
                  friends.focus();
                  return false;
              }else{
                  highlight_field('friends',0);
                  $('errorbox').style.display = "none";
              }    
          }
          highlight_field('friends',0);
          $('errorbox').style.display = "none";
      }
      
      formEl = id2elem(f);
      //alert("form submitted");
      formEl.submit();
}

function popup(url,width,height){
    nw = window.open(url,"newwindow","width="+width+",height="+height+",status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1,location=0");
    nw.focus();
}
