$(document).ready(function()
	{
	    /* expandable Personalisation div styles */
	    $(".CustomizeHomePage a").click(function() {			
			if($('div.personalisation').hasClass("collapsed")) {
				$('div.personalisation').find(".content").slideDown("200");
				$('div.personalisation').removeClass("collapsed");
				/*$(this).Class("off");*/
			}
			else {
				$('div.personalisation').find(".content").slideUp("200");
				$('div.personalisation').addClass("collapsed");
				/*$(this).addClass("off");*/
			}
			return false;
		});
		$("a.expand").click(function () {
			$(this).parents('div.personalisation').find(".content").height("200");
			$(this).parents('div.personalisation').find("h2 a").removeClass("off");
			$(this).parents('div.personalisation').removeClass("collapsed");
			return false;
			alert(1);
		});
		$("a.collapse").click(function () {
			$(this).parents('div.personalisation').find(".content").height("200");
			$(this).parents('div.personalisation').find("h2 a").addClass("off");
			$(this).parents('div.personalisation').addClass("collapsed");
			return false;			
		});
});


function onSnapDock(sender, eventArgs) {    
    var sChangesNotSaved = 'Personalisation changes could not be saved.';
    var ColumnFrom = '';
    var RowFrom = '';
    var ColumnTo = '';
    var RowTo = '';
    var WebPartID = '';

    var oTxtPersonalisationDragDrop = fnGetDomObjectWithingArea('PersonalisationUpdateDragDropValueControls', 'PersonalisationUpdateDragDropValues');
    if (!oTxtPersonalisationDragDrop) {
        alert(sChangesNotSaved);
        return;
    }
    if (null == eventArgs.get_elementId() || eventArgs.get_index() < 0) {
        //alert('Docking snap ' + sender.get_id() + ' from ' + sender.LastDockElement + ' | ' + sender.LastDockIndex);
        ColumnFrom = sender.LastDockElement;
        RowFrom = sender.LastDockIndex;
        oTxtPersonalisationDragDrop.value = ColumnFrom + '|' + RowFrom;
        return;
    }
    else {
        //alert('Docking snap ' + sender.get_id() + ' into ' + eventArgs.get_elementId() + ' | ' + eventArgs.get_index());
        ColumnTo = eventArgs.get_elementId();
        RowTo = eventArgs.get_index();
        oTxtPersonalisationDragDrop.value = oTxtPersonalisationDragDrop.value + '|' + ColumnTo + '|' + RowTo
        
        //Get Web Part ID
        WebPartID = sender.get_id();            
        var oTxtPersonalisationDragDropWebPartID = fnGetDomObjectWithingArea('PersonalisationUpdateDragDropValueControls', 'PersonalisationWebPartID');
        if (!oTxtPersonalisationDragDropWebPartID) {
            alert(sChangesNotSaved);
            return;
        }
        oTxtPersonalisationDragDropWebPartID.value = WebPartID;
    }

    var oButtonPersonalisationDragDrop = fnGetDomObjectWithingArea('PersonalisationUpdateDragDropValueControls', 'btnSubmitPersonalisationUpdateDragDropValues');
    if (!oButtonPersonalisationDragDrop) {
        alert(sChangesNotSaved);
        return;
    }

    if (ColumnTo != '' && String(RowTo) != '') {
        oButtonPersonalisationDragDrop.click();
        oTxtPersonalisationDragDrop.value = '';
    }
    else {
        return;
    }
}


function PersonalistionArrayValidation(CheckBoxID, WebPartID, MaxAllowedCB, TotalCB) {
    //check that maximum number of CBs (CheckBoxes) has not been exceeded

    if (MaxAllowedCB != TotalCB) {
        var j
        var k = 0;
        for (var i = 0; i < TotalCB; i++) {
            //alert(i);
            j = i;
            if (document.getElementById('checkbox' + j).checked) {
                k++
            }
        }
        if (k > MaxAllowedCB) {
            document.getElementById('dvWarningMessage').innerHTML = 'You have reached the maximum number of allowed Information Portlets (' + MaxAllowedCB + ').';
            document.getElementById('checkbox' + CheckBoxID).checked = false;
        }
        else {
            document.getElementById('dvWarningMessage').innerHTML = '';            
        }        
    }

}

function ApplyPersonalisationUpdate(TotalCB) {    
    var sChangesNotSaved = 'Personalisation changes could not be saved.';
    var j
    var k = 0;
    var sSelectedWebPartIDs = '';
    for (var i = 0; i < TotalCB; i++) {
        //alert(i);
        j = i;
        if (document.getElementById('checkbox' + j).checked) {
            if (k == 0) {
                sSelectedWebPartIDs += (document.getElementById('checkbox' + j).value);
            } 
            else {
                sSelectedWebPartIDs += '|' + (document.getElementById('checkbox' + j).value);
            }            
            k++
        }
    }
    //alert(sSelectedWebPartIDs);
    
    var oTxtPersonalisationUpdateWebPartsChosen = fnGetDomObjectWithingArea('divPersonalisationUpdateWebPartsChosen', 'PersonalisationUpdateWebPartsChosen');
    if (!oTxtPersonalisationUpdateWebPartsChosen) {
        alert(sChangesNotSaved);
        return;
    }
    oTxtPersonalisationUpdateWebPartsChosen.value = sSelectedWebPartIDs;

    var oTxtPersonalisationUpdateWebPartsCount = fnGetDomObjectWithingArea('divPersonalisationUpdateWebPartsChosen', 'PersonalisationUpdateWebPartsCount');
    if (!oTxtPersonalisationUpdateWebPartsCount) {
        alert(sChangesNotSaved);
        return;
    }
    oTxtPersonalisationUpdateWebPartsCount.value = String(k);

    var oButtonPersonalisationUpdateWebPartsChosen = fnGetDomObjectWithingArea('divPersonalisationUpdateWebPartsChosen', 'btnSubmitPersonalisationUpdateWebPartsChosen');
    if (!oButtonPersonalisationUpdateWebPartsChosen) {
        alert(sChangesNotSaved);
        return;
    }   
    else 
    {
        oButtonPersonalisationUpdateWebPartsChosen.click();
        oTxtPersonalisationUpdateWebPartsChosen.value = '';
        oTxtPersonalisationUpdateWebPartsCount.value = '';
        return;
    }
    
    
}
