function expand()
    {
        if (e=document.getElementById('content'))
            {
                e.style.height='1000px';
                if (e=document.getElementById('continue'))
                    e.style.display='none';
                if (e=document.getElementById('click'))
                    e.style.display='none';
                if (e=document.getElementById('caption'))
                    e.style.display='none';
                if (e=document.getElementById('bblinksmall'))
                    e.style.display='none';
                if (e=document.getElementById('bblink'))
                    e.style.display='block';
            }
    }

function where_to_buy(what, callback)
    {
        if (callback)
            {
                if (r=document.getElementById('wtb_results'))
                    {
                        r.innerHTML = what;
                        setCookie( 'wtbw' , document.getElementById('words').value, 7);
                    }
            }
        else
            {
                if (what == 'focus')
                    {
                        if (w=document.getElementById('words'))
                            w.focus();
                    }
                else
                    {       
                        app_name = (what.title == 'fso_ds') ? 'distributor_stores' : 'where_to_buy';
                        p = 'app_name='+app_name+'&words=' + what.value;
                        delayed(5,'index.php', p, 'where_to_buy', 'GET');
                    }
            }
    }

var wait = false;
var timeoutID;
function delayed(h, portal, params, action, method)
{
    f = "communicate('"+portal+"','"+params+"','"+action+"','"+method+"');";

    clearInterval(timeoutID);
    timeoutID = setTimeout(f, 100*h);
}

    
function defaultLoad()
    {
        if (e=document.getElementById("focus")) e.focus();
        loadMap();
    }
/* new registration feature */
var geocoder;
var map;    
function nohttp(caller)
    {
        caller.value = caller.value.replace('http://','');
    }

function loadMap()
    {
        if (!(e=document.getElementById("map_canvas"))) return;
        
        geocoder = new google.maps.Geocoder();
        
        map = new google.maps.Map(e, {
            center: new google.maps.LatLng(40, -95),
            zoom: 3,
            mapTypeId: 'roadmap'
          });
        
        pinpoint();
    }
    
function register_salon(server_text,callback)
    {
        if (callback)
            {
                ary = server_text.split('|');
                if (parseInt(ary[0]) == 1)
                    {
                        alert('Your salon has been added.');
                        document.location = 'http://www.bosleypro.com/';
                    }
                else
                    {
                        alert('Your salon could not be added at this time. Please try again later.');
                    }
                return;
            }
        
        
        if (!can_register(true)) return;
        
        if (!confirm('Are you sure all the information is correct\n\tand the marker on the map is accurate?')) return;
        
        //sBtn.disabled = true;
            
        //alert(lat.value+' '+lng.value);
        params = 'name='+urlencode(sName.value);
        params += '&address='+urlencode(sAddress.value);
        params += '&city='+urlencode(sCity.value);
        params += '&region='+sRegion.options[sRegion.selectedIndex].value;
        params += '&code='+urlencode(sCode.value);
        params += '&phone[]='+urlencode(sP0.value);
        params += '&phone[]='+urlencode(sP1.value);
        params += '&phone[]='+urlencode(sP2.value);
        params += '&website='+urlencode(sWebsite.value);
        params += '&lat='+urlencode(lat.value);
        params += '&lng='+urlencode(lng.value);
        
        //alert(params);
        communicate('index.php?app_name=register_salon',params,'register_salon');
    }

function can_register(show_red)
    {
        if (!got_reg_fields()) return;    
        
        sName.className = sName.className.replace('error','');
        sAddress.className = sAddress.className.replace('error','');
        sCity.className = sCity.className.replace('error','');
        sRegion.className = sRegion.className.replace('error','');
        sCode.className = sCode.className.replace('error','');
        sP0.className = sP0.className.replace('error',''); 
        sP1.className = sP1.className.replace('error','');
        sP2.className = sP2.className.replace('error','');
        
        if (show_red) sBtn.className = sBtn.className.replace('red','green');
        
        if (sName.value == '')
            {
                if (show_red) field_warning(sName);
                return;
            }
            
        if (sAddress.value == '')
            {
                if (show_red) field_warning(sAddress);
                return;
            }
        
        if (sCity.value == '')
            {
                if (show_red) field_warning(sCity);
                return;
            }
        
        if (sRegion.value == '')
            {
                if (show_red) field_warning(sRegion);
                return;
            }
        
        if (sCode.value == '' || sCode.value.length < 5 || !is_numeric(sCode.value))
            {
                if (show_red) field_warning(sCode);
                return;
            }
        
        if (sP0.value == '' || sP0.value.length < 3 || !is_numeric(sP0.value))
            {
                if (show_red) field_warning(sP0);
                return;
            }
        if (sP1.value == '' || sP1.value.length < 3 || !is_numeric(sP1.value))
            {
                if (show_red) field_warning(sP1);
                return;
            }
        if (sP2.value == '' || sP2.value.length < 4 || !is_numeric(sP2.value))
            {
                if (show_red) field_warning(sP2);
                return;
            }
        
        return true;
    }

function field_warning(field)
    {
        field.className += ' error';
        field.focus();
        if (field.type == 'text') field.select();
        sBtn.className = sBtn.className.replace('green','red');
    }

function is_numeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}
    
var sName,sAddress,sCity,sRegion,sCode,sWebsite,sP0,sP1,sP2;
function got_reg_fields()
    {
        if (!(sName=document.getElementById("focus"))) return;
        if (!(sAddress=document.getElementById("r_address"))) return;
        if (!(sCity=document.getElementById("r_city"))) return;
        if (!(sRegion=document.getElementById("r_region"))) return;
        if (!(sCode=document.getElementById("r_code"))) return;
        if (!(sWebsite=document.getElementById("r_website"))) return;
        if (!(sP0=document.getElementById("r_p0"))) return;
        if (!(sP1=document.getElementById("r_p1"))) return;
        if (!(sP2=document.getElementById("r_p2"))) return;
        
        if (!(lat=document.getElementById('lat'))) return; 
        if (!(lng=document.getElementById('lng'))) return;
        
        if (!(sBtn=document.getElementById('submit'))) return;
        
        return true;
    }


    
function pinpoint()
    {        
        if (!got_reg_fields()) return;
        can_register();//clear any field warnings
        
        if (sRegion.options[sRegion.selectedIndex].value == '' || (sAddress.value == '' && sCity.value == '' && sCode.value == '')) return;
        
        var zoom = 7;
        var full = '';
        if (sAddress.value != '')
            {
                full += sAddress.value+' ';
                zoom += 3;
            }
        if (sCity.value != '')
            {
                full += sCity.value+', ';
                zoom += 3;
            }
        
        full += sRegion.options[sRegion.selectedIndex].value+' ';
        
        if (sCode.value != '')
            {
                full += sCode.value+' ';
                zoom += 3;
            }
                       
        codeAddress(sName.value,full,zoom);
    }
    
var marker = null;
function codeAddress(title,address,zoom)
      {
            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                map.setZoom(zoom);
                map.panTo(results[0].geometry.location);
                
                if (marker == null)
                    {
                        marker = new google.maps.Marker({
                          map:map,
                          draggable:true,
                          animation: google.maps.Animation.DROP,
                          position: results[0].geometry.location
                        });
                        google.maps.event.addListener(marker, 'dragend', function() {
                            pos = marker.getPosition();
                            lat.value = pos.lat();
                            lng.value = pos.lng();
                        });
                    }
                else
                    {
                        marker.setPosition(results[0].geometry.location);
                        lat.value = results[0].geometry.location.lat();
                        lng.value = results[0].geometry.location.lng();
                    }                
                    
                
                
                //alert(results[0].geometry.location);
                
                //url = src+'&lat='+results[0].geometry.location.lat()+'&lng='+results[0].geometry.location.lng();
                //downloadUrl(url);
              }
            });
      }
      
function downloadUrl(url) { return; }
        
/* end new registration feature */


function verify(form)
    {
        switch(form)
            {
                case 'register' :
                    if (n=document.getElementById('name'))
                        n.value = urlencode(n.value);
                    if (a=document.getElementById('address'))
                        a.value = urlencode(a.value);
                    if (c=document.getElementById('city'))
                        c.value = urlencode(c.value);
                    if (co=document.getElementById('code'))
                        co.value = urlencode(co.value);    
                    return true;
                break;
            }
        
        alert('Form data could not be verified. Please try again later');
        return false;
    }
    
    
function register()
    {
        if (e=document.getElementById('over'))
            {
                if (prompt('Enter code...')=='000000')
                    e.style.display='none';
            }
        else
            window.open ("http://www.bosleypro.com/index.php?app_name=rt","register","menubar=0,resizable=0,width=750,height=180");
    }
    
function find()
    {
        window.open ("http://www.bosleypro.com/index.php?app_name=wtbt","wtb","menubar=0,resizable=0,width=280,height=350");
    }



    
var searchWait = false;
var searchTimeoutID;
function search(what, callback)
{
    if (callback)
        {
            //alert(what);
            if (sr=document.getElementById('sitesearch'))
                {
                    sr.innerHTML = what;
                    sr.style.display = 'block';
                }
        }
    else
        {            
            //if (what.value != cache) {
                f = "instantSearch('"+what.value+"');";
                
                if (searchWait)
                    {
                        //setTimeout is running. clear it, and reset it
                        clearInterval(searchTimeoutID);
                        searchTimeoutID = setTimeout(f, 200);
                    }
                else
                    {
                        searchWait = true;
                        searchTimeoutID = setTimeout(f, 200);
                    }
            //}
        }
}

function instantSearch(words) {
    params = "mode=xml&submode=search&limit=7&words=" + words;
      
    communicate("index.php", params, 'search', 'GET');
}

function result(text,action)
    {
        switch(action)
            {
                case 'search' :
                    search(text,1);
                break;
                case 'where_to_buy' :
                    where_to_buy(text,1);
                break;
                case 'register_salon' :
                    register_salon(text,1);
                break;
            }
    }

    
function occupyInput(caller, def)
{
    if (caller.value == '')
        caller.value = def;
}
function clearInput(caller, def)
{
    if (caller.value == def)
        caller.value = '';
}

function urlencode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
        output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}
