﻿// This file contains common functions that are used in the site.  

function ExecuteTrigger(triggerId)
{

    var trigger = document.getElementById(triggerId);
    if (trigger != null)
    {
        trigger.click();
    }        
}

function LimitToMaxLength(tb, length, e)
    {
	var keynum;
	if(window.event) {
  		keynum = e.keyCode;
  	}
	else if(e.which) {
  		keynum = e.which;
  	}

        if(keynum  == 8) { return true;}
	if(keynum == 46) { return true;}
	if(keynum == 35) { return true;}
       
        var maxLength = 254;
        if(length) { maxLength = length}

        return tb.value.length <= maxLength;
    }
    
function triggerlogin()
{
    
    ExecuteTrigger("ctl00_CornerLogin_btnSignIn");
}

function returnRefresh(returnVal) {
 window.location.href=window.location.href;
}
 function btnSave_Click()
{            
    var searchTriggers = document.getElementsByName('searchTrigger');
    
    if (searchTriggers.length > 0)
    {
        if(searchTriggers[0] != null)
        {                   
            searchTriggers[0].click();
            return false;
        }
    }
    return true;
}                    

// finds a control that had the given server id, of a the given type
// in the given parent.
//
function findControl(parent, tagName, serverId) {
    var items = parent.getElementsByTagName(tagName);
    
    // walk the items looking for the right guy
    for (var i = 0; i < items.length; i++) {
        var ctl = items[i];
        if (ctl && ctl.id) {
            // check the end of the name.
            //
            var subId = ctl.id.substring(ctl.id.length - serverId.length);
            if (subId == serverId) {
                return ctl;                        
            }
        }
    }   
    return null;
}

function SetCookie(sName, sValue)
{
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function DelCookie(sName)
{
  document.cookie = sName + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function lookAtDate(objectMONTH,objectDAY,objectYEAR)
{
// initializing variables 
// grabing values from the option drop down boxes

	var MONTHItem = objectMONTH.options[objectMONTH.selectedIndex].value;
	var DAYItem = objectDAY.options[objectDAY.selectedIndex].value;
	var YEARItem = objectYEAR.options[objectYEAR.selectedIndex].value;

// If month is equal to Febuary
	if (MONTHItem == "2") 
	{			
// Checking for leap year	
		if (((YEARItem % 4 == 0) && (YEARItem % 100 != 0)) || (YEARItem % 400 == 0))
		{

// if Day 30 or 31 is highlighted when it is a leap year then it highlights day 28		
			if (DAYItem == 30 || DAYItem == 31)
				{
					objectDAY.selectedIndex = 28
				}
		
			objectDAY.length = 29;
			objectDAY.options[28].value = "29"
			objectDAY.options[28].text = "29"
		}else
		{
// same as above sort of		
			if (DAYItem == "31" || DAYItem == "30" || DAYItem == "29")
				{
					objectDAY.selectedIndex = 27
				}
				objectDAY.length = 28
		}
	}else
	{
//If one of these months they all have 31 days	
		if (MONTHItem == "1" || MONTHItem == "3" || MONTHItem == "5" || MONTHItem == "7" || MONTHItem == "8" || MONTHItem == "10" || MONTHItem == "12")
		{

				objectDAY.length = 31
				objectDAY.options[30].value = "31"
				objectDAY.options[30].text = "31"
		}
		else
		{
			if (DAYItem == "31")
//If not they have 30 days			
				{
					objectDAY.selectedIndex = 29
				}

			objectDAY.length = 30
		}

			objectDAY.options[28].value = "29"
			objectDAY.options[28].text = "29"
			objectDAY.options[29].value = "30"
			objectDAY.options[29].text = "30"

	}
	
}

function EvalKeyPress(btnName, e)
{
    var c = e.keyCode; 
    if (c == 13)
    {
        if(document.getElementById(btnName).tagName.toLowerCase() == 'input')
        {
            document.getElementById(btnName).click();
        }
        else
        {
            __doPostBack(btnName.replace(/_/g, '$'), '');
        }
        return false;
    }
    else
    {
        return true;
    }
}

function listBoxUpdate(listBox1, listBox2, hiddenfield, updateFrom)
{
    if (listBox2.length == 1)
    {
        if (listBox2.options[0].text == "All Neighborhoods" || listBox2.options[0].text == "All Cuisines" || listBox2.options[0].text == "All Attributes")
        {
            listBox2.remove(0);
        }
    }

    if (updateFrom == 0)
    {
        for (i=0; i<listBox1.length; i++)
        {
            if (listBox1.options[i].text != "All Neighborhoods" && listBox1.options[i].text != "All Cuisines" && listBox1.options[i].text != "All Attributes")
            {
                var transferOption = new Option(listBox1.options[i].text, listBox1.options[i].value);
                
                listBox2.options.add(transferOption);
                listBox1.remove(i);
                i = -1;
            }
        }
    }
    else
    {
        for (i=0; i<listBox1.length; i++)
        {
            if (listBox1.options[i].selected)
            {
                if (listBox1.options[i].text != "All Neighborhoods" && listBox1.options[i].text != "All Cuisines" && listBox1.options[i].text != "All Attributes")
                {               
                    var transferOption = new Option(listBox1.options[i].text, listBox1.options[i].value);
                    listBox2.options.add(transferOption);
                    listBox1.remove(i);
                    i = -1;
                }
            }
        }
    }
    
    //Sort box 1
    arrTexts = new Array(); 
    arrValues = new Array(); 
    arrOldTexts = new Array(); 

    for(i=0; i<listBox1.length; i++) 
    { 
        arrTexts[i] = listBox1.options[i].text; 
        arrValues[i] = listBox1.options[i].value; 

        arrOldTexts[i] = listBox1.options[i].text; 
    } 

    arrTexts.sort(); 

    for(i=0; i<listBox1.length; i++) 
    { 
        listBox1.options[i].text = arrTexts[i]; 
        for(j=0; j<arrOldTexts.length; j++) 
        { 
            if (arrTexts[i] == arrOldTexts[j]) 
            { 
                listBox1.options[i].value = arrValues[j];
                arrOldTexts.splice(j,1); 
                arrValues.splice(j,1);
                j = arrOldTexts.length; 
            } 
        } 
    } 
    
    //Sort box 2
    arrTexts = new Array(); 
    arrValues = new Array(); 
    arrOldTexts = new Array(); 

    for(i=0; i<listBox2.length; i++) 
    { 
        arrTexts[i] = listBox2.options[i].text; 
        arrValues[i] = listBox2.options[i].value; 

        arrOldTexts[i] = listBox2.options[i].text; 
    } 

    arrTexts.sort(); 

    for(i=0; i<listBox2.length; i++) 
    { 
        listBox2.options[i].text = arrTexts[i]; 
        for(j=0; j<arrOldTexts.length; j++) 
        { 
            if (arrTexts[i] == arrOldTexts[j]) 
            { 
                listBox2.options[i].value = arrValues[j]; 
                arrOldTexts.splice(j,1); 
                arrValues.splice(j,1);
                j = arrOldTexts.length; 
            } 
        } 
    } 
    
    //Update the hiddenfield
    if (updateFrom == 2)
    {
        hiddenfield.value = "";
        for(i=0; i<listBox2.length; i++) 
        { 
            hiddenfield.value += listBox2.options[i].value + ',';
        }
    }
    else
    {
        hiddenfield.value = "";
        for(i=0; i<listBox1.length; i++) 
        { 
            hiddenfield.value += listBox1.options[i].value + ',';
        }
        
        if (listBox1.length == 0)
        {
            if (listBox1.id.indexOf("lstNeighborhood") > -1)
            {
                var transferOption = new Option("All Neighborhoods", "-1");
                listBox1.options.add(transferOption);
            }
            else if (listBox1.id.indexOf("lstCuisine") > -1)
            {
                var transferOption = new Option("All Cuisines", "-1");
                listBox1.options.add(transferOption);
            }
            else if (listBox1.id.indexOf("lstAttributes") > -1)
            {
                var transferOption = new Option("All Attributes", "-1");
                listBox1.options.add(transferOption);
            }
        }
    }
    
    return false;
}

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}

function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}


//mxf added start
 function validateLength(oSrc, args){
    var lstCuisine_select =document.getElementById('<%=lstCuisine_select.ClientID%>')
    args.IsValid = (lstCuisine_select.options.length <= 3);
}

    function getCity(el,cityControl)
    {
        var zipCode = el.value;
        if(zipCode == null || zipCode == '') { return; }
        if(el.id == "ProfileEdit1_ZipHome")
        {
            getCityByZipCode(zipCode,setCity);
        }
        else
        {
            getCityByZipCode(zipCode,setCity);
        }
    }
    function setCity(val)
    {
        document.getElementById("ctl00_MainContentPlaceHolder_ProfileEdit_hid_metro").value =val;
    }
    
    function listCuisineUpdate(listBox1, listBox2, hiddenfield, updateFrom)
    {
        if (listBox1.length == 1)
        {
            if (listBox1.options[0].text == "No Cuisines")
            {
                listBox1.remove(0);
            }
        }  
        if(updateFrom==2)
        {   
            if (listBox2.length> 0)
            {
                for (i=0; i<listBox2.length; i++)
                {
                    if (listBox2.options[i].text != "No Cuisines" && listBox2.options[i].selected)
                    {
                        var transferOption = new Option(listBox2.options[i].text, listBox2.options[i].value);
                        listBox1.options.add(transferOption);
                        listBox2.remove(i);
                        i = -1;
                    }
                }
                hiddenfield.value = "";
                for(i=0; i<listBox1.length; i++) 
                {             
                    hiddenfield.value += listBox1.options[i].value + ',';            
                }
            }
        }
        if(updateFrom==0)
        {
            for (i=0; i<listBox1.length; i++)
            {
                if (listBox1.options[i].text != "No Cuisines")
                {
                    var transferOption = new Option(listBox1.options[i].text, listBox1.options[i].value);
                    listBox2.options.add(transferOption);
                    listBox1.remove(i);
                    i = -1;
                }
            }
            hiddenfield.value = ""; 
        }
        if(updateFrom==1)
        {
            for (i=0; i<listBox1.length; i++)
            {
                if (listBox1.options[i].text != "No Cuisines" && listBox1.options[i].selected)
                {
                   var transferOption = new Option(listBox1.options[i].text, listBox1.options[i].value);
                    listBox2.options.add(transferOption);
                    listBox1.remove(i);
                    i = -1;
                }
               
            }
            hiddenfield.value = "";
            for(i=0; i<listBox1.length; i++) 
            { 
                //alert(listBox1.options[i].value);
                hiddenfield.value += listBox1.options[i].value + ',';            
            }
         }
         SortItem(listBox1,listBox2);
        return false;
    }
    
    function SortItem(listBox1,listBox2)
    {
    //Sort box 1
    arrTexts = new Array(); 
    arrValues = new Array(); 
    arrOldTexts = new Array(); 

    for(i=0; i<listBox1.length; i++) 
    { 
        arrTexts[i] = listBox1.options[i].text; 
        arrValues[i] = listBox1.options[i].value; 

        arrOldTexts[i] = listBox1.options[i].text; 
    } 

    arrTexts.sort(); 

    for(i=0; i<listBox1.length; i++) 
    { 
        listBox1.options[i].text = arrTexts[i]; 
        for(j=0; j<arrOldTexts.length; j++) 
        { 
            if (arrTexts[i] == arrOldTexts[j]) 
            { 
                listBox1.options[i].value = arrValues[j];
                arrOldTexts.splice(j,1); 
                arrValues.splice(j,1);
                j = arrOldTexts.length; 
            } 
        } 
    } 
    
    //Sort box 2
    arrTexts = new Array(); 
    arrValues = new Array(); 
    arrOldTexts = new Array(); 

    for(i=0; i<listBox2.length; i++) 
    { 
        arrTexts[i] = listBox2.options[i].text; 
        arrValues[i] = listBox2.options[i].value; 

        arrOldTexts[i] = listBox2.options[i].text; 
    } 

    arrTexts.sort(); 

    for(i=0; i<listBox2.length; i++) 
    { 
        listBox2.options[i].text = arrTexts[i]; 
        for(j=0; j<arrOldTexts.length; j++) 
        { 
            if (arrTexts[i] == arrOldTexts[j]) 
            { 
                listBox2.options[i].value = arrValues[j]; 
                arrOldTexts.splice(j,1); 
                arrValues.splice(j,1);
                j = arrOldTexts.length; 
            } 
        } 
    } 
    }

    var valueControl;
    function onMouseOver(cate,num) 
    {
       
        for(i=1;i<=5;i++)
        {
            if(num>=i)
                document.getElementById(cate+i).className ="flv_div_Selected"; 
            else
            {
                if(i==5)
                    document.getElementById(cate+i).className ="flv_div_notSelected1";
                else
                {
                    document.getElementById(cate+i).className ="flv_div_notSelected"; 
                }
            }
        }
    } 
    function onMouseOut(cate,control) 
    {    
         var ctlTemp=valueControl;
         valueControl=control;
         for(i=1;i<=5;i++) 
         {        
            if(i==5)
                document.getElementById(cate+i).className ="flv_div_notSelected1";
            else   
                document.getElementById(cate+i).className ="flv_div_notSelected";
         }
        
            //alert(ctlTemp!=valueControl);
            if(document.getElementById(valueControl).value.length!=0) 
            {  
                for(i=1;i<=5;i++)
                {
                    if(document.getElementById(valueControl).value>=i)
                    {
                        document.getElementById(cate+i).className ="flv_div_Selected"; 
                    }
                }
            } 
        
         
    } 
    function onClick(cate,num,control) 
    {    
        valueControl=control;
        document.getElementById(valueControl).value=num;      
    }
    function loadRate(ctlPrefix)
    {  
        var arr=new Array('Rich','Fried','Spicy','Sweet','Light','Adven');
        for(j=0;j<arr.length;j++)
        {
        
            if(document.getElementById(ctlPrefix+arr[j]+'Value') && document.getElementById(ctlPrefix+arr[j]+'Value').value.length!=0) 
            {
                for(i=1;i<=5;i++)
                {
                    if(document.getElementById(ctlPrefix+arr[j]+'Value').value>=i)
                    {
                        document.getElementById(arr[j]+i).className ="flv_div_Selected";                         
                    }
                }
            } 
        }
    }
//mxf add ended