﻿var BodyID = '';
function Setup(iBodyID) {
    BodyID = iBodyID
}
function MasterPageSetup() {
    var ViewportWidth;
    var ViewportHeight;
    if (typeof window.innerWidth != 'undefined') {
        ViewportWidth = window.innerWidth,
        ViewportHeight = window.innerHeight
    }
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        ViewportWidth = document.documentElement.clientWidth,
        ViewportHeight = document.documentElement.clientHeight
    }
    else {
        ViewportWidth = document.getElementsByTagName('body')[0].clientWidth,
        ViewportHeight = document.getElementsByTagName('body')[0].clientHeight
    }

    var HeaderTop = 0;
    var HeaderLeft = 0;
    var HeaderWidth = 0;
    var HeaderHeight = 0;
    var MenuTop = 0;
    var MenuLeft = 0;
    var MenuWidth = 0;
    var MenuHeight = 0;
    var ContentTop = 0;
    var ContentLeft = 0;
    var ContentWidth = 0;
    var ContentHeight = 0;
    var FooterTop = 0;
    var FooterLeft = 0;
    var FooterWidth = 0;
    var FooterHeight = 0;

    //Get Window Positions
    var Header = document.getElementById('header');
    var Menu = document.getElementById('menuContainer');
    var Content = document.getElementById('content');
    var Footer = document.getElementById('footer');
    if (Header && Menu && Content && Footer) {
        if (Header.offsetTop) {
            HeaderTop = Header.offsetTop;
        }
        else if (Header.style.pixelHeight) {
            HeaderTop = Header.style.pixelTop;
        }
        if (Header.offsetLeft) {
            HeaderLeft = Header.offsetLeft;
        }
        else if (Header.style.pixelLeft) {
            HeaderLeft = Header.style.pixelLeft;
        }
        if (Header.offsetWidth) {
            HeaderWidth = Header.offsetWidth;
        }
        else if (Header.style.pixelWidth) {
            HeaderWidth = Header.style.pixelWidth;
        }
        if (Header.offsetHeight) {
            HeaderHeight = Header.offsetHeight;
        }
        else if (Header.style.pixelHeight) {
            HeaderHeight = Header.style.pixelHeight;
        }
        if (Menu.offsetTop) {
            MenuTop = Menu.offsetTop;
        }
        else if (Menu.style.pixelHeight) {
            MenuTop = Menu.style.pixelTop;
        }
        if (Menu.offsetLeft) {
            MenuLeft = Menu.offsetLeft;
        }
        else if (Menu.style.pixelLeft) {
            MenuLeft = Menu.style.pixelLeft;
        }
        if (Menu.offsetWidth) {
            MenuWidth = Menu.offsetWidth;
        }
        else if (Menu.style.pixelWidth) {
            MenuWidth = Menu.style.pixelWidth;
        }
        if (Menu.offsetHeight) {
            MenuHeight = Menu.offsetHeight;
        }
        else if (Menu.style.pixelHeight) {
            MenuHeight = Menu.style.pixelHeight;
        }
        if (Content.offsetTop) {
            ContentTop = Content.offsetTop;
        }
        else if (Content.style.pixelHeight) {
            ContentTop = Content.style.pixelTop;
        }
        if (Content.offsetLeft) {
            ContentLeft = Content.offsetLeft;
        }
        else if (Content.style.pixelLeft) {
            ContentLeft = Content.style.pixelLeft;
        }
        if (Content.offsetWidth) {
            ContentWidth = Content.offsetWidth;
        }
        else if (Content.style.pixelWidth) {
            ContentWidth = Content.style.pixelWidth;
        }
        if (Content.offsetHeight) {
            ContentHeight = Content.offsetHeight;
        }
        else if (Content.style.pixelHeight) {
            ContentHeight = Content.style.pixelHeight;
        }
        if (Footer.offsetTop) {
            FooterTop = Footer.offsetTop;
        }
        else if (Footer.style.pixelHeight) {
            FooterTop = Footer.style.pixelTop;
        }
        if (Footer.offsetLeft) {
            FooterLeft = Footer.offsetLeft;
        }
        else if (Footer.style.pixelLeft) {
            FooterLeft = Footer.style.pixelLeft;
        }
        if (Footer.offsetWidth) {
            FooterWidth = Footer.offsetWidth;
        }
        else if (Footer.style.pixelWidth) {
            FooterWidth = Footer.style.pixelWidth;
        }
        if (Footer.offsetHeight) {
            FooterHeight = Footer.offsetHeight;
        }
        else if (Footer.style.pixelHeight) {
            FooterHeight = Footer.style.pixelHeight;
        }
        ChangeCSS("#content", "min-height", (ViewportHeight - HeaderHeight - MenuHeight - FooterHeight - 10) + "px");
    }
    window.onresize = MasterPageSetup;
}

function ChangeCSS(CSSClass, Element, ElementValue) {
    var cssRules;
    var added = false;
    for (var S = 0; S < document.styleSheets.length; S++) {
        if (document.styleSheets[S]['rules']) {
            cssRules = 'rules';
        }
        else if (document.styleSheets[S]['cssRules']) {
            cssRules = 'cssRules';
        }
        else {
            //no rules found... browser unknown
        }
        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
            if (document.styleSheets[S][cssRules][R].selectorText == CSSClass) {
                if (document.styleSheets[S][cssRules][R].style[Element]) {
                    document.styleSheets[S][cssRules][R].style[Element] = ElementValue;
                    added = true;
                    break;
                }
            }
        }
        if (!added) {
            if (document.styleSheets[S].insertRule) {
                document.styleSheets[S].insertRule(CSSClass + ' { ' + Element + ': ' + ElementValue + '; }', document.styleSheets[S][cssRules].length);
            }
            else if (document.styleSheets[S].addRule) {
                document.styleSheets[S].addRule(CSSClass, Element + ': ' + ElementValue + ';');
            }
        }
    }
}

function SearchSiteOnEnter(myfield, e) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;
    if (keycode == 13) {
        var SearchBox = document.getElementById('q');
        if (SearchBox.value != 'search accent surfaces' && SearchBox.value != '') {
            var SearchValue = URLEncode(SearchBox.value)
            SearchSite(SearchValue);
        }
        return false;
    }
    else
        return true;
}

function URLEncode(clearString) {
    var encodedString = '';
    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] != '') {
            encodedString += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                encodedString += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                encodedString += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return encodedString;
}

function URLDecode(encodedString) {
    // Replace + with ' '
    // Replace %xx with equivalent character
    // Put [ERROR] in output if %xx is invalid.
    var HEXCHARS = "0123456789ABCDEFabcdef";
    var decodedString = "";
    var i = 0;
    while (i < encodedString.length) {
        var ch = encodedString.charAt(i);
        if (ch == "+") {
            decodedString += " ";
            i++;
        } else if (ch == "%") {
        if (i < (encodedString.length - 2)
					&& HEXCHARS.indexOf(encodedString.charAt(i + 1)) != -1
					&& HEXCHARS.indexOf(encodedString.charAt(i + 2)) != -1) {
            decodedString += unescape(encodedString.substr(i, 3));
                i += 3;
            } else {
            alert('Bad escape combination near ...' + encodedString.substr(i));
                decodedString += "%[ERROR]";
                i++;
            }
        } else {
            decodedString += ch;
            i++;
        }
    }
    return decodedString;
};

function SearchSiteOnClick() {
    var SearchBox = document.getElementById('q');
    if (SearchBox.value != 'search accent surfaces' && SearchBox.value != '') {
        var SearchValue = URLEncode(SearchBox.value)
        SearchSite(SearchValue);
    }
    return false;
}

function SearchSite(SearchValue) {
    document.location.href = 'http://www.accentsurfaces.com/SearchResults.aspx?cx=014609037679206639306:ipujtvhprh0&cof=FORID:9&ie=UTF-8&q=' + SearchValue + '&sa=Search';
}

var myGClientGeocoder;
var TextBox_Address;
var TextBox_Street;
var TextBox_PostalCode;
var TextBox_Locality;
var TextBox_SubAdministrativeArea;
var TextBox_AdministrativeArea;
var TextBox_Country;
var TextBox_Longitude;
var TextBox_Latitude;
var Button_Server;

function GClientGeocoderInitialize() {
    myGClientGeocoder = new GClientGeocoder();
}

function GClientGeocoderGetAddress(TextBox_Address_ID, TextBox_Street_ID, TextBox_PostalCode_ID, TextBox_Locality_ID, TextBox_SubAdministrativeArea_ID, TextBox_AdministrativeArea_ID, TextBox_Country_ID, TextBox_Longitude_ID, TextBox_Latitude_ID, Button_ServerID) {
    TextBox_Address = document.getElementById(TextBox_Address_ID);
    TextBox_Street = document.getElementById(TextBox_Street_ID);
    TextBox_PostalCode = document.getElementById(TextBox_PostalCode_ID);
    TextBox_Locality = document.getElementById(TextBox_Locality_ID);
    TextBox_SubAdministrativeArea = document.getElementById(TextBox_SubAdministrativeArea_ID);
    TextBox_AdministrativeArea = document.getElementById(TextBox_AdministrativeArea_ID);
    TextBox_Country = document.getElementById(TextBox_Country_ID);
    TextBox_Longitude = document.getElementById(TextBox_Longitude_ID);
    TextBox_Latitude = document.getElementById(TextBox_Latitude_ID);
    Button_Server = document.getElementById(Button_ServerID);
    if (TextBox_Street && TextBox_Locality && TextBox_SubAdministrativeArea && TextBox_AdministrativeArea && TextBox_PostalCode && TextBox_Country) {
        var address = TextBox_Street.value + ', ' + TextBox_Locality.value + ', ' + TextBox_AdministrativeArea.value + ' ' + TextBox_PostalCode.value;
        myGClientGeocoder.getLocations(address, GClientGeocoderPopulateAddress);
    }
}

function GClientGeocoderPopulateAddress(response) {
    if (!response || response.Status.code != 200) {
        TextBox_Longitude.value = '0';
        TextBox_Latitude.value = '0';
    } else {
        if (TextBox_Address && TextBox_Street && TextBox_Locality && TextBox_SubAdministrativeArea && TextBox_AdministrativeArea && TextBox_PostalCode && TextBox_Country) {
            place = response.Placemark[0];
            try {
                TextBox_Address.value = place.address;
                TextBox_Street.value = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                TextBox_PostalCode.value = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                TextBox_Locality.value = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                TextBox_SubAdministrativeArea.value = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
                TextBox_AdministrativeArea.value = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
                TextBox_Country.value = place.AddressDetails.Country.CountryName;
                TextBox_Longitude.value = place.Point.coordinates[0];
                TextBox_Latitude.value = place.Point.coordinates[1];
            }
            catch (err) {
            }
        }
    }
    if (Button_Server) {
        Button_Server.click();
    }
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

function ConfirmFormClose() {
    var qResponse = confirm('You have modified data on this form since last saving.  If you close this form, any changes since your last save will be lost.  To save your changes, click cancel to return to the form, and save your changes.');
    if (qResponse == true) {
        return true;
    }
    else {
        return false;
    }
}

function ValidateEmail(TextBox_Email, ErrorMessage) {
    EmailPattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    if (!EmailPattern.test(TextBox_Email.value)) {
        alert(ErrorMessage);
        TextBox_Email.focus();
        TextBox_Email.select();
        return false;
    }
    return true;
}

function ClickButtonOnEnter(e, ButtonID) {
    var oButton = document.getElementById(ButtonID);
    if (oButton) {
        if (window.event) // IE
        {
            oKeyCode = e.keyCode;
        }
        else if (e.which) // Netscape/Firefox/Opera
        {
            oKeyCode = e.which;
        }
        if (oKeyCode == 13) {
            oButton.click();
            return false;
        }
    }
}

















//var map;
//var geocoder;

//function GoogleMapsLoad() {
//    if (GBrowserIsCompatible()) {
//        geocoder = new GClientGeocoder();
//        map = new GMap2(document.getElementById('map_canvas'));
//        map.enableScrollWheelZoom();
//        map.setCenter(new GLatLng(40, -95), 3, G_NORMAL_MAP);
//        format_gmap(map, '', '', 1, '', '', '')
//    }
//}

//function searchDealers() {
//    var address = document.getElementById('Address').value;
//    geocoder.getLatLng(address, function(latlng) {
//        if (!latlng) {
//            alert(address + ' not found');
//        } else {
//            searchLocationsNear(latlng);
//        }
//    });
//}

//function searchLocationsNear(center) {
//    var radius = document.getElementById('MaxDistance').value;
//    var searchUrl = 'MapSupport.aspx?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&MaxDistance=' + radius;
//    GDownloadUrl(searchUrl, function(data) {
//        var xml = GXml.parse(data);
//        var markers = xml.documentElement.getElementsByTagName('Marker');
//        map.clearOverlays();

//        var sidebar = document.getElementById('map_sidebar');
//        sidebar.innerHTML = '';
//        if (markers.length == 0) {
//            sidebar.innerHTML = 'No results found.';
//            map.setCenter(new GLatLng(40, -95), 3, G_NORMAL_MAP);
//            return;
//        }

//        var bounds = new GLatLngBounds();
//        var baseIcon = new GIcon();
//        baseIcon.iconSize = new GSize(39, 34);
//        baseIcon.shadowSize = new GSize(39, 34);
//        baseIcon.iconAnchor = new GPoint(9, 34);
//        baseIcon.image = 'http://maps.google.com/mapfiles/arrow.png';
//        baseIcon.shadow = 'http://maps.google.com/mapfiles/arrowshadow.png';
//        var point = new GLatLng(parseFloat(center.lat()), parseFloat(center.lng()));
//        var newmarker = new GMarker(point, baseIcon);
//        map.addOverlay(newmarker);
//        bounds.extend(point);
//        for (var i = 0; i < markers.length; i++) {
//            var Organization = markers[i].getAttribute('Organization');
//            var OrganizationURL = markers[i].getAttribute('OrganizationURL');
//            var OrganizationLocationID = markers[i].getAttribute('OrganizationLocationID');
//            var OrganizationLocation = markers[i].getAttribute('OrganizationLocation');
//            var Address1 = markers[i].getAttribute('Address1');
//            var Address2 = markers[i].getAttribute('Address2');
//            var Phone = markers[i].getAttribute('Phone');
//            var Fax = markers[i].getAttribute('Fax');
//            var Latitude = parseFloat(markers[i].getAttribute('Latitude'));
//            var Longitude = parseFloat(markers[i].getAttribute('Longitude'));
//            var Distance = parseFloat(markers[i].getAttribute('Distance'));
//            var DefaultContact = markers[i].getAttribute('DefaultContact');
//            var DefaultContactEmail = markers[i].getAttribute('DefaultContactEmail');
//            var point = new GLatLng(parseFloat(markers[i].getAttribute('Latitude')), parseFloat(markers[i].getAttribute('Longitude')));
//            var marker = createMarker(point, Organization, OrganizationLocation, Address1, Address2);
//            map.addOverlay(marker);
//            if (i > 0) {
//                var SeperatorLine = true;
//            }
//            else {
//                var SepeartorLine = false;
//            }
//            var sidebarEntry = createSidebarEntry(marker, Organization, OrganizationLocationID, OrganizationLocation, Address1, Address2, Phone, Fax, Distance, DefaultContact, DefaultContactEmail, SeperatorLine);
//            sidebar.appendChild(sidebarEntry);
//            bounds.extend(point);
//        }
//        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) - 1);
//    });
//}

//function createMarker(point, Organization, OrganizationLocation, Address1, Address2) {
//    var marker = new GMarker(point);
//    var html = '<b>' + Organization + '</b><br/>' + OrganizationLocation + '<br/>' + Address1 + '<br/>' + Address2;
//    GEvent.addListener(marker, 'click', function() {
//        marker.openInfoWindowHtml(html);
//    });
//    return marker;
//}

//function createSidebarEntry(marker, Organization, OrganizationLocationID, OrganizationLocation, Address1, Address2, Phone, Fax, Distance, DefaultContact, DefaultContactEmail, SeperatorLine) {
//    var div = document.createElement('div');
//    if (SeperatorLine == true) {
//        var SeperatorStyle = ' border-top: dashed 1px #646464;'
//    }
//    else {
//        var SeperatorStyle = '';
//    }
//    var html = '<table border=0 border=0 cellpadding=3px cellspacing=0 width=100% style=""font-size: 75%;"">' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;' + SeperatorStyle + '"">' +
//        'Organization:' +
//        '</td>' +
//        '<td style=""text-align: left;' + SeperatorStyle + '"">' +
//        '<b>' + Organization + '</b>' +
//        '</td>' +
//        '</tr>' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;"">' +
//        'Location:' +
//        '</td>' +
//        '<td style=""text-align: left;"">' +
//        OrganizationLocation +
//        '</td>' +
//        '</tr>' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;"">' +
//        'Distance:' +
//        '</td>' +
//        '<td style=""text-align: left;"">' +
//        Distance.toFixed(1) + ' miles' +
//        '</td>' +
//        '</tr>' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;"">' +
//        'Address:' +
//        '</td>' +
//        '<td style=""text-align: left;"">' +
//        Address1 + '<br/>' + Address2 +
//        '</td>' +
//        '</tr>' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;"">' +
//        'Phone:' +
//        '</td>' +
//        '<td style=""text-align: left;"">' +
//        Phone +
//        '</td>' +
//        '</tr>' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;"">' +
//        'Fax:' +
//        '</td>' +
//        '<td style=""text-align: left;"">' +
//        Fax +
//        '</td>' +
//        '</tr>' +
//        '<tr>' +
//        '<td style=""width: 20%; text-align: right; font-size: 75%; vertical-align: top;"">' +
//        '</td>' +
//        '<td style=""text-align: left;"">' +
//        '<a href=""#"" onclick=""Form_ContactOrganizationLocation_Open(' + OrganizationLocationID + ');"">Contact</a>' +
//        '<input type=""hidden"" name=""' + OrganizationLocationID + '_Organization"" id=""' + OrganizationLocationID + '_Organization"" value=""' + Organization + '""/>' +
//        '<input type=""hidden"" name=""' + OrganizationLocationID + '_OrganizationLocation"" id=""' + OrganizationLocationID + '_OrganizationLocation"" value=""' + OrganizationLocation + '""/>' +
//        '<input type=""hidden"" name=""' + OrganizationLocationID + '_DefaultContact"" id=""' + OrganizationLocationID + '_DefaultContact"" value=""' + DefaultContact + '""/>' +
//        '<input type=""hidden"" name=""' + OrganizationLocationID + '_DefaultContactEmail"" id=""' + OrganizationLocationID + '_DefaultContactEmail"" value=""' + DefaultContactEmail + '""/>' +
//        '</td>' +
//        '</tr>' +
//        '</table>';
//    div.innerHTML = html;
//    div.style.cursor = 'pointer';
//    div.style.marginBottom = '5px';
//    GEvent.addDomListener(div, 'click', function() {
//        GEvent.trigger(marker, 'click');
//    });
//    GEvent.addDomListener(div, 'mouseover', function() {
//        //div.style.backgroundColor = '#eee';
//    });
//    GEvent.addDomListener(div, 'mouseout', function() {
//        //div.style.backgroundColor = '#fff';
//    });
//    return div;
//}

//function format_gmap(map, control, maptype, show_marker, addoverview, addscale, addgoogle) {
//    if (control != '') {
//        var controls = control.split('|');
//        for (i = 0; i < controls.length; ++i) { eval('map.addControl(new ' + controls[i] + '());'); }
//    }
//    if (maptype != '') { eval('map.setMapType(' + maptype + ');'); }
//    if (control == '' && maptype == '') { map.setUIToDefault(); }
//    if (addoverview == '1') map.addControl(new GOverviewMapControl());
//    if (addscale == '1') map.addControl(new GScaleControl());
//    if (addgoogle == '0') map.enableGoogleBar();
//}

//GoogleMapsLoad();