var incdec = 0;

//eraseCookie("incdec")

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function eraseCookie(name) {
	createCookie(name,"",-1);
}

function autoIncrease(){
    incdec = readCookie("incdec")
    incdec = Math.round(incdec)
    //alert(incdec)
    if (incdec > 0) {
        changeFontSize(incdec, false);
    }
}

function changeFontSize(inc, booUpdate)
{
  if (inc == -1){
    eraseCookie("incdec")
    window.location.reload()
    return false;
  }
  if (booUpdate) {
    incdec = incdec + inc 
  }
  //alert(incdec)
  createCookie("incdec", incdec, 100)
  
  var p = document.getElementsByTagName('p');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 12;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
   
  var p = document.getElementsByTagName('div');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 12;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
   
  var p = document.getElementsByTagName('a');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 12;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
   
  var p = document.getElementsByTagName('li');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 12;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
  var p = document.getElementsByTagName('h2');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 15;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
}

function sharePage(){
    //alert('Sharing page: ' + window.location.href)
    document.getElementById('blanket').style.display = 'inline';
    document.getElementById('share').style.display = 'inline';
    document.getElementById('emailFriendForm').style.display = 'inline';
    document.getElementById('sendtoURL').value = window.location.href;
    document.getElementById('senderName').focus();  
}


function validateCompForm(form) {
	var form_valid = true;
	var email_valid = true;

	// first name
	if(form.senderName.value.length == 0) {
		form_valid = false;
		form.senderName.style.color = '#990000';
		form.senderName.style.background = '#cccccc';
//		form.senderName.value = '';
		form.senderName.onchange = function(){this.style.color = '#000000';this.style.background='#ffffff';}
	}
	// first email
	if(!checkEmail(form.senderEmail.value)) {
		form_valid = false;
		form.senderEmail.style.color = '#990000';
		form.senderEmail.style.background = '#cccccc';
//		form.senderEmail.value = '';
		form.senderEmail.onchange = function(){this.style.color = '#000000';this.style.background='#ffffff';}
	}
	
	//friend name
	if(form.friendName.value.length == 0) {
		form_valid = false;
		form.friendName.style.color = '#990000';
		form.friendName.style.background = '#cccccc';
//		form.friendName.value = '';
		form.friendName.onchange = function(){this.style.color='#000000';this.style.background='#ffffff';}
	}
		
	//friend email
	if(!checkEmail(form.friendEmail.value)) {
		form_valid = false;
		form.friendEmail.style.color = '#990000';
		form.friendEmail.style.background = '#cccccc';
//		form.friendEmail.value = '';
		form.friendEmail.onchange = function(){this.style.color = '#000000';this.style.background='#ffffff';}
	}
	
	if(form_valid) {
		form.submit();	
	} else {
		alert("Please enter valid information in the greyed out fields.");
	}
}

function checkEmail(str) {
	var at	 = "@";
	var dot	 = ".";
	var lat	 = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (-1 == str.indexOf(at)) {
	   return false;
	}

	if (-1 == str.indexOf(at) || 0 == str.indexOf(at) || str.indexOf(at) == lstr) {
	   return false;
	}

	if (-1 == str.indexOf(dot) || 0 == str.indexOf(dot) || str.indexOf(dot) == lstr) {
	    return false;
	}

	if (-1 != str.indexOf(at,(lat+1))) {
		return false;
	}

	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) {
		return false;
	}

	if (-1 == str.indexOf(dot,(lat+2))) {
		return false;
	}

	if (-1 != str.indexOf(" ")) {
		return false;
	}

	return true;				
}

