var oForm = null, vQueryHeading, vBusinessName;

function showMyDPlaces()
{    
    $("mydPlacesList").style.display = ($("mydPlacesList").style.display == 'block')? 'none' : 'block';
    if ($('relatedResultsSelect')) $('relatedResultsSelect').style.visibility = ($("mydPlacesList").style.display == 'block' & $('mydPlacesList').clientHeight > 50 & browser == 'MSIE')? 'hidden' : 'visible';
}

function checkSearchEngine(defaultQueryHeading, defaultBusinessName, defaultWhereName)
{   
    var queryHeading = document.forms['mainForm']._queryHeading;
    var businessName = document.forms['mainForm']._business_name;
    var whereName = document.forms['mainForm']._where_name;
    if ((queryHeading.value == defaultQueryHeading || queryHeading.value == '') & (businessName.value == defaultBusinessName || businessName.value == ''))
    {
        if ($("searchEngineErr")) 
        {
            $("searchEngineErr").style.display = "block";
        }
        else 
        {
            alert('מלא/י אחד מהשדות: "שם העסק" או "סיווג" לביצוע החיפוש');
        }
        return false;
    }
    queryHeading.value = (queryHeading.value == defaultQueryHeading)? '' : queryHeading.value;
    businessName.value = (businessName.value == defaultBusinessName)? '' : businessName.value;
    whereName.value = (whereName.value == defaultWhereName)? '' : whereName.value;
    if (myd_isMatchCookie('MydMapZones') && document.mainForm) checkIsMyPlace(whereName);
    fTags2Entites(queryHeading, businessName, whereName);
    return true;
}

function fTags2Entites(){
	var vArgs = fTags2Entites.arguments;	
	for (var i=0; i<vArgs.length; i++){
		if (vArgs[i].value.match(/</)!=null) vArgs[i].value=vArgs[i].value.replace(/</g,'&lt;');
		if (vArgs[i].value.match(/>/)!=null) vArgs[i].value=vArgs[i].value.replace(/>/g,'&gt;');
	}
}

function checkIsMyPlace(whereName)
{    
    var userZones = myd_getCookie("MydMapZones");
    var zones = userZones.split(";");
    for (var i = 0; i < zones.length; i++)
    {        
        if (zones[i].indexOf("#") != -1)
        {
            zone = zones[i].split("#");            
            if (zone[0] == whereName.value) document.forms['mainForm'].isMyDPlace.value = zone[1];
        }
    }    
}

function fSetPage()
{
    if (!oForm) return false;
	if((oForm._queryHeading.value != vQueryHeading && oForm.headingCode!=null && oForm.headingCode.value=='') || oForm._business_name.value != vBusinessName){
		oForm.page.value = 'Summaries';
	}
	return true;
}

function fInitSearch(){
	oForm = document.forms['mainForm'];		
	if (!oForm) return;
	// set initial qeuryHeading & business_name values for use in fSetPage function
	vQueryHeading = oForm._queryHeading.value;
	if(oForm._business_name)
		vBusinessName = oForm._business_name.value;
	else
		vBusinessName="";
	if(oForm.isMyDPlace)
		oForm.isMyDPlace.value = "0";
}

var vOpenCustBubbleIndex = -1;
// 0=name , 1=address, 2=phone , 3=info ,4=contact ,5=kupon ,6=freeCall, 7=webSite, 8=video, 9=picture ,10=picWidth , 11=picHeight
function fCreateBubbleHTML(index, custId)
{
	vCustInfoHash[custId][10] = parseInt(vCustInfoHash[custId][10]);
	vCustInfoHash[custId][11] = parseInt(vCustInfoHash[custId][11]);
	var fSize = "16px";
	var buttonMarginTop = "25";
	if(vCustInfoHash[custId][0].length >25)
	{
		fSize = "14px";
		buttonMarginTop = "25";
	}
	
	var style ="<style type=\"text/css\">";
	style += "#mainDiv{border:0px solid red;height:146px!important;height:149px;width:248px;text-align:right; font-size:13px; color:#000000; font-weight:normal;background-color:#ffffff}";
	style += "#topLine{height:20px;}";
	style += "#businessName{font-size:" + fSize + ";font-weight: bold;float:right;width:180px;height:auto}";
	style += "#businessName A{color: red;text-decoration:none;}";
	style += "#voteDiv{font-size:10px;float:left;width:50px;height:auto;}";
	style += "#businessAddress{float:right;height:auto;width:150px;}";
	style += "#businessPhone{font-size:" + fSize + ";font-weight:bold;float:right;width:120px}";
	style +="#businessFreeCall{height:27px;float:right;clear:right;width:120px;}";
	style +="#businessFreeCall A{height:100%;color:black;text-decoration:none;white-space:nowrap;}";
	style += "#businessButtons{height:30px;float:right;width:100%;}";
	style +="#picHolder{float:left;height:75px;width:auto;border:0px solid red;margin-left:1px;}";
	style +="IMG.icon { margin-left:5px;}";
	var  vImgSize = '';
	if(vCustInfoHash[custId][9] !=""){	// if has picture
		if(vCustInfoHash[custId][10] >vCustInfoHash[custId][11]){
			if (vCustInfoHash[custId][10] > 75)
				vImgSize = ' width="75"';
			else
				vImgSize = ' width="'+vCustInfoHash[custId][10]+'"';
			//style +="#picHolder IMG{height:75px;}";
		} else {
			if (vCustInfoHash[custId][11] > 75)
				vImgSize = ' height="75"';
			else
				vImgSize = ' height="'+vCustInfoHash[custId][10]+'"';
			//style +="#picHolder IMG{width:75px;}";
		}
	}
	//style +="#picHolder IMG{height:70px;}";
	style +="</style>";
	var buttons ="";
	//info button
	if(vCustInfoHash[custId][3] !="")
		
		buttons += "<a href='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][3] + "\")' title='מידע נוסף'><img border='0' src='/Hebrew/images/Skins/icons/info.gif' class='icon'/></a>";
	
	//map button
	buttons += "<a href='javascript:setCenterPosition("+index+", 0);hideGeographicDiv()' title='מפה'><img border='0' src='/Hebrew/images/Skins/icons/map.gif' class='icon'/></a>";
	
	//contact button
	if(vCustInfoHash[custId][4] != ""){
		vCustInfoHash[custId][4] = vCustInfoHash[custId][4].replace(/\%22/g, '%26quot%3B');
		buttons += "<a href='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][4] + "\")' title='יצירת קשר'><img border='0' src='/Hebrew/images/Skins/icons/contact.gif' class='icon'/></a>"; }
	//kupon button
	if(vCustInfoHash[custId][5] != "")
	{
		if (vCustInfoHash[custId][5].search("%22") != -1)
			vCustInfoHash[custId][5] = vCustInfoHash[custId][5].replace('%22','%26quot%3B');
		buttons += "<a href ='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][5] + "\")' title='קופון'><img border='0' src='/Hebrew/images/Skins/icons/cupon.gif' class='icon'/></a>";
	}
	//site button
	if(vCustInfoHash[custId][7] != "")
		buttons += "<a href ='" + vCustInfoHash[custId][7] + "' target='_blank' title='אתר'><img border='0' src='/Hebrew/images/Skins/icons/site.gif' class='icon'/></a>";
	//video button
	if(vCustInfoHash[custId][8] != "")
	{
		
		if (vCustInfoHash[custId][8].search("%22") != -1)
			vCustInfoHash[custId][8] = vCustInfoHash[custId][8].replace('%22','%26quot%3B');
		buttons += "<a href ='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][8] + "\")' title='וידאו'><img border='0' src='/Hebrew/images/Skins/icons/video.gif' class='icon'/></a>";
		
	}	

	var html ="<html><head>" +  style + "</head> <body>" ;
	html += "<div id='mainDiv'>";
	html += "<div id='closeDiv'><a href ='javascript:removeInfoOverlay()'><img width='11' height='11' border='0' alt='' src='/Hebrew/images/Skins/icons/close_x.gif'/></a></div>";
	html += "<div id='topLine'>"
	html += "<div id='businessName'>" 
		if(vCustInfoHash[custId][3] !="")
		{	
			html += "<a href='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][3] + "\")'>"
			html += vCustInfoHash[custId][0];
			html += "</a>";
		}
		else
		{
			html += vCustInfoHash[custId][0];
		}
	html += "</div>";
	if (vCustInfoHash[custId][14].search("%22") != -1)
			vCustInfoHash[custId][14] = vCustInfoHash[custId][14].replace('%22','%26quot%3B');
	if (custId != 80044627)
	{
	    html+="<div class='stamps' >"
	    html+="<a class='grade' style='float:right;' id='aGrade"+custId
	    html+="' href='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][14] + "\")'"
	    html+="rel='nofollow' title='דרג' >"
	    html+="<span id='gr" + custId + "'  nowrap='nowrap'>"
	    if(vCustInfoHash[custId][15]!="0")
	    {

		    html+= "דירגו (" + vCustInfoHash[custId][15] + ")"; 
	    }
	    else
	    {
		    html+= "דרג";
	    }			
	    html+= "</span></a></div>";
	}
	html+= "</div>"
	html+="<div style='font-weight:bold'>" + vCustInfoHash[custId][16] + "</div>";
	html +="<div id='businessAddress'>" + vCustInfoHash[custId][1] +  "</div>";
	html +="<div id='picHolder'>";
	if(vCustInfoHash[custId][9] !="")
		html +="<img src='" + vCustInfoHash[custId][9] + "' "+vImgSize+" id='bizImg'/>";
	html+="</div>"
	
	if (custId != 80044627)
	    if (vCustInfoHash[custId][2].substring(0, 4) == 'http')
	        html += "<div id='businessPhone'><img alt='טלפון' src='" + vCustInfoHash[custId][2] + "' /></div>"
	    else
	        html += "<div id='businessPhone'>" + vCustInfoHash[custId][2] + "</div>";
	
	html += "<div id='businessFreeCall'>";
	
	if(vCustInfoHash[custId][6] !="" && !(custId =='80028073' || custId == '80028072'))
	{
		if (vCustInfoHash[custId][6].search("%22") != -1)
			vCustInfoHash[custId][6] = vCustInfoHash[custId][6].replace('%22','%26quot%3B');
        html += "<img src='/Hebrew/images/Skins/icons/click2call.jpg'  border='0'/>"
		html += "<a href='javascript:openExInfoWindow("+index+",\"" + vCustInfoHash[custId][6] + "\")'>";
		html += " <span style='padding-top:5px;height:100%;cursor:hand;font-weight:bold;text-decoration:underline'>הקלק ודבר חינם</span></a>";
	}
	html += "</div>";
	html += "<div id='businessButtons'>" + buttons  + "</div>";
	html += "</div></body></html>";
	vOpenCustBubbleIndex = index;
	return html;
}

function drawCircle()
{
	
	if(dr!=0 && dx!=0 && dy!=0 )//draw
	{
		circle = new Object();
		circle.radius = dr;
		circle.x = dx;
		circle.y =dy;
		mapWidth = 466;
		mapHeight = 393;
		
	} 
	else//MyD
	{
		var userZones = myd_getCookie("MydMapZones");
		if(userZones)
		{
			userZones = userZones.split(";");
			for (var i = 0; i < userZones.length; i++)
			{        
				if (userZones[i].indexOf("#") != -1)
				{
					var zone = userZones[i].split("#");
					if( zone[1]==areaCode)
					{
						circle = new Object();
						circle.radius = zone[4];
						circle.x = zone[2];
						circle.y = zone[3];
						mapWidth = 466;
						mapHeight = 393;
						break;
					}	
				}
			}
		}
	}	
}

function changeAddress(showDiv, unShowDiv, searchLink, searchLinkTop)
{
	
	$(showDiv).style.display = "block";
    $(unShowDiv).style.display = "none";  
}


function fGotoHeading(){
	var oHeadings = document.forms["headingResultsForm"].headings;
	if(oHeadings[oHeadings.selectedIndex].value == "-1")return;
	
	document.location.href = oHeadings[oHeadings.selectedIndex].value;
}

function checkZapForm()
{
    if (document.getElementsByName('_ZapKeywords')[0].value == '')
    {
        alert('יש למלא את שדה החיפוש');
        return false;
    }
    return true;
}

function deleteText(field,defaultTxt)
{
	if(field.value==defaultTxt)
		field.value="";
}
function setText(field,defaultTxt)
{
	if(field.value=="")
		field.value=defaultTxt;
}
