// JavaScript Document
var FireFox = (navigator.appName.indexOf('Netscape') != -1);

var userAgent = navigator.userAgent.toLowerCase();
 
// Figure out what browser is being used
var browser = {
	version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
	chrome: /chrome/.test( userAgent ),
	safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};

var urlAddress = "http://www.dafnys.co.il/"; 

function addToFavorites() { 
	if (window.external && !FireFox) { 
		window.external.AddFavorite(urlAddress,pageName) 
	}
	else if (FireFox) { 
		window.sidebar.addPanel(pageName, urlAddress,"");
	} else { 
		alert("Sorry! Your browser doesn't support this function."); 
	} 
} 

function mouseOver(obj){
	if(obj.className.indexOf("Off") > 0){
		obj.className = obj.className.replace("Off","On");
		return;
	}
	obj.className = obj.className.replace("On","Off");
}

function mouseOut(obj){
	if(obj.className.indexOf("Off") > 0){
		obj.className = obj.className.replace("Off","On");
		return;
	}
	obj.className = obj.className.replace("On","Off");
}

function goTo(obj, pageName){
	if(obj.className.indexOf("Off") < 0 && obj.className.indexOf("On") < 0)	
		return;
	window.location.href = pageName;
}

function moveToPage(pageName){
	window.location.href = pageName;
}

function openImage(imgName, imgTitle){
	//alert(imgTitle)
	//debugger
	var winOpen = window.open("cakeImage.html?img=" + imgName + "&title=" + escape(imgTitle),"cakeImage","resizable=0, height=610, width=650, location=0; menubar=0, resizable=0, scrollbars=0; toolbar=0, status=0; left=150, top=50");
	if(!winOpen)
		return;
	winOpen.focus();
}

function getNextSibling(obj){
	while(obj.nodeType == 3 || obj.nodeType == 8){
		obj = obj.nextSibling;
	}
	return obj;
}

function getPrevSibling(obj){
	while(obj.nodeType == 3 || obj.nodeType == 8){
		obj = obj.previousSibling;
	}
	return obj;
}

function getFirstChild(obj){
	return getNextSibling(obj);
}

function getLastChild(obj){
	return getPrevSibling(obj);
}


function Trim(string) {

	index = 0;
	while ( string.substr(index, index + 1) == " " ){
		string = string.substr(index + 1, string.length)
		index++;
	}
	index = string.length;
	while ( string.substr(index - 1, index) == " " ){
		string = string.substr(0, index - 1)
		index--;
	}
	return string;
}

function validEmail(s) {
	invalidChars = " /:,;"
	
	if (Trim(s) == "") 
		return false;
	
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (s.indexOf(badChar,0) > -1) 
			return false;
	}

	atPos = s.indexOf("@",1);
	if (atPos == -1) 
		return false;
	
	if (s.indexOf("@",atPos+1) > -1) 
		return false;
	
	periodPos = s.indexOf(".",atPos);
	if (periodPos == -1) 
		return false;
	
	if (periodPos+3 > s.length)	
		return false;
	
	return true;
}
var timeOutFunc;
function showHide(objId, obj, img, imgOver, imgOut, changeDiv){
	//debugger
	var commentsDiv = document.getElementById("commentsDiv");
	//var tblComments = document.getElementById("tblComments");
	if(obj.getAttribute("clickDisabled") == "disabled")
		return;
	if(obj.style.display == "none"){
		obj.style.display = "";
		obj.style.visibility = "visible";
		if(img)
			img.src = img.src.replace(imgOut, imgOver);
		if(changeDiv){
			commentsDiv.style.height = "210px";
			commentsDiv.style.borderTopWidth = "1px";
		}
		//tblComments.style.height = "400px";
		MM_effectAppearFade(objId, 1200, 0, 100, false);
		return;
	}
	MM_effectAppearFade(objId, 1200, 100, 0, false);
	
	timeOutFunc = setTimeout(function() { showFile(objId, obj, img, imgOver, imgOut, changeDiv);}, 1200)
	
	//tblComments.style.height = "400px";
}

function showFile(objId, obj, img, imgOver, imgOut, changeDiv){
	clearTimeout(timeOutFunc);
	document.getElementById("CAPTCHADisplay").style.display = "none";
	document.getElementById("name").value = "";
	document.getElementById("title").value = "";
	document.getElementById("comment").value = "";
	/*if(FireFox){
		window.location.href=window.location.href;
		return;
	}*/
	var commentsDiv = document.getElementById("commentsDiv");
	obj.style.visibility = "hidden";
	obj.style.display = "none";
	//obj.style.height = "0px";
	if(img)
		img.src = img.src.replace(imgOver, imgOut);
	if(changeDiv){
		commentsDiv.style.height = "350px";
		commentsDiv.style.borderTopWidth = "0px";
	}
	
}

function onTxtOver(obj, stepNum){
	if(obj.getAttribute("clickDisabled") == "disabled")
		return;
	var objCells = obj.childNodes
	for (var idx=0; idx < objCells.length; idx+=stepNum){
		if(objCells[idx].tagName == undefined)
			continue;
		objCells[idx].style.textDecoration='underline';
	}
}

function onTxtOut(obj, stepNum){
	if(obj.getAttribute("clickDisabled") == "disabled")
		return;
	var objCells = obj.childNodes
	for (var idx=0; idx < objCells.length; idx+=stepNum){
		if(objCells[idx].tagName == undefined)
			continue;
		objCells[idx].style.textDecoration='none';
	}
}

function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for (var iParam = 0; iParam < aQueryString.length; iParam++){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}

function openImageAfterLoad(){
	var cakeId = getURLParam('cake');
	if(cakeId == "" || cakeId == null || cakeId == undefined || !document.getElementById(cakeId + "_img"))
		return;
	document.getElementById(cakeId + "_img").onclick();
}

function loadParams(){
	var loaderImg = document.getElementById("imageLoader");
	loaderImg.style.display = "block";
	var imgObj = document.getElementById("cakesImg");
	var title = unescape(getURLParam('title'));
	var rExp = new RegExp("_", "gi");
	title = title.replace(rExp , "'");
	var imageName = getURLParam('img');
	window.document.title = title;
	imgObj.setAttribute("src","images/cakes/" + imageName + ".jpg");
	imgObj.parentNode.style.display = "";
	var parentCakeContent = window.opener.document.getElementById(imageName).cloneNode(true);

	var cakesContent = document.getElementById("cakesContent");
	if(browser.msie){
		cakesContent.innerHTML = parentCakeContent.outerHTML;	
	}
	else{
		cakesContent.appendChild(parentCakeContent);
	}
	document.getElementById(imageName).removeChild(document.getElementById("cake_image"));
	loaderImg.style.display = "none";
	cakesContent.parentNode.style.display = "block";
}
	
function SiteCompMsg(){
	//if(getCookie("msgAppeared") == "yes")
		return;
	
	var winOpen = window.open("SiteCompMsg.html","SiteCompMsg","resizable=0, height=220, width=480, location=0; menubar=0, resizable=0, scrollbars=0; toolbar=0, status=0; left=400, top=150");
	if(!winOpen)
		return;
	winOpen.focus();
	
}

//Handle cookies

function setCookie(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 getCookie(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 delCookie(name) {
	setCookie(name,"",-1);
}
