/*
####################################
REORDERABLE MENU FUNTIONALITY AND
AJAX SAVE LOGIC
####################################
*/

$(document).ready(
	function () {
		$('ul.blinds-menu').Sortable(
			{
				accept : 		'sortableitem',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		0.8,
				tolerance:      'pointer',
				fx:				200,
				axis:			'vertically',
				opacity:		0.4,
				revert:			true
			}
		)
	}
);

$(document).ready(
	function () {
		$('ul.special-blinds-menu').Sortable(
			{
				accept : 		'sortableitem',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		0.8,
				tolerance:      'pointer',
				fx:				200,
				axis:			'vertically',
				opacity:		0.4,
				revert:			true
			}
		)
	}
);

function toggleIt(element)
{
        $('#' + element).toggle();
}

//serialize string or reordered items
function serialize(s)
{
	serial = $.SortSerialize(s);
	return serial.hash;
}

//post new data as string to ajaxcallback method
function postIt(s)
{
    //alert("called");
    var menuData = serialize(s);
    var ts = new Date();
    //$("a#lnkSave").click(
        saveOrder(menuData, ts)
            //);

    //$('ul#menu').SortableDestroy(); 
}

//using ajax techniques to save
function saveOrder(menuData, ts)
{
   $.ajax(
    {
         url: 'AjaxCallBacks/saveMenuOrder.aspx?ts=' + ts.getMilliseconds(),
         type: 'POST',
         data: menuData,
         timeout: 1000,
        success: function(msg){alert( msg );},
        error: function(msg){alert( msg );}
    })
}

function sendMail(mailType, dataString, target)
{
alert(dataString);
    var ts = new Date();
    $.ajax(
        {
            url: 'AjaxCallBacks/sendMail.aspx?type=' + mailType + '&ts=' + ts.getMilliseconds(),
            type: 'POST',
            data: dataString,
            timeout: 1000,
            success: function(msg){$(target).html(msg);},
            error: function(msg){$(target).html(msg);}
        });
}

/*
#######################################
ITS OVER
#######################################
*/

/*
OTHER
*/
var lastID = null;
var el;
function addFileUploadField()
{
    if (!document.getElementById || !document.createElement)
        {
        alert('It seems that you have an old browser type. You cannot add extra upload fields without upgrading your browser.');
        return false;
        }		
    var div = document.getElementById ("moreUps");
	
    if (!div)
    {
        alert('Cannot find target element.');
        return false;
    }

    var newLine = document.createElement ("br");
    div.appendChild (newLine);
	
    var newUploadBox = document.createElement ("input");
	
    // Set up the new input for file uploads
    newUploadBox.type = "file";
    //newUploadBox.size = "60";
	
    // The new box needs a name and an ID
    if (!lastID)
    {
        lastID = 100;
    }
    else if(lastID > 101)
    {
        alert('You can attach a maximum of 3 files.');
        return false;
    }
	    
    newUploadBox.setAttribute ("id", "dynamic" + lastID);
    newUploadBox.setAttribute ("name", "dynamic:" + lastID);
    div.appendChild (newUploadBox);
    
    lastID++;
}
