window.onload = prep;
function prep()
{
	
	document.onclick = function(e){
		hidemenus()
	}

	var links = document.getElementsByTagName('a');
	var link_num = links.length;
	for ( var x=0; x < link_num; x++ ){
		if ( links[x].className == 'menu' ){
			links[x].onclick = function(e){
				if (!e) var e = window.event;
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				return openClose(this);
			}
		}
		
		if ( links[x].className == 'zoom' ){
			links[x].onclick = function(){
				return showZoom(this);
			}
		}
	}
	
	if ( document.forms['search_form'] ){
		document.search_form.search.setAttribute('class', 'no_focus');
		document.search_form.search.onfocus = function(){
			return clearField(this, 'Search');
			
		}
	}
	
	if ( document.forms['mailing_list'] ){
		document.mailing_list.email.setAttribute('class', 'no_focus');
		document.mailing_list.email.onfocus = function(){
			return clearField(this, 'Enter Email Address');
		}
	}
	
	if ( document.forms['appt_form'] ){
		
		document.appt_form.pref_date.onfocus = function(){
			clearField(this, 'M/D/YYYY');
			if (BrowserDetect.browser != 'Explorer'){
				Calendar(this);
			}
		}
		
	}
	
	if ( document.getElementById('bigger') ){
		document.getElementById('bigger').onclick = function(){
			return bigger();
		}
	}
	
	if ( document.getElementById('smaller') ){
		document.getElementById('smaller').onclick = function(){
			return smaller();
		}
	}
	
	if ( document.getElementById('reset') ){
		document.getElementById('reset').onclick = function(){
			return reset();
		}
	}

	
	text_size = parseInt( getCookie('fontsize') );
	if ( isNaN(text_size) ){
		text_size = 10;
	} else if (text_size > 16) {
		text_size = 16;
	} else if ( text_size < 10 ){
		text_size = 10;
	}
	
}

function hideZoom()
{
	document.body.removeChild(document.getElementById('new_overlay'));
}

function showZoom(link)
{
	var pic_title = link.getAttribute('title');
	//make overlay
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'new_overlay');
	overlay.style.opacity = 0;
	document.body.appendChild(overlay);
	fadein('new_overlay', 0);  
	
	var holder = document.createElement('div');
	holder.setAttribute('id', 'zoom_img_holder');
	
	var img_title = document.createElement('div');
	img_title.setAttribute('class', 'img_title');
	img_title.setAttribute('title', 'close');
	img_title.appendChild(document.createTextNode(pic_title));
	holder.appendChild(img_title);
	
	overlay.appendChild(holder);
	
	var close = document.createElement('p');
	close.setAttribute('id', 'close');
	var close_button = document.createElement('img');
	close_button.setAttribute('src', 'images/close_button.png');
	close_button.onclick = function (){
		return hideZoom();
	} 
	close.appendChild(close_button);
	holder.appendChild(close);
	
	var zoom = new Image();
	zoom.onload = function()
	{
		var pic = document.createElement('img');
		pic.setAttribute('src', link.href);
		pic.setAttribute('id', 'zoom_pic');
		pic.setAttribute('alt', '');
		holder.appendChild(pic);
		
		var before = document.createElement('div');
		before.setAttribute('class', 'before');
		before.appendChild(document.createTextNode('Before'));
		
		var after = document.createElement('div');
		after.setAttribute('class', 'after');
		after.appendChild(document.createTextNode('After'));
		holder.appendChild(before);
		holder.appendChild(after);
		
	}
	zoom.src = link.href;
	return false;
}

function clearField(input, defText)
{

	if ( input.value.toLowerCase() == defText.toLowerCase() ){
		input.value = '';
	}
	input.onblur = function(){
		return restoreText(input, defText);
	}
	input.setAttribute('class', 'has_focus');
	input.className = 'has_focus';
}

function restoreText(input, defText){
	if ( input.value == '' ){
		input.value = defText;
		input.setAttribute('class', 'no_focus');
		input.className = 'no_focus';
	}	
}

function openClose(obj){
	
	var parent = obj.parentNode;
	var childList = parent.getElementsByTagName("ul");
	if (childList[0].style.display == "none" || childList[0].style.display == "" ){
		childList[0].style.display = "block";
	} else {
		childList[0].style.display = "none";
	}
	hideothers(childList[0]);
	return false;
}

function hideothers(obj)
{
	var menus = document.getElementsByTagName('ul');
	var menu_num = menus.length;

	for ( var n=0; n < menu_num; n++ ){
		if ( menus[n].className == 'menu' && menus[n] != obj ){
			menus[n].style.display = 'none';
		}
	}
}

function hidemenus()
{
	var menus = document.getElementsByTagName('ul');
	var menu_num = menus.length;

	for ( var n=0; n < menu_num; n++ ){
		if ( menus[n].className == 'menu' ){
			menus[n].style.display = 'none';
		}
	}
}

function send_email()
{
	if(document.getElementById("email").value=="" || document.getElementById("email").value==0)
	{
		alert("Please enter your email address.");
		document.getElementById("email").focus();
		return false;
	}
	emailID=document.getElementById("email");
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
}
function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
   alert("Invalid E-mail ID")
   return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   alert("Invalid E-mail ID")
   return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	alert("Invalid E-mail ID")
	return false
}

 if (str.indexOf(at,(lat+1))!=-1){
	alert("Invalid E-mail ID")
	return false
 }

 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	alert("Invalid E-mail ID")
	return false
 }

 if (str.indexOf(dot,(lat+2))==-1){
	alert("Invalid E-mail ID")
	return false
 }

 if (str.indexOf(" ")!=-1){
	alert("Invalid E-mail ID")
	return false
 }

 return true					
}

/*
	April 30th 2010:
	the func_submit has been moved to this "cohen.js" external file and modified to take the "pageName" as a parameter
*/
function func_submit(pageName)
{
	form = document.createElement("form");
	form.method = "POST";
	form.action = "editor_index.php?pageName="+pageName;
	form.target = "_blank";
	document.body.appendChild(form);
	form.submit();	
}

function bigger()
{
	if ( text_size < 16 ){
		text_size += 1;
		if ( document.getElementById('maintext') ){
			document.getElementById('maintext').style.fontSize = text_size+'pt';
		} else {
			document.getElementById('maintext2').style.fontSize = text_size+'pt';
		}
		setCookie('fontsize', text_size, 365 );
	}
}

function smaller()
{
	if ( text_size > 10  ){
		text_size -= 1;
		if ( document.getElementById('maintext') ){
			document.getElementById('maintext').style.fontSize = text_size+'pt';
		} else {
			document.getElementById('maintext2').style.fontSize = text_size+'pt';
		}
		setCookie('fontsize', text_size, 365 );
	}
}

function reset()
{
	text_size = 10;
	if ( document.getElementById('maintext') ){
		document.getElementById('maintext').style.fontSize = text_size+'pt';
	} else {
		document.getElementById('maintext2').style.fontSize = text_size+'pt';
	}
	setCookie('fontsize', text_size, 365 );
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function fadein(objId,opacity, callback) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 20;
			window.setTimeout("fadein('"+objId+"',"+opacity+")", 75);
		}
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

