﻿// Global var --------------------------------------------------------------------------------------

var mouseX = 0;
var mouseY = 0;

// 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, url, qString, callback) {	
    var random = parseInt(Math.random()*999999999999999);
    $('#' + div).show().load(url + '?random=' + random + '&div=' + div + qString , function() {
		if (callback){
			eval(callback);
		}
    });
}

function aLoadSlide(div, url, qString,callback) {
    var random = parseInt(Math.random() * 999999999999999);
    $('#' + div).slideDown(600).load(url + '?random=' + random + '&div=' + div + qString , function() {
		if (callback){
			eval(callback);
		}

    });


}
// 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);
    
    // Visar AJAX icon
    $('#' + icon).css('visibility', 'visible');
    $('#' + icon2).css('visibility', 'visible');
    // 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');
    });
}

// aClickMouse -------------------------------------------------------------------------------------------

function aClickMouse(e) {

    mouseX = e.clientX
    mouseY = e.clientY
    //alert(e.clientY + 1); 
}

// 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()

function aPost(div, icon, url, type, data) {
    
	//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'); 	
		}	
	});
 }

// 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 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);
}
