﻿function showModal(id) {    
    $('#'+id).dialog({
        modal: true,
        buttons: { "OK": function() { $(this).dialog("close"); } },
        close: function(event, ui) { $(this).dialog('destroy'); },
        width: 600
    });
}

function toggleVisibility(id) {        
    if($(id).is(':visible')){        
        $(id).hide('fast');
    }   
    else{        
        $(id).show('slow');
    }
}

function launchPopUp(theURL) {
    var win, launcher;

    // Makes sure the viewer launches at a good size for the users screen.

    var availWidth, availHeight;

    if (screen.availWidth) {

        availWidth = screen.availWidth;

        availHeight = screen.availHeight;

    } else {

        availWidth = screen.width;

        availHeight = screen.height - 30;

    }
	
	var w = 880, h = 640;
	
    var scrollbars = 0;

    if (w > availWidth || h > availHeight) {

        scrollbars = 1;

        if (w > availWidth) w = availWidth;

        if (h > availHeight) h = availHeight;

    }

    var wint = Math.floor((availHeight - h) / 2);

    var winl = Math.floor((availWidth - w) / 2);
	
	var winProps = "height=" + h + ",width=" + w + ",top=" + wint + ",left=" + winl + ",scrollbars=" + scrollbars;
	
	if ( theURL.indexOf("mobile") > -1 ) {
        // var w = 880, h = 640; don't set the width and height
	    winProps = "height=" + 768 + ",width=" + 1024 + ",top=" + 0 + ",left=" + winl + ",scrollbars=" + scrollbars;
    }
    
	
	if (theURL.indexOf)

    //Direct media links    
    if (theURL.indexOf(".wmv") > -1 || theURL.indexOf(".wma") > -1 || theURL.indexOf(".mp3") > -1 || theURL.indexOf(".asf") > -1) {        
        return (true);
    }


    //Attemps to open the new window (handles pop-up blockers)

    if (typeof (win = window.open(theURL, 'win', winProps)) != "object") {

        // pop-up blocker stopped new window. just go to the URL anyway.

        return (true);

    }

    else if (typeof (win) == "object") {

        if (win == null) {

            var msg = "Software on your computer is preventing us from launching a pop-up window.\r\n" +

                                                            "Please disable any pop-up blocking software and try again by first hitting the\r\n" +

                                                            "cancel button.\r\n\r\n" +

                                                            "If you continue to experience problems, hit the okay button and we will attempt to\r\n" +

                                                            "load the new page in this window.";

            if (confirm(msg)) self.location = theURL;

        } else {

            win.location.replace('about:blank');

            win.focus();

            win.location = theURL;

            // the window already exists. replace it with the new one.
        }
    }

    // window launched okay. stop mouseclick event for clicked link.

    return (false);
}

function togglePlusMinus(id) {
	var img = $(id).find("img");
	if ($(img).attr("src").lastIndexOf("plus.gif") >= 0)
		$(img).attr("src", "/content/images/minus.gif");
	else
		$(img).attr("src", "/content/images/plus.gif");
	return (true);
}

function getCountries(selectedValue, id) {
    var myId = '#' + id;
    var select = $(myId)[0];
    $.getJSON('/Home/Countries', function (data) {        
        $.each(data, function (key, value) {
            $(select).
                append($("<option></option>").
                attr("value", value.code).
                text(value.name));
        });
        $(select).val(selectedValue);
    });
}

function getStates(country, selectedValue, id) {
    var myId = '#' + id;
    var element = $(myId)[0];
    var input = null;
    $.getJSON('/Home/States', { country: country }, function (data) {
        var disabled = $(element).attr('disabled');
        if (data == null || data.length == 0) {
            input = $("<input type='text'></input>").
                                attr('id', id).
                                attr('name', id).
                                attr('class', 'required inreach_large_width')
        }
        else {
            input = $("<select></select>").
                                attr('id', id).
                                attr('class', 'required inreach_small_width').
                                attr('name', id);

            $.each(data, function (key, value) {
                $(input).
                    append($("<option></option>").
                    attr("value", value).
                    text(value));
            });
        }
        $(element).replaceWith(input);        
        $(input).val(selectedValue.toUpperCase());                
        $(input).attr('disabled', disabled);
    });
}
