// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist

function setCookieS09( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function getCookie09(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function checkRedir() {
    //alert('in check redir');
    var ismobile = getCookie09("mobile2");
    //alert('ismobile:' + ismobile);
    //alert('ismobile M:' + (ismobile=="M") );
    //alert('ismobile S:' + (ismobile=="S") );
    //alert('ismobile null:' + (ismobile==null));

    if (ismobile==null) window.location= "http://www.myjewishlearning.com/choose.htm";

    if (ismobile=="M")
        window.location = "http://mobile.myjewishlearning.com";
    if (ismobile=="S")
        return;

}
//checkRedir();
//alert("agent:" + navigator.userAgent.toLowerCase());
if (navigator.userAgent.toLowerCase().indexOf('webtv') > -1) {
  checkRedir();
} else if (navigator.userAgent.toLowerCase().indexOf('windows ce') > -1) {
  checkRedir();
} else if (navigator.userAgent.toLowerCase().indexOf('blackberry') > -1) {
  checkRedir();
} else if (navigator.userAgent.toLowerCase().indexOf('palm') > -1) {
  checkRedir();
} else if (navigator.userAgent.toLowerCase().indexOf('iphone') > -1) {
  checkRedir();
} else if (navigator.userAgent.toLowerCase().indexOf('wince') > -1) {
  checkRedir();
}

function writeFooter () {

var footTxt= '';
//footTxt += '<div id="footer">';
//footTxt += '<a href="http://www.myjewishlearning.com/blog">Blog</a> | <a href="http://www.myjewishlearning.com/forums">Discussions</a> | <a href="http://www.myjewishlearning.com/aboutus/abindex.shtml">About MyJewishLearning</a> | <a href="http://www.myjewishlearning.com/aboutus/abindex/privacy.shtml">Privacy Policy</a> | <a href="/links/lindex.shtml">Links</a> | <a href="http://www.myjewishlearning.com/links/lindex/communities.shtml">In Your Area</a> | <a href="http://www.myjewishlearning.com/contact_us">Contact Us</a>';
//footTxt += '</div>';

footTxt= '';
footTxt += '<div id="footer">';
footTxt += '<table width="999" border="0" cellpadding="0" cellspacing="0">';
footTxt += '<td align="left" width="778" valign="top">';
footTxt += '	<a href="http://www.myjewishlearning.com/blog">Blog</a> | <a href="http://www.myjewishlearning.com/forums">Discussions</a> | <a href="/aboutus/abindex.shtml">About MyJewishLearning</a> | <a href="/aboutus/abindex/privacy.shtml">Privacy Policy</a> | <a href="/links/lindex.shtml">Links</a> | <a href="/links/lindex/communities.shtml">In Your Area</a> <a href="/spider.html"> | </a> <a target="_blank" href="/contact_us">Contact Us</a> | </a> <a href="mailto:ads@myjewishlearning.com?subject=Advertise on MJL">Advertise on MJL</a>';
footTxt += '</td>';
footTxt += '<td align="right" width="100" valign="top">site design by</td>';
footTxt += '<td align="left" width="121" valign="top"><a href="http://www.freerangestudios.com/"><img border="0" src="/images/free-range-logo.gif" width="121" height="14"></td>';
footTxt += '</tr>';
footTxt += '</table>';
footTxt += '</div>';

document.write(footTxt);

}

function validEmail2(email){invalidChars=" /:,;"
if(email==""){return false;}
for(i=0;i<invalidChars.length;i++){badChar=invalidChars.charAt(i)
if(email.indexOf(badChar,0)!=-1){return false;}}
atPos=email.indexOf("@",1)
if(atPos==-1){return false;}
if(email.indexOf("@",atPos+1)!=-1){return false;}
periodPos=email.indexOf(".",atPos)
if(periodPos==-1){return false;}
if(periodPos+3>email.length){return false;}
return true;
}

function validateELsubmit () {
var em = document.elform.textfield.value;
var emOK = validEmail2(em);
if (!(emOK)) {
    alert('Please enter valid email address.');
    return false;
}
else
    return true;

}


function askQ() {
    window.location="/ask_the_expert/ask";
}

function doEletterSection (tidIn) {
    var classout = 'This Week on MJL';
    switch(tidIn)
    {
    case 'FO':
      classout = 'Recipes';
      break;
    case 'WT':
      classout = 'Weekly Torah';
      break;
    case 'HY':
      classout = 'Holidays';
      break;
    case 'LF':
      classout = 'Life';
      break;
    case 'CL':
      classout = 'Culture';
      break;
    case 'BF':
      classout = 'Beliefs';
      break;
    case 'PR':
      classout = 'Practices';
      break;
    case 'HS':
      classout = 'History';
      break;
    case 'IS':
      classout = 'Israel';
      break;
    case 'TS':
      classout = 'Texts';
      break;
    default:
      classout = 'Now on MJL';
    }
    return(classout.toUpperCase());
}
function doSetELBox (tidIn) {

    switch(tidIn)
    {
    case 'FO':
      document.elform["EL[]"][2].checked = true;
      break;
    case 'WT':
      document.elform["EL[]"][1].checked = true;
      break;
    case 'HY':
      document.elform["EL[]"][3].checked = true;
      break;
    case 'LF':
      document.elform["EL[]"][10].checked = true;
      break;
    case 'CL':
      document.elform["EL[]"][8].checked = true;
      break;
    case 'BF':
      document.elform["EL[]"][4].checked = true;
      break;
    case 'PR':
      document.elform["EL[]"][5].checked = true;
      break;
    case 'HS':
      document.elform["EL[]"][6].checked = true;
      break;
    case 'IS':
      document.elform["EL[]"][7].checked = true;
      break;
    case 'TS':
      document.elform["EL[]"][9].checked = true;
      break;
    default:
      document.elform["EL[]"][0].checked = true;
    }

}

function doWrap (tidIn) {

    switch(tidIn)
    {
    case 'HY':
      classout = 'holidays';
      break;
    case 'LF':
      classout = 'life';
      break;
    case 'CL':
      classout = 'culture';
      break;
    case 'BF':
      classout = 'beliefs';
      break;
    case 'PR':
      classout = 'practices';
      break;
    case 'HS':
      classout = 'history';
      break;
    case 'IS':
      classout = 'israel';
      break;
    case 'TS':
      classout = 'texts';
      break;
    default:
      classout = '';
    }
    document.write(divout + ' class="' + classout + '">');

}

function popWinEmail(page,w,h) {
emailwindow=window.open(page,'emailwindow','width=' + w + ',height=' + h + ',left=300,top=150,location=no,resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes');
emailwindow.opener=self;
emailwindow.focus();
}

function emailArticle(docidin) {
//urlIn = (urlIn.replace(".shtml", ".js"));
popWinEmail('/emailarticle.php?i=' + docidin,500,500);
}

// This function enables the dropdown nav in IE 6 //
function startList() {
	if (document.all&&document.getElementById) {
		cssdropdownRoot = document.getElementById("cssdropdown");
		for (x=0; x<cssdropdownRoot.childNodes.length; x++) {
			node = cssdropdownRoot.childNodes[x];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}


// The two functions below make the add more check boxes ( in the homepage sidebar)  //
function assignStyle() {
	if (document.getElementById) {
		obj = document.getElementById('moreCheckBoxes');
		obj.style.display="none";
		bg = document.getElementById('butMore');
		bg.onclick = showDiv;
	}
}
showDiv = function(name) {
	if (obj.style.display=="none") {
		obj.style.display="";
		bg.style.background="url('http://www.myjewishlearning.com/includes/images/but-less.gif') top left no-repeat";
	} else {
		obj.style.display="none";
		bg.style.background="url('http://www.myjewishlearning.com/includes/images/but-more.gif') top left no-repeat";
		sidebar = document.getElementById('homeSidebar');
	}
}


// This function adds the rollover states to formButtons //
function formButtons() {
	if (document.getElementById) {
		searchButton = document.getElementById('submitSearch');
		searchButton.onmouseover= function() {
			searchButton.src='http://www.myjewishlearning.com/includes/images/but-search-over.gif';
		}
		searchButton.onmouseout= function() {
			searchButton.src='http://www.myjewishlearning.com/includes/images/but-search.gif';
		}
		emailButton = document.getElementById('submitNewsletters');
		emailButton.onmouseover= function() {
			emailButton.src='http://www.myjewishlearning.com/includes/images/but-email-over.gif';
		}
		emailButton.onmouseout= function() {
			emailButton.src='http://www.myjewishlearning.com/includes/images/but-email.gif';
		}
		zipcodeButton = document.getElementById('submitZipcode');
		zipcodeButton.onmouseover= function() {
			zipcodeButton.src='http://www.myjewishlearning.com/includes/images/but-zipcode-over.gif';
		}
		zipcodeButton.onmouseout= function() {
			zipcodeButton.src='http://www.myjewishlearning.com/includes/images/but-zipcode.gif';
		}
	}
}


// The two functions below preload images //
userAgent = window.navigator.userAgent;
browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		image1 = newImage("/includes/images/bg-nav-threecol.gif");
		image2 = newImage("/includes/images/nav-holidays-over.gif");
		image3 = newImage("/includes/images/but-less.gif");
		image4 = newImage("/includes/images/but-search-over.gif");
		image5 = newImage("/includes/images/but-email-over.gif");
		image6 = newImage("/includes/images/but-zipcode-over.gif");
		image7 = newImage("/includes/images/bg-nav-onecol.gif");
		image8 = newImage("/includes/images/bg-nav-twocol.gif");
		image9 = newImage("/includes/images/bg-nav-threecol.gif");
		image10 = newImage("/includes/images/bg-nav-fourcol.gif");
		preloadFlag = true;
	}
}


// This function clears the default value of the "freeNews" search field //
function prepareSearchFields() {
	//check that the browser is capable of executing the script
	if(!document.getElementById) return false;
	//find our input field and assign it to a variable for easy referencing
	var searchField = document.getElementById("freeNews");
	//find the default value of the search field
	var defaultValue = searchField.defaultValue;

	searchField.onfocus = function() {
		if(searchField.value == defaultValue) {
			searchField.value = "";
			searchField.style.background = "#fff";
			searchField.style.textIndent = "0px";
		}
	}

	searchField.onblur = function() {
		if(searchField.value == "") {
			searchField.value = defaultValue;
			browser=navigator.appName;
			if (browser!="Microsoft Internet Explorer") {
				searchField.style.textIndent = "18px";
				searchField.style.background = "#fff url('/includes/images/bg-envelope.gif') 1px 2px no-repeat";
			}
		}
	}
}


// Onload Events //
start = function() {
	assignStyle();
	formButtons();
	startList();
	prepareSearchFields();
	preloadImages();
}
if (window.attachEvent) {
	window.attachEvent("onload", start)
} else {
	window.onload=start;
}
