// cookie script

var expDays = 1;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}

function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}



// email format checker for signup page

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function checkAge(form){	
	var min_age = 13;
	var month = (parseInt(form.dob_month.options[form.dob_month.selectedIndex].value)-1);
	var day = parseInt(form.dob_day.options[form.dob_day.selectedIndex].value);
	var year = parseInt(form.dob_year.options[form.dob_year.selectedIndex].value);	
	var user = new Date((year+min_age), month, day);
	var today = new Date();
	if(today.getTime() - user.getTime() < 0){
		return false;
	}else {
		return true;
	}
}

function checkemailsignup(which){

var msg="";
	
  if (which.email.value == ""){
    msg=msg+"Please enter a value for the \"email\" field.\n";
  }
  if (!isEmailAddr(which.email.value)){
    msg=msg+"Please enter a complete email address in the form: yourname@yourdomain.com\n";
  }   
  if (which.email.value.length < 3){
    msg=msg+"Please enter at least 3 characters in the \"email\" field.\n";
  }  
  if (which.dob_month.value=="0"){
	msg=msg+"You must select a birth month\n";
  }
  if (which.dob_day.value=="0"){
	msg=msg+"You must select a birth day\n";
  }
  if (which.dob_year.value=="0"){
	msg=msg+"You must select a birth year\n";
  }
    
  if (msg!=""){
  
		alert("the following errors were detected:\n\n" + msg + "\nPlease fix them to proceed.");
		return (false);
		
  }else if(!checkAge(which)){
  
		SetCookie ('notoldenough', 'that_be_true', exp);
		homedivs('tooyoung');
		return (false); 
		
  }else{  
  		
		var that=window.hiddenform.document.forms.form;
		var this_dob_full = which.dob_year.value + "-" + which.dob_month.value + "-" + which.dob_day.value;
		that.dob_full.value=this_dob_full;
		that.email.value=which.email.value;
		that.submit();		
 	 	homedivs('thankyou');
		return (false);
		
  }
  
  
}
