﻿// Global var --------------------------------------------------------------------------------------

var mouseX = 0;
var mouseY = 0;

// jQuery center div -------------------------------------------------------------------------------------------

jQuery.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

// aClose -------------------------------------------------------------------------------------------

function aClose(div) { $('#' + div).css('visibility', 'hidden'); }

function aCloseAndEmpty(div, divEmpty) {

    $('#' + div).css('visibility', 'hidden');
    $('#' + divEmpty).html(''); 

}

// aLoad -------------------------------------------------------------------------------------------

// AJAX load function
// div: anger vilken div som skall ta emot HTML response
// icon: id namn på AJAX icon som skall visas
// url: AJAX request destination. Ex ajax.apsx
// qString: QueryString till url
// HTML response sker endast från div id=aReturn
// Exempel: onclick="javascript:aLoad('Return1','icon1','aLoad_ajax.aspx','&id1=1&id2=2'); return false;"

function aLoad(div, icon, url, qString) {
    var random = parseInt(Math.random()*999999999999999);
    //$('#icon').hide(); //$('#icon').show(); //$('#'+icon).css('visibility', 'visible'); //$('#'+icon).css('visibility', 'hidden');$('#' + icon).attr("src", "../_bilder/animering/icon.gif");    $('#'+icon).attr( "src", "../_bilder/animering/empty.png"); 
    
    $('#' + icon).css('visibility', 'visible');
    $('#' + div).load(url + '?random=' + random + '&div=' + div + qString + ' #aReturn', function() {
        $('#' + icon).css('visibility', 'hidden');
    });
}

function aLoadSlide(div, icon, url, qString) {
    var random = parseInt(Math.random() * 999999999999999);
    //$('#icon').hide(); //$('#icon').show(); //$('#'+icon).css('visibility', 'visible'); //$('#'+icon).css('visibility', 'hidden');$('#' + icon).attr("src", "../_bilder/animering/icon.gif");    $('#'+icon).attr( "src", "../_bilder/animering/empty.png"); 

    $('#' + icon).css('visibility', 'visible');
    //$('#' + div).load(url + '?random=' + random + '&div=' + div + qString + ' #aReturn', function() {
    //    $('#' + icon).css('visibility', 'hidden');
    //});

    $('#' + div).slideDown("slow").load(url + '?random=' + random + '&div=' + div + qString + ' #aReturn', function() {
        $('#' + icon).css('visibility', 'hidden');
    });


}
// aLoad x2 -------------------------------------------------------------------------------------------

// Samma som ovan fast 2 AJAX request
// Exempel: onclick="javascript:aLoadx2('Return1','icon1','aLoad_ajax.aspx','&id1=1&id2=2','Return2','icon2','aLoad2_ajax.aspx','&id1=3&id2=4'); return false;"

var aLoadx2 = function(div, icon, url, qString, div2, icon2, url2, qString2) { 
    var random = parseInt(Math.random()*999999999999999);
    

    // HTMl output 1
    $('#' + div).load(url + '?random=' + random + '&div=' + div + qString + ' #aReturn', function() {
        $('#' + icon).css('visibility', 'hidden');
    });
    // HTMl output 2
    $('#' + div2).load(url2 + '?random=' + random + '&div=' + div2 + qString2 + ' #aReturn', function() {
        $('#' + icon2).css('visibility', 'hidden');
    });

    // Visar AJAX icon
    $('#' + icon).css('visibility', 'visible');
    $('#' + icon2).css('visibility', 'visible');
}

// aClickMouse -------------------------------------------------------------------------------------------

function aMousePosistion() {

    $(document).click(function(e) {
        //$('#status').html(e.pageX + ', ' + e.pageY);
        //alert(e.pageX + ', ' + e.pageY);
        mouseX = e.pageX;
        mouseY = e.pageY;

        //$('#editAdminHolder').css('top', adjustY);
        //$('#editAdminHolder').css('left', adjustX);

        //$('#editAdminHolder').css('visibility', 'visible');
    });
}


// aLoadBox -------------------------------------------------------------------------------------------

// AJAX load function
// div: anger vilken div som skall ta emot HTML response
// icon: id namn på AJAX icon som skall visas
// url: AJAX request destination. Ex ajax.apsx
// qString: QueryString till url
// HTML response sker endast från div id=aReturn
// Exempel: onclick="javascript:aLoadBox('Return1','icon1','aLoad_ajax.aspx','&id1=1&id2=2','box1','200','Rubrik'); return false;"

function aLoadBox(div, icon, url, qString, boxID, boxWidth, boxName) {

    aLoad(div, icon, url, qString);

    var adjustX = mouseX - boxWidth/2;
    var adjustY = mouseY;

    $('#' + boxID).dialog('option', 'width', +boxWidth);
    $('#' + boxID).dialog('option', 'title', boxName);
    //$('#' + boxID).dialog('option', 'transfer', 'slow');
    $("#" + boxID).dialog('option', 'position', [adjustX, adjustY]);

    //$('#' + boxID).effect("transfer", { to: $('#' + boxID) }, 1000); 
    
    //var options = { to: "#pnl_vktop" };
    //$("#" + boxID).show('transfer', options, '500');

    //$('#' + boxID).dialog('option', 'show', 'transfer', options, 500);
    
    //show(effect, [options], [speed], [callback])
    //$('#' + boxID).dialog('option', 'show', 'slide');
    //$('#' + boxID).dialog({ position: 'top' });
    //$('#' + boxID).dialog('option', 'position', 'top');
    //$('#' + boxID).effect('transfer', { to: '#pMenu' }, 'slow');
    //$('#' + boxID).show("clip", { direction: "horizontal" }, 500);
    //$(".box").effect("transfer",{to:"h1.logo a"},"slow")"
    //$('#' + boxID).effect('transfer', { to: '#pMenu' }, 'slow'); 
    //$('#' + boxID).dialog({ show: 'slide' });
    $('#' + boxID).dialog('open');
}
// aPost -------------------------------------------------------------------------------------------

// AJAX post formulär funktion
// div: anger vilken div som skall ta emot HTML response
// icon: id namn på AJAX icon som skall visas
// url: AJAX request destination. Ex ajax.apsx
// type: POST eller GET
// data: Formulär fältnamn, laddas från aPostName()
// loadFunc: startar en funktion efter avslutad aPost

function aPost(div, icon, url, type, data, loadFunc) {
    
	//disabled all the text fields
	//$('.text').attr('disabled','true');
	
	//show the loading sign
	$('#' + icon).css('visibility', 'visible');
	
	//start the ajax
	$.ajax({
	
		//this is the php file that processes the data and send mail
	    url: url,	
		
		//GET method is used
	    type: type,

		//pass the data
	    data: data,		
		
		//Do not cache the page
		cache: false,
		
		//success
		success: function (html) {	
		    
		    // HTMl output
		    $('#' + div).html(html);
		    $('#' + icon).css('visibility', 'hidden');

		    // Startar funktion
		    eval(loadFunc);
		}	
	});
}

function aPostJson(icon, url, type, data) {

    var random = parseInt(Math.random() * 999999999999999);
    data = data + "&random=" + random

    //disabled all the text fields
    //$('.text').attr('disabled','true');

    //show the loading sign
    $('#' + icon).css('visibility', 'visible');

    //start the ajax
    $.ajax({

        //this is the php file that processes the data and send mail
        url: url,

        //GET method is used
        type: type,

        //pass the data
        data: data,

        //data type
        dataType: 'json',

        //Do not cache the page
        cache: false,

        //success
        success: function(json) {

            // HTMl output
            //$('#' + div).html(html);
            if (json.success) {
                //self.location="aLoad.aspx";
                //$('#Return3').text(json.data.text2);
                //alert("Success: " + json.success);

                //var strFun = json.loadFunction;
                //var strParam = json.data.text1;

                //Create the function call from function name and parameter.
                //var funcCall = strFun + "('" + strParam + "');";

                //Call the function
                eval(json.loadFunction);

            } else {
                //alert("Error: " + json.error);
                //$('#Return3').html(json.success.text1);
                //Call the function
                eval(json.loadFunction);
            }


            $('#' + icon).css('visibility', 'hidden');
        }
    });
}

function callBack_error(callBackPanel, callBackLabel, callBackText) {

    //Visa callBackPanel
    $('#' + callBackPanel).css('visibility', 'visible');
    $('#' + callBackLabel).text(callBackText);

}

function aPostRedirect(div, icon, url, type, data, redirectURL) {

    //disabled all the text fields
    //$('.text').attr('disabled','true');

    //show the loading sign
    $('#' + icon).css('visibility', 'visible');

    //start the ajax
    $.ajax({

        //this is the php file that processes the data and send mail
        url: url,

        //GET method is used
        type: type,

        //pass the data
        data: data,

        //Do not cache the page
        cache: false,

        //success
        success: function(html) {

            // HTMl output
            //$('#' + div).html(html);
            //$('#' + icon).css('visibility', 'hidden');
            self.location = redirectURL;
        }
    });
}

// aPost anrop -------------------------------------------------------------------------------------------

// AJAX post formulär funktion
// div: anger vilken div som skall ta emot HTML response
// icon: id namn på AJAX icon som skall visas
// url: AJAX request destination. Ex ajax.apsx
// type: POST eller GET
// text1: Formulär fält, ex. id="text1"
function aPostName(div, icon, url, type) {

    //Get the data from all the fields
    //var text1 = encodeURIComponent($('input[id=Text1]').val());
    var text1 = encodeURIComponent($('#Text1').val());
    var text2 = encodeURIComponent($('input[id=Text2]').val());
    var text3 = encodeURIComponent($('input[id=Text3]').val());
    //var select1 = $('select[id=Select1]').val();
    var select1 = $('#Select1').val();
    var select1_text = $('#Select1 :selected').text();

    var checkbox1 = ($('#Checkbox1').is(':checked')) ? $('#Checkbox1').val():0;
    
    var radio1 = 0;
    if ($('#Radio1').is(':checked')) { radio1 = $('#Radio1').val(); }
    if ($('#Radio2').is(':checked')) { radio1 = $('#Radio2').val(); }
    if ($('#Radio3').is(':checked')) { radio1 = $('#Radio3').val(); }
    
    //organize the data properly
    var data = 'text1=' + text1 + '&text2=' + text2 + '&select1=' + select1 + '&checkbox1=' + checkbox1 + '&radio1=' + radio1 + '&text3=' + text3;

    //var data = 'text1=' + text1.val() + '&text2=' + text2.val() + '&text3=' + encodeURIComponent(text3.val());		

    //alert(data);

    // Anropar AJAX funktion, aPost
    aPost(div, icon, url, type, data);
}


function alertReturn4() {
		var text1 = encodeURIComponent($('textarea[id=Return4]').val());
		alert(text1);
}

// Replace to punkt ------------------------------------------------------------
function replaceKomma(formValue) {
    return formValue.replace(/,/, ".");
}