﻿// JScript File
var google_adnum = 0;

/////////////////////////////////////////
// General
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


String.prototype.isValidMail = function() {
        var _email = this;
        var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
        return emailReg.test(_email);
}

/*
 showForgotPass()
 Function opens forgotPassword page in popup window
*/
function showForgotPass()
{
    window.open('/forgot_password.aspx','forgotPSW','menubar=0,resizable=0,width=350,height=250,location=0,status=0,scrollbars=0')
}


/*
 getHttpRequestObj()
 Function returns HTTP request object for all browsers.
*/
function getHttpRequestObj() {
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

/*
Function opens terns page.
*/
function showTerms()
{
    window.open("/Terms");
}



///////////////////////////////////////
// Business

/*
Function displays the business contact span.
*/
function showBusinContact()
{
    document.getElementById("businContactSpan").style.display = '';
}

/*
Function displays the business website thumbshot span
*/
function showSiteSpan(siteUrl, businId, hide)
{

    var spanObj = document.getElementById('imgSpan' + businId);
    
    if(hide)
    {
        spanObj.style.display = 'none';
    }
    else
    {
        spanObj.innerHTML = '<img src="http://open.thumbshots.org/image.pxf?url=' + escape(siteUrl)  + '" />'
        spanObj.style.display = '';
    }
}


/*
Function displays the grade rounds with the mouse over actions - all in CSS.
*/
function gr(gradeNum, bId, bName, doSelect, goToUrl)
{
        var mul = 22;
        var currWidth = gradeNum * mul;
        var htmStr = '';
        var respURL = "";
        bName = escape(bName);
        respURL = "/Response/WriteBReview.aspx?g={g}&id=" + bId + "&name=" + bName;
        if(goToUrl != undefined)
        {
            respURL = goToUrl;
        }
        if(doSelect)
        {
            respURL = "javascript:setSelectedGrade({g})";
        }
        htmStr += '<div class="ratingblock">';
        htmStr += '<ul id="unit_ul1a" class="unit-rating" style="width:' +  mul*5 + 'px;">';     
        htmStr += '<li id="curr_rating" class="current-rating" style="width:' + currWidth + 'px;"></li>';
        htmStr += '<li><a href="' + respURL.replace("{g}","1") + '" title="כוכב אחד" class="r1-unit rater" rel="nofollow"></a></li>';
        htmStr += '<li><a href="' + respURL.replace("{g}","2") + '" title="שני כוכבים" class="r2-unit rater" rel="nofollow"></a></li>';
        htmStr += '<li><a href="' + respURL.replace("{g}","3") + '" title="שלושה כוכבים" class="r3-unit rater" rel="nofollow"></a></li>';
        htmStr += '<li><a href="' + respURL.replace("{g}","4") + '" title="ארבעה כוכבים" class="r4-unit rater" rel="nofollow"></a></li>';
        htmStr += '<li><a href="' + respURL.replace("{g}","5") + '" title="חמישה כוכבים" class="r5-unit rater" rel="nofollow"></a></li>'; 
        htmStr += '</ul>';
        htmStr += '</div>'; 
        document.write(htmStr);
}

/*
Function sets the selected grade.
*/
function setSelectedGrade(gradeNum)
{
    var mul = 22;
    var currWidth = gradeNum * mul;
    var selObj = document.getElementById("curr_rating");
    selObj.style.width = currWidth +  "px";
    
    try
    {
        selectedGrade = gradeNum;
        setHiddenGradeValue();
    }
    catch(expt){}
}


/*
Function shows vote grade.
*/
function showGradeVote()
{
    document.getElementById('voteGrade').style.display='';
}

/*
Function hides vote grade.
*/
function hideGradeVote()
{
    document.getElementById('voteGrade').style.display='none';
}


/*
Function is called when user is voting for a response (good or bad).
the np supplier form lead.
*/
function responseVote(responseId, voteValue)
{
        document.getElementById("voteValue" + responseId).innerHTML = "תודה, הצבעתך נרשמה";
        var pageURL = "/ajax/responseVote.aspx?voteValue=" + voteValue + "&responseId=" + escape(responseId);
        //location.href = pageURL;
        var xObj = getHttpRequestObj();
        xObj.onreadystatechange =  function () 
            {
                if(xObj.readyState==4)
                {
                   
                }
            };

        xObj.open ("get", pageURL ,false);
        xObj.send(null);
} 


///////////////////////////////////////////////
// NP Lead - Cat Page


/*
Global variables for the np supplier functions.
*/
var globalSupplierIndex = 0;
var npLeadPhoneNum = '';
var npLeadCatGuid = '';
var npLeadSubCatGuid = '';
var npLeadCityGuid = '';
var npLeadNumOfSuppliers = 3;
var startStepHtm = '';


/*
Function creates string with the categories drop down list for
the np supplier form lead.
*/
function getNPleadSubCats(businId, catGuid)
{
    var containerObj = document.getElementById("leadSubCatsSpan" + businId);
    var strArray = []; 
    var pageURL = "/ajax/getNPsubCats.aspx?mainCatGuid=" + escape(catGuid);
    var htmStr = "<img align=\"middle\" src=\"/images/load.gif\" />";
    htmStr = "<span id=\"loading" + businId + "\" style=\"text-align:center;\">" + htmStr + " נא להמתין...</span>";
    containerObj.innerHTML = htmStr;
    var xObj = getHttpRequestObj();
    
    xObj.onreadystatechange =  function () 
	        {
			    if(xObj.readyState==4)
			    { 
	                   var responseText = xObj.responseText.trim();
	                   if(responseText != '')
	                   {
	                       var valsArr = xObj.responseText.split('|');
	                       var i = 0;
	                       var oneArrayCell = '';
	                       var tagName = '';
	                       var tagExternalId = '';
    			
			               strArray.push ('<div style=\"margin-top:3px;\">נא בחר תחום משני:<br />');
			               strArray.push ('<select id="leadSubCatsList' + businId + '" style=\"font-family:arial;font-size:12px;\">');
			               for(i = 0 ; i < valsArr.length ; i++)
			               {
			                    oneArrayCell =  valsArr[i];
			                    tagName = oneArrayCell.split('~')[0];
			                    tagExternalId = oneArrayCell.split('~')[1];
			                    strArray.push ('<option value="' + tagExternalId + '">' + tagName + '</option>');
			               }
			               strArray.push ('</select></div>');
			               
			               containerObj.innerHTML += strArray.join('');
			           }
			      
                }
		    }
	xObj.open ("get", pageURL ,true);
	xObj.send(null);
}


/*
Function creates string with the cities drop down list for
the np supplier form lead.
*/
function getNPleadCities(businId, currentCityGuid)
{
    var containerObj = document.getElementById("leadSubCitiesSpan" + businId);
    var strArray = []; 
    var pageURL = "/ajax/getNPcities.aspx";
    
    var xObj = getHttpRequestObj();
    xObj.onreadystatechange =  function () 
	        {
			    if(xObj.readyState==4)
			    { 
			       var valsArr = xObj.responseText.split('|');
			       var i = 0;
			       var oneArrayCell = '';
			       var cityName = '';
			       var cityExternalId = '';
			       var selStr = '';
			       strArray.push ('<div>נא בחר את אזור מגוריך:<br />');
			       strArray.push ('<select id="leadCitiesList'+ businId +'" style=\"font-family:arial;font-size:12px;\">');
			       for(i = 0 ; i < valsArr.length ; i++)
			       {
			            oneArrayCell =  valsArr[i];
			            cityName = oneArrayCell.split('~')[0];
			            cityExternalId = oneArrayCell.split('~')[1];
			            if(currentCityGuid == cityExternalId && currentCityGuid != '')
			            {
			                selStr = 'selected';
			            }
			            else
			            {
			                selStr = '';
			            }
			            strArray.push ('<option ' + selStr + ' value="' + cityExternalId + '">' + cityName + '</option>');
			       }
			       strArray.push ('</select></div>');
			       strArray.push ('<br /><input style="font-size:12px;" class="generalButton" type="button" id="sendImg' + businId + '" value="שלח" onclick="NPleadLastStep(' + businId + ')">');
			       containerObj.innerHTML = strArray.join('');
			       document.getElementById("loading" + businId).innerHTML = '';
                }
		    }
	xObj.open ("get", pageURL ,true);
	xObj.send(null);
}


/*
Function is called in the first step of sending np supplier form lead.
*/
function NPleadFirstStep(businId, catGuid , cityGuid)
{

    NPleadErr(businId, '');
    startStepHtm = document.getElementById("businLeadDiv" + businId).innerHTML;
    npLeadPhoneNum = document.getElementById("phonePre" + businId).value + "-" + document.getElementById("phoneNum" + businId).value;
    npLeadCatGuid = catGuid;
    npLeadNumOfSuppliers = 3;
    
    if(npLeadPhoneNum.length < 10)
    {
        NPleadErr(businId, "יש להזין מס' טלפון וקידומת");
        return;
    }
    
    document.getElementById("businLeadDiv" + businId).innerHTML = "<span id=\"leadSubCatsSpan" + businId + "\"></span><span id=\"leadSubCitiesSpan" + businId + "\"></span>";
    getNPleadSubCats(businId, catGuid);
    getNPleadCities(businId, cityGuid);
    
    
}

/*
Function is called in the last step of sending np supplier form lead.
*/
function NPleadLastStep(businId)
{

   document.getElementById("sendImg" + businId).src = "/images/load.gif";
   
   NPleadErr(businId, '');
   npLeadSubCatGuid = document.getElementById("leadSubCatsList" + businId).value;
   npLeadCityGuid = document.getElementById("leadCitiesList" + businId).value;
   var pageURL = "/ajax/webService.asp?phoneNumber=" + npLeadPhoneNum.trim() + "&PrimaryExpertise=" + npLeadCatGuid.trim() + "&SecondaryExpertise=" + npLeadSubCatGuid.trim() + "&City=" + npLeadCityGuid.trim() + "&NumOfSuppliers=" + npLeadNumOfSuppliers;
    var xObj = getHttpRequestObj();
    xObj.onreadystatechange =  function () 
	        {
			    if(xObj.readyState==4)
			    { 
			       NPleadCompleated(businId);
                }
		    }
	xObj.open ("get", pageURL ,true);
	xObj.send(null);
    
}

/*
Function displays completed message in np supplier form lead (after it was sent by AJAX).
*/
function NPleadCompleated(businId)
{
  var divObj = document.getElementById("businLeadDiv" + businId);
  divObj.innerHTML = "<div style=\"color:gray;font-size:12px;font-weight:bold;margin:5px;\">פנייתך התקבלה בהצלחה, נציגינו ייצרו עמך קשר בהקדם.</div>"
}


/*
Function displays error in np supplier form lead.
*/
function NPleadErr(businId, errText)
{

    document.getElementById("errSpan-S-" + businId).innerHTML = "<b style=\"color:red;font-size:11px;font-weight:bold;\">" + errText + "</b>";
}


/*
Function hides the phone number error message
in np supplier form lead.
*/
function hideNumberError()
{
    if(globalSupplierIndex == 0)
    {
        try
        {
            document.getElementById("errSpan").innerHTML = "";
            
        }
        catch(e1)
        {} 
        try
        {
            document.getElementById("errSpan2").innerHTML = document.getElementById("errSpan").innerHTML;
        }
        catch(e2)
        {} 
    }
    else
    {
        document.getElementById("errSpan-S-" + globalSupplierIndex).innerHTML = "";
    }       
}


/*
Function displays np supplier image tag
*/
function supImgDisplay(imgObj)
{
    imgObj.style.display = '';

}


/*
Function hides np supplier image tag
*/
function supImgDefault(imgObj)
{
    imgObj.style.display = 'none';

}


//////////////////////////////////////
// Top Message
function closeTopMessagePanel()
{
    document.getElementById("topMessagePanel").style.display = "none";
    createCookie("tguvotTopMessage","1",1/4);
}


function showTopMessagePanel()
{
    
    if(!readCookie("tguvotTopMessage"))
    {
        document.getElementById("topMessagePanel").style.display = "";
    }
    else
    {
        document.getElementById("topMessagePanel").style.display = "none";
    }
    
}

/////////////////////////////////////
// Cookies

function createCookie(name,value,days,ipath)
{
	if (ipath == undefined) 
	{
		ipath = "";
	}
	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=/"+ipath;
}

function readCookie(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 eraseCookie(name)
{
	createCookie(name,"",-1);
}