// JavaScript Document
function isEmail(strEmail){
	var patrn = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	return patrn.test(strEmail);
}
function isValidDigit(s) 
{ 
	var patrn=/^[0-9]{1,20}$/; 
	if (!patrn.exec(s)) return false;
	return true;
} 
function isValidInput(s)
{
	var patrn=/^([a-zA-Z0-9]|[. ,-]){1,}$/; 
	if (!patrn.exec(s)) return false;
	return true;
}

function sort_locations() {

    var arr = new Array($('.ullocations li').length);

    $('.ullocations li').each(function(i) {
        arr[i] = new loc(parseFloat($(this).attr('title')), $(this).html(), $(this).attr('id'), $(this).find('.cl').html());
    });
    //   arr.sort(loc_sort);
    //arr.sort(loc_sort);
    arr.sort(loc_sort);
    var h10 = false;
    var h20 = false;
    var h99 = false;


    var html = '';
    for (var i = 0; i < arr.length; i++) {
        //alert(arr[i].dis);
        if (!h10 && arr[i].dis <= 10) {
            html += '<li style="margin:0; padding:10px 0 5px 0;"><h1 style="font-size:18px; line-height: 22px; margin:0px; padding:0px">Stores & Authorized Dealers within 0 - 10 miles</h1><hr /><\/li>';
            h10 = true;
        }


        if (!h20 && arr[i].dis > 10 && arr[i].dis <= 20) {
            html += '<li  style="margin:0; padding:10px 0 5px 0;"><h1 style="font-size:18px; line-height: 22px; margin:0px; padding:0px">Stores & Authorized Dealers within 10 - 20 miles</h1><hr /><\/li>';
            h20 = true;
        }


        if (!h99 && arr[i].dis > 20) {
            html += '<li  style="margin:0; padding:10px 0 5px 0;"><h1 style="font-size:18px; line-height: 22px; margin:0px; padding:0px">Stores & Authorized Dealers within 20 - ' + $('.fap_miles').html() + '  miles</h1><hr /><\/li>';
            h99 = true;
        }

        html += '<li id="' + arr[i].id + '">' + arr[i].html + '<\/li>';
    }
    $('.ullocations').html(html);
	
    //lbtn_appointment
    //$('.lbtn_appointment input').click(function() { lbtn_appointment_click(this) });

    //galink
    //$('.galink').click(function () { galink_click(this); });
}

function loc(dis, html, id, cl) {
    this.dis = dis;
    this.html = html;
    this.id = id;
    this.cl = cl;
}

function showmap(obj, url) {

    var parent = $(obj).parent();

    $('.ullocations li').not(parent).css('background', 'none');
    //parent.css('background', '#f7f7f7');


    var id = parent.attr('id');
    id = id.substr(id.indexOf('_') + 1);

    $('.ullocations li .googlemapcontent').hide();

    if (!parent.find('.googlemapcontent').attr('class')) {
        parent.append('<div class="googlemapcontent"><iframe width="508" height="420" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + url + '"></iframe></div>');
    }
    parent.find('.googlemapcontent').show();
    var loc = location.href;
    if (loc.indexOf('#') != -1) {
        loc = loc.substr(0, loc.indexOf('#'));
    }
    location.href = loc + '#l' + id;
}

function loc_sort(a, b) {
    //alert((a.dis > b.dis) + ' ' + a.dis + '>' + b.dis);
    // if ((a.dis <= 10 && b.dis <= 10) || (a.dis > 10 && b.dis > 10 && a.dis <= 20 && b.dis <= 20) || (a.dis > 20 && b.dis > 20)) {
    if ((a.dis <= 10 && b.dis <= 10) || (a.dis > 10 && b.dis > 10 && a.dis <= 20 && b.dis <= 20)) {

        if (a.id == 'location_37' || b.id == 'location_37') {
            //   alert(a.dis+'-'+a.cl+'-'+ (GetK(a.cl) + a.dis/10000.0) +'-'+b.dis+'-'+b.cl+'-'+(GetK(b.cl)+ b.dis/10000.0));
        }
        return ((GetK(a.cl) + a.dis / 10000.0) - (GetK(b.cl) + b.dis / 10000.0));
    }
    return (a.dis - b.dis);
}

function GetK(cl) {
    var k = 1;
    switch (cl) {
        case 'P':
            k = 1; break;
        case 'S':
            k = 2; break;
        case 'NP':
            k = 4; break;
        default:
            k = 8; break;
    }
    return k;
}






function createInfo(title, content) {
	return '<div id="popup"><h1 class="popup-title">' + title + '</h1><div id="popup-body"><p>' + content + '</p></div></div>';
}
