﻿var thisBrowser;
var engLangCode	 	= 2;
var hebLangCode		= 1;
var thisPageLang 	= hebLangCode;
var False = false;
var True  = true;

function validEmail(email){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return email.match(emailRegEx);
}
function alfanumberic(str){
	return str.match(/[^a-zA-Z0-9_]/);
}

function setBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();

    thisBrowser = {
        version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
        safari: /webkit/.test( userAgent ),
        opera: /opera/.test( userAgent ),
        msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
    };

}
setBrowser();

function doNothingOnSelect(e,item){
}
function doNothing(){}

function stdCheckForm() 
{
	if (thisPageLang == engLangCode){
		var requestFieldMsg = 'Please enter value for ';
		var badEmailMsg = 'Value in $fieldName$ is not a valid Email address. ';
		var mustCheckMsg = 'You must Check ';
	}
	else {
		var requestFieldMsg = ' הכנס ערך לשדה ';
		var badEmailMsg = 'הערך בשדה  $fieldName$ אינו כתובת דוא"ל תקינה. אנא בדוק ונסה שנית. ';
		var mustCheckMsg = 'חובה לסמן את ';
	
	}
	var formOK = true;
	var mustItems = document.getElementsByClassName('must');
	for(var i=0;i<mustItems.length;i++)
	{ 
		var curField   	= mustItems[i];
		var fieldName	= ((curField.getAttribute("fieldUserName")) ?  curField.getAttribute("fieldUserName") : curField.name);
		if (curField.value.trim() == '' || (curField.type == 'checkbox' && !(curField.checked)) ) {					
			// alert('חובה למלא את השדה :' + curField.name);
			if (curField.type == 'checkbox') {
				formOK = false;
				alert(mustCheckMsg + fieldName);		
			}
			else {
				if (curField.tagName == 'INPUT' && !(thisBrowser.msie)) {
					curField.value = prompt(requestFieldMsg+ fieldName);
				}
				else alert(requestFieldMsg + fieldName);
				if (curField.value.trim() == '') {	
					formOK = false;
					curField.focus();
				}
			}
		}
	}
	var emailItems = document.getElementsByClassName('emailCheck');
	for(var i=0;i<emailItems.length;i++)
	{ 
		var curField   	= emailItems[i];
		var fieldName	= ((curField.getAttribute("fieldUserName")) ?  curField.getAttribute("fieldUserName") : curField.name);
		if (curField.value.trim() != '') {	
			if (!validEmail(curField.value.trim())) {
				badEmailMsg = badEmailMsg.replace('$fieldName$',fieldName);
				alert(badEmailMsg );
				formOK = false;
			}
		}
	}
	
	return formOK;
}	

function makeRound(selector){
	var roundTopHtml     = '<div id="roundTop"><span><!--round corners --></span></div>'
	var roundBottomHtml  = '<div id="roundBottom"><span><!--round corners --></span></div>'
	$(selector).each(function(){
		this.innerHTML = roundTopHtml + this.innerHTML + roundBottomHtml;
		});
}


function businessAutoCode(autoFieldId,resultFunc) 
{
	var opt = {
		width	: 200,
		minChars:2,
		//limit:15,
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data	: row,
					value	: row.BusinessName,
					result	: row.BusinessCode 
				}
			});
		},
		formatItem: function(item) {
			return item.BusinessName;
		}
	}			
	$("#" + autoFieldId).autocomplete('/allSites/Ajax/BusinessAuto.asp',opt)
	.result(resultFunc);					
}

function eventAutoCode(autoFieldId,resultFunc) 
{
	var opt = {
		width	: 200,
		minChars:3,
		//limit:15,
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data	: row,
					value	: row.EventName,
					result	: row.EventCode 
				}
			});
		},
		formatItem: function(item) {
			return item.EventName;
		}
	}			
	$("#" + autoFieldId).autocomplete('/allSites/Ajax/EventAuto.asp',opt)
	.result(resultFunc);					
}

function streetCodeTip(streetCodeID) 
{
	$('#'+ streetCodeID).qtip(
	   {
		  content: 'שולף אזורים מתאימים', // Give it a loading message while request is being sent
		  position: {
			 corner: {
				target: 'topMiddle',
				tooltip: 'bottomMiddle'
			 }
		  },
		  show: {
			 when: 'change focus click', // Show it on click...
			 solo: true // ...but hide all others when its shown
		  },
		  hide: 'unfocus', // Hide when it loses focus...
		  style: {
			 tip: true, // Create speech bubble tip at the set tooltip corner above
			 textAlign: 'center',
			 name: 'cream'
		  },
		  api: {
			 // Retrieve the content when tooltip is first rendered
			 onShow: function()
			 {
				var self = this;
				var streetCode = $('#'+ streetCodeID).val();
				self.loadContent("/allsites/ajax/getStreetAreas.asp?StreetCode=" + streetCode);

				//alert();
			 }
		  }
	   });
}

if (thisBrowser.msie) {
	document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
	}; 		
}
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
		
function setDefault(val,defaultVal)
{
	if ((val != '') && (val != undefined) ) return val;
	return defaultVal;
}

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 setHomePage(obj,Url)
{
	if (obj.style == null)
	{
		obj = obj.document.body;
	}
	obj.style.behavior='url(#default#homepage)'; obj.setHomePage(Url);
	//event.returnValue = false;	
}


function showByObject(obj){
	if (obj) {
		if ((obj.style.display == 'none') ||( obj.style.visibility != 'visible')) 
		{
		  obj.style.display = 'inline';
		  obj.style.visibility = 'visible';
		  return true;
		} 
		else {
			//alert('new view already shown')
		}
	}
	return false;
}

function showById(idStr){
    var obj = document.getElementById(idStr);
    //alert('Showing ' + idStr);
	return(showByObject(obj));
}

function hideByObject(obj){
	if (obj) {
		if ((obj.style.display != 'none') || (obj.style.visibility != 'hidden')) 
		{
			obj.style.display = 'none';
			obj.style.visibility = 'hidden';
			//alert('Hide OK');
			return true;
		} 
		else {
			//alert('old view already hidden' )
			//alert(obj.style.display  + '  ' +  obj.style.visibility)
		}
	}
	return false;
}

function hideById(idStr){
    var obj = document.getElementById(idStr);
    //alert('Hiding ' + idStr);
	return(hideByObject(obj));
}

function switchById(hideId,showId){
	if (!(hideId == showId)){
		if (showById(showId)) {
			//alert('Show by Id OK');
			return (hideById(hideId))
		}
	}
	return false;	
}

function toggleDisplayById(objId){
	if (showById(objId)){
	}
	else (hideById(objId))
}


function fitFrameByObject(editFrame){
	if (editFrame != null){
		if (editFrame.contentDocument && editFrame.contentDocument.body.offsetHeight) // Netscape or Firefox
		{ 
			editFrame.height = editFrame.contentDocument.body.offsetHeight;
			editFrame.width = editFrame.contentDocument.body.offsetWidth  ; 
		}
		else if (editFrame.Document && editFrame.Document.body.scrollHeight) // IE
		{ 	
			editFrame.height = editFrame.Document.body.scrollHeight +20;
			editFrame.width = editFrame.Document.body.scrollWidth +20;
		}
		
	}
}

function fitFrame(frameId){
	var editFrame = document.getElementById(frameId)
	if (editFrame != null){
		return  fitFrameByObject(editFrame)
		//alert(frameId+editFrame.height)
		//alert(frameId+editFrame.width)
	}
}

function fitImageByObject(ImageObj,fitWidth,fitHeight){
	var newImage = new Image();
	
	if (ImageObj) {
		newImage.src = ImageObj.src;
		//alert( 'Source : ' + ImageObj.src)
		//alert( 'width : ' + newImage.width)
		//alert( 'height : ' +newImage.height)

		if (newImage.width > fitWidth   ) {
			var ratio =  fitWidth  / newImage.width 
			//alert( 'ratio : ' +ratio)
			ImageObj.width  = fitWidth  
			ImageObj.height = newImage.height * ratio
			//alert( 'width : ' + ImageObj.width)
			//alert( 'height : ' +ImageObj.height)
	
			//alert( 'height : ' +ImageObj.height)
			if ( ImageObj.height > fitHeight  ) {
				var ratio =  fitHeight   /  ImageObj.height
				ImageObj.height = fitHeight 
				ImageObj.width  = ImageObj.width * ratio
				//alert( 'height : ' +ImageObj.height)
			}
		} 
		else {
			//alert('width Ok - adjusting height');
			//alert( 'height : ' +ImageObj.height)
			if (newImage.height > fitHeight   )
			{
				var ratio =  fitHeight  / newImage.height 
				//alert( 'ratio : ' +ratio)
				ImageObj.height = fitHeight  
				ImageObj.width  = newImage.width * ratio

				//alert( 'width : ' + ImageObj.width)
				//alert( 'height : ' +ImageObj.height)
				if (ImageObj.width >  fitWidth   ) {
					var ratio =  fitWidth  / ImageObj.width 
					//alert( 'ratio : ' +ratio)

					ImageObj.width  = fitWidth  
					ImageObj.height = ImageObj.height * ratio
					//alert( 'height : ' +ImageObj.height)
				} 
			}
		}
	}


}

function showImageObjAfterFit(ImageObj,fitWidth,fitHeight)
{
	showByObject(ImageObj)
	fitImageByObject(ImageObj,fitWidth,fitHeight)
	return true;
}

function showImageByIdAfterFit(ImageId,fitWidth,fitHeight)
{
    var ImageObj = document.getElementById(idStr);
	if (ImageObj) {
		fitImageByObject(ImageObj,fitWidth,fitHeight);
		showByObject(ImageObj)
	}
	return true;
}

function fitImageById(strId,fitWidth,fitHeight){
	//alert( 'height : ' +fitHeight + ' width:' + fitWidth)
	var ImageObj = document.getElementById(strId);
	if (ImageObj) {
		fitImageByObject(ImageObj,fitWidth,fitHeight)
	}
	else {
		// alert('No object found');
	}
}



	function openUserInterface(url){
		var OpenMode = "top=150,left=200,titlebar=0,height=550,width=350,toolbar=0,location=0,directories=0,menubar=0,resizable=1,status=1"
		var win = window.open(rootPath+"allSites/userinterface/"+url ,"UserInterface",OpenMode)
		if (win) {
			win.focus()
			window.blur 
		}
		else {
			alert('פתיחת חלון נחסמה ע"י  חוסם חלונות. אנא בטל חסימת חלונות לאתר זה')
		}
	}
	
	function EopenUserInterface(url){
		var OpenMode = "top=150,left=200,titlebar=0,height=550,width=350,toolbar=0,location=0,directories=0,menubar=0,resizable=1,status=1"
		var win = window.open(rootPath+"allSites/eng/userinterface/"+url ,"UserInterface",OpenMode)
		if (win) {
			win.focus()
			window.blur 
		}
		else {
			alert('action was blocked by pop up window blocker.please allow pop up windows for this site')
		}
	}
	
  function goBackToSite(){
	var mainWin = top.opener
	if ((mainWin == null) || (mainWin.closed)){
		mainWin = window.open(rootPath+'main.asp','TelavivCityMain')
	}
	mainWin.focus()
  
  }

  function schunaVote(workcode){
	openUserInterface('schunaVote.asp?voteworkcode='+workcode)
  }  
  function EschunaVote(workcode){
	EopenUserInterface('schunaVote.asp?voteworkcode='+workcode)
  }  

  function plastikVote(workcode){
	openUserInterface('plastikVote.asp?voteworkcode='+workcode)
  }  
  function EplastikVote(workcode){
	EopenUserInterface('plastikVote.asp?voteworkcode='+workcode)
  }  

function eventArrive(eventcode){
	openUserInterface('userarrive.asp?eventcode='+eventcode)
  }  
  function EeventArrive(eventcode){
	EopenUserInterface('userarrive.asp?eventcode='+eventcode)
  }  

  function joinBusiness(businesscode){
	openUserInterface('businessClub.asp?businesscode='+businesscode)
  }  

  function EjoinBusiness(businesscode){
	EopenUserInterface('businessClub.asp?businesscode='+businesscode)
  }  

  function businessArrive(businesscode){
	openUserInterface('userarrive.asp?businesscode='+businesscode)
  }  

  function EbusinessArrive(businesscode){
	EopenUserInterface('userarrive.asp?businesscode='+businesscode)
  }  

  function tellFriend(url){
	openUserInterface('tellFriend.asp'+url)
  }  

  function EtellFriend(url){
	EopenUserInterface('tellFriend.asp'+url)
  }  
  
  function addOpinion(url){
	openUserInterface('addOpinion.asp'+url)
  }
  
  function addArrive(url){
	openUserInterface('userarrive.asp'+url)
  }
  
  function EaddArrive(url){
	EopenUserInterface('userarrive.asp'+url)
  }
  
  function EaddOpinion(url){
	EopenUserInterface('addOpinion.asp'+url)
  }

  function SendCupon(CuponCode)
  {
	var OpenMode = "top=150,left=200,titlebar=0,height=200,width=350,toolbar=0,location=0,directories=0,menubar=0,resizable=1,status=1"
	var URL = "?CuponCode=" + CuponCode 
	var win = window.open(rootPath+"SMS/SendCupon.asp"+URL,"SMSWin",OpenMode)
    if (win) {
    	win.focus()
    	window.blur 
    }
  }

  function MapOpen(zone , xloc ,yloc,code)
  {
	var OpenMode = "toolbar=0,location=0,directories=0,menubar=0,resizable=1,width=800,height=600"
    if (zone != 0) 
    {
		var URL = "?varflag=1" + "&zone=" + zone+ "&xloc=" + xloc+ "&yloc=" + yloc + "&code=" + code
    	var win = window.open(rootPath+"/flash/av.asp"+URL,"MapWin",OpenMode)

    	if (win) {
    		window.blur 
    		win.focus()
    	}
    	else {
    		alert(popUpBlockMsg )
    	}
    }
    else {
		var win = window.open(rootPath+"/flash/av.asp","MapWin",OpenMode)
     	if (win) {
     		window.blur 
    		win.focus()
	 		//win.document.focus
    	}
    	else {
    		alert(popUpBlockMsg)
    	}
   }
  }
  function EMapOpen(zone , xloc ,yloc,code)
  {
	var OpenMode = "toolbar=0,location=0,directories=0,menubar=0,resizable=1,width=800,height=600"
    if (zone != 0) 
    {
		var URL = "?varflag=1" + "&zone=" + zone+ "&xloc=" + xloc+ "&yloc=" + yloc + "&code=" + code
    	var win = window.open(rootPath+"eng/flash/av.asp"+URL,"MapWin",OpenMode)

    	if (win) {
    		window.blur 
    		win.focus()
    	}
    	else {
    		alert(EPopUpBlockMsg)
    	}
    }
    else {
		var win = window.open(rootPath + "eng/flash/av.asp","MapWin",OpenMode)
     	if (win) {
     		window.blur 
    		win.focus()
	 		//win.document.focus
    	}
    	else {
    		alert(EpopUpBlockMsg)
    	}
   }
  }
  
function OpenGallery(BusinessCode,EventCode,ArticleCode)
{
	var OpenMode = "toolbar=0,location=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=680,height=530"
	var URL
	if (EventCode != 0) {
		URL = "?EventCode=" + EventCode
	}
	else{
		if (ArticleCode != 0) {
			URL = "?ArticleCode=" + ArticleCode
		}
		else {
			URL = "?businessCode=" + BusinessCode
		}
	}
    var win = window.open(rootPath+"/GalleryDisplay.asp"+URL,"GalleryWin",OpenMode)
    if (win) {
    	window.blur 
    	win.focus()
    }
}
function EOpenGallery(BusinessCode,EventCode,ArticleCode)
{
	var OpenMode = "toolbar=0,location=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=680,height=530"
	var URL
	if (EventCode != 0) {
		URL = "?EventCode=" + EventCode
	}
	else{
		if (ArticleCode != 0) {
			URL = "?ArticleCode=" + ArticleCode
		}
		else {
			URL = "?businessCode=" + BusinessCode
		}
	}
    var win = window.open(ErootPath+"/GalleryDisplay.asp"+URL,"GalleryWin",OpenMode)
    if (win) {
    	window.blur 
    	win.focus()
    }
}

  function askFriend(friendCode){
	var OpenMode = "top=150,left=200,titlebar=0,height=375,width=330,toolbar=0,location=0,directories=0,menubar=0,resizable=1,status=1"
	var win = window.open(rootPath+"allSites/userinterface/askFriend.asp?friendCode="+friendCode,"tellWin",OpenMode)
    if (win) {
    	win.focus()
    	window.blur 
    }
  }  

  function addFriend(friendCode){
	var OpenMode = "top=150,left=200,titlebar=0,height=375,width=330,toolbar=0,location=0,directories=0,menubar=0,resizable=1,status=1"
	var win = window.open(rootPath+"allSites/eng/userinterface/updateFriend.asp?friendCode="+friendCode,"tellWin",OpenMode)
    if (win) {
    	win.focus()
    	window.blur 
    }
  }  

