function openWindow(URL,width,height) {day = new Date();id = day.getTime();eval("page" + id + " = window.open(" + URL + ", '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left="+((screen.width - width)/2)+",top="+((screen.height - height)/2)+"');");return false;}
function isEmail(address) {var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;if(reg.test(address) == false) {return false;} else {return true;}}

$(document).ready(function() {
    browserDetection();
    fixIE6alphaTransparency();
    attachNewsticker();
    attachTooltip('.required');
    $('#category').change(attachProcessCategory);
    $('#ajax-begin').hide();
    $('#form-leads').submit(validateLeads);
    $('#form-leads-submit').click(function () {
        if (validateLeads() == true) {
            $('#form-leads').submit();
        }
        return false;
    });
    $('dl.faqs').children('dd').hide();
    $('dl.faqs').children('dt').click(function () {
        $('dl.faqs').children('dd').hide();
        $(this).next('dd').slideDown();
    });
    $('#form-contact-submit').click(function () {
        if (validateContact() == true) {
            $('#form-contact').submit();
        }
        return false;
    });
});

function attachProcessCategory() {
    $loader = '<img alt="Please wait..." src="/resources/images/loader.gif" style="display:block;margin:0 auto;" />';
    $('#ajax-q').empty();
    $('#ajax-subs').empty();
    $('#ajax-subs').html($loader);
    $('#ajax-subs').show();
    $('#ajax-begin').hide();
    $.getJSON("/process.php", {action: "subs", cat: $(this).attr("value")}, function(data){
        $('#ajax-subs').empty();
        if (data.length > 0) {
            $('#ajax-subs').append('<label for="service" class="line">Select a Service:<em>*</em></label>');
            $('#ajax-subs').append('<select name="service" id="service" class="required" onchange="attachProcessSubcategory()" onmouseover="attachTooltip(\'#service\')" style="width:400px;"></select>');
            $('#ajax-subs').children('select').append('<option value=""> Select a Service </option>');
            $.each(data, function(i, item){
                $('#ajax-subs').children('select').append('<option value="' + item['id'] + '">' + item['name'] + '</option>');
            });
            $('#ajax-subs').append('<span class="tooltip">Please select Service</span>');
        }
    });
}
function attachProcessSubcategory() {
    $loader = '<img alt="Please wait..." src="/resources/images/loader.gif" style="display:block;margin:0 auto;" />';
    $('#ajax-q').empty();
    $('#ajax-q').html($loader);
    $('#ajax-begin').hide();
    $.getJSON("/process.php", {action: "q", sub: $('#service').attr("value")}, function(data){
        $('#ajax-q').empty();
        $.each(data, function(i, item){
            $('#ajax-q').append('<p id="p-' + item['id'] + '"></p>');
            $('#p-' + item['id']).append('<label for="' + item['name'] + '" id="label-' + item['id'] + '" class="line">' + item['question'] + '</label>');
            if (item['required'] == 1) {
                $('#label-' + item['id']).append('<em>*</em>');
            }
            $event = '';
            $class = '';
            if (item['type'] == '2') {
                if (item['required'] == 1) {
                    $event = ' onmouseover="attachTooltip(\'#' + item['name'] + '\')"';
                    $class = ' required';
                }
                $('#p-' + item['id']).append('<input type="text" name="extra[\'' + item['name'] + '\']" id="' + item['name'] + '" class="text' + $class + '" size="55"' + $event + ' />');
                if (item['required'] == 1) {
                    $('#p-' + item['id']).append('<span class="tooltip">Please enter ' + item['question'] + '</span>');
                }
            }
            if (item['type'] == '1') {
                if (item['required'] == 1) {
                    $class = ' class="required"';
                }
                $.each(item['values'], function(j, val){
                    $('#p-' + item['id']).append('<input type="radio" name="extra[\'' + item['name'] + '\']" value="' + val + '"' + $class + ' /> ');
                    $('#p-' + item['id']).append(val + ' ');
                });
                if (item['required'] == 1) {
                    $('#p-' + item['id']).append('<span class="tooltip">Please choose ' + item['question'] + '</span>');
                }
            }
            if (item['type'] == '0') {
                if (item['required'] == 1) {
                    $event = ' onmouseover="attachTooltip(\'#' + item['name'] + '\')"';
                    $class = ' class="required"';
                }
                $('#p-' + item['id']).append('<select name="extra[\'' + item['name'] + '\']" id="' + item['name'] + '"' + $class + $event + ' style="width:400px;"><option value=""> -- </option></select>');
                $.each(item['values'], function(j, val){
                    $('#' + item['name']).append('<option value="' + val + '">' + val + '</option>');
                });
                if (item['required'] == 1) {
                    $('#p-' + item['id']).append('<span class="tooltip">Please select ' + item['question'] + '</span>');
                }
            }
            $('#p-' + item['id']).append('<span style="clear:left;display:block;"></span>');
        });
    });
    $('#ajax-begin').show();
}

function attachNewsticker() {
    if ($('#ticker').length > 0) {
        $('#ticker').newsticker();
    }
}

function attachTooltip(selector) {
    $(selector).tooltip({
        position: ['center', 'right'],
        offset: [-2, 10],
        effect: 'fade',
        opacity: 1,
        api: true,
        onBeforeShow: function() {
            if (this.getTrigger().attr('value').length > 0) {
                return false;
            } else {
                return true;
            }
        }
    });
}
function attachNewTooltip(selector) {
    var api = $(selector).tooltip({
        position: ['center', 'right'],
        offset: [-2, 10],
        effect: 'fade',
        opacity: 1,
        api: true
    });
    api.show();
}

function validateLeads() {
    if ($.trim($('#category').attr('value')) == '') {
        $('#category').focus();
        attachNewTooltip('#category');
        return false;
    }
    if ($.trim($('#service').attr('value')) == '') {
        $('#service').focus();
        attachNewTooltip('#service');
        return false;
    }
    if ($.trim($('#proj_begin').attr('value')) == '') {
        $('#proj_begin').focus();
        attachNewTooltip('#proj_begin');
        return false;
    }
    if ($.trim($('#fname').attr('value')) == '') {
        $('#fname').focus();
        attachNewTooltip('#fname');
        return false;
    }
    if ($.trim($('#lname').attr('value')) == '') {
        $('#lname').focus();
        attachNewTooltip('#lname');
        return false;
    }
    if ($.trim($('#address1').attr('value')) == '') {
        $('#address1').focus();
        attachNewTooltip('#address1');
        return false;
    }
    if ($.trim($('#city').attr('value')) == '') {
        $('#city').focus();
        attachNewTooltip('#city');
        return false;
    }
    if ($.trim($('#state').attr('value')) == '') {
        $('#state').focus();
        attachNewTooltip('#state');
        return false;
    }
    if ($.trim($('#zip').attr('value')) == '') {
        $('#zip').focus();
        attachNewTooltip('#zip');
        return false;
    }
    if (!isEmail($('#email').attr('value'))) {
        $('#email').focus();
        attachNewTooltip('#email');
        return false;
    }
    if ($.trim($('#day_phone').attr('value')) == '') {
        $('#day_phone').focus();
        attachNewTooltip('#day_phone');
        return false;
    }
    if ($.trim($('#btc').attr('value')) == '') {
        $('#btc').focus();
        attachNewTooltip('#btc');
        return false;
    }
    return true;
}

function validateContact() {
    if ($.trim($('#fname').attr('value')) == '') {
        $('#fname').focus();
        attachNewTooltip('#fname');
        return false;
    }
    if ($.trim($('#lname').attr('value')) == '') {
        $('#lname').focus();
        attachNewTooltip('#lname');
        return false;
    }
    if (!isEmail($('#email').attr('value'))) {
        $('#email').focus();
        attachNewTooltip('#email');
        return false;
    }
    if ($.trim($('#phone').attr('value')) == '') {
        $('#phone').focus();
        attachNewTooltip('#phone');
        return false;
    }
    return true;
}
