function checkEmail(e) {
  ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

  for(i=0; i < e.length ;i++){
    if(ok.indexOf(e.charAt(i))<0){ 
	  //found character not in OK string
      return -1;
      }	
    } 
  
  // Note {2, 4} indicates the length of domain from 2 to 4 characters. For example .com, .tv, .us
  if (document.images) {
   re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
   re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
   if (!e.match(re) && e.match(re_two)) {
     //Everything is OK
     return 1;		
     } 
   }
  
  //Bad by Default
  return -1;
  }
  
function indicateError(el){
  if(document.all || document.getElementByID){
    // change the color of text field
    el.style.background = "yellow"; 
	el.onfocus = new Function("this.style.backgroundColor = '#FFFFFF';this.onfocus='';");
    }
  
  return false;
  }

function indicateErrorSelect(el){
  if(document.all || document.getElementByID){
    // change the color of text field
    el.style.background = "yellow"; 
	el.onchange = new Function("this.style.backgroundColor = '#FFFFFF';");
    }
  
  return false;
  }  
  
function deactivate(el){

  if(document.all || document.getElementByID){
    if(el.style.background == "yellow"){
      // change the color of text field
      el.style.background = "white";
	  } 
	
    }
  
  return false;
  }  


function validateForm(){
  if (window.document.chatForm.firstname.value == ""){
    alert('Please enter your first name.');
	indicateError(window.document.chatForm.firstname);
	return false;
	}
  if (window.document.chatForm.lastname.value == ""){
    alert('Please enter your last name.');
	indicateError(window.document.chatForm.lastname);
	return false;
	}
  if (window.document.chatForm.email.value == ""){
    alert('Please enter your email address so we can send you a reply.');
	indicateError(window.document.chatForm.email);
	return false;
	}
  
  if (checkEmail(window.document.chatForm.email.value) < 0){
    alert('Please enter in a valid email address.');
	indicateError(window.document.chatForm.email);
	return false;
	}

  if(window.document.chatForm.cancer.selectedIndex == 0) {
    alert('Please supply your cancer of interest.');
	indicateErrorSelect(window.document.chatForm.cancer);
	return false;
	}	

	
  if(!window.document.chatForm.accept.checked) {
    alert('You must check the checkbox indicating you understand our terms and conditions.');
	indicateError(window.document.chatForm.accept);
	return false;
	}	
	
  newwin = window.open('', 'chat_window', 'screenX=10,screenY=25,top=25,left=10,width=500,height=320,scrollbars=no,menubar=no,resizable=yes,status=no,toolbar=no');
  
	  return submitRequest();
    }	
	
	var requestSubmitted = false;
	var ms = (new Date().getTime()/1000); //milliseconds since 1970-01-01
    function submitRequest() {
              if (!requestSubmitted || ((new Date().getTime()/1000) - ms) > 2) // time interval between two click is greater than 2 seconds
			  {
                     requestSubmitted  = true;
					 ms = new Date().getTime()/1000;
                     return true;
              }
			  else 
			  {
				  //alert('dbl click1!');
				  window.location.reload(false);
				  return false;
			  }
       }

function clearAll(){
  deactivate(window.document.chatForm.firstname);
  deactivate(window.document.chatForm.lastname);
  deactivate(window.document.chatForm.email);
  deactivate(window.document.chatForm.cancer);
  deactivate(window.document.chatForm.affect);
  deactivate(window.document.chatForm.location);
  deactivate(window.document.chatForm.accept);
  
  window.document.chatForm.reset();
  }
  
function validateNotAvailableForm(){
  if(window.document.notAvailableForm.preferred.selectedIndex == 0) {
    alert('Please indicate how you would like us to contact you.');
	indicateError(window.document.notAvailableForm.preferred);
	return false;
	}	
	
  if((window.document.notAvailableForm.preferred.selectedIndex == 2) && (window.document.notAvailableForm.phone.value == "")) {
    alert('You indicated to us that you would like us to give you a call, but you did not provide a telephone number to call you at. Please supply a telephone number.');
	indicateError(window.document.notAvailableForm.phone);
	return false;
	}	
  }  