function createNewSection(name) {
	var name = $F('sectionName');
	if (name != '') {
		var newDiv = Builder.node('div', {id: 'group', className: 'section', style: 'display:none;' }, [
			Builder.node('h3', {className: 'handle'}, name)
		]);

		$('page').appendChild(newDiv);
		Effect.Appear(newDiv.id);
	}
}


function createRequest(){
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
    } else {
        return http_request;
    }
}

function loadFriends(){
    Effect.Appear("loader",{duration:0.5,from:0.0,to:0.85});
    if(readCookie('PHPSESSID') == null) document.location = "./index.php";
    var xmlhttp = createRequest();
    xmlhttp.open('POST','twitter/load-friends.php',true);
    xmlhttp.onreadystatechange = function() {  
	if (xmlhttp.readyState == 4) {
	    if (xmlhttp.status == 200){
		 Effect.Fade('loader',{duration:0.20});
	    }
	    else { loadFriends(); }
	}
   }
    xmlhttp.send("");
}

function loadHelp(){
    Effect.Appear("help",{duration:0.7,from:0.0,to:1.0});
}

function closeHelp() {
	Effect.Fade('help',{duration:0.30});
}

function loadPrivacy(){
    Effect.Appear("privacy",{duration:0.7,from:0.0,to:1.0});
}

function closePrivacy() {
	Effect.Fade('privacy',{duration:0.30});
}


function loadTour(){
    Effect.Appear("tour",{duration:0.7,from:0.0,to:1.0});
}

function closeTour() {
	Effect.Fade('tour',{duration:0.30});
}



function updatePosition(currentX,currentY,element_id) { 
    if(readCookie('PHPSESSID') == null) document.location = "./login.php?se";
    var xmlhttp = createRequest();
    xmlhttp.open('POST', 'notes/update-pos.php', true);
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('id=' + element_id + '&x=' + currentX + '&y=' + currentY);
}

function deleteElement(element_id) { 
    if(readCookie('PHPSESSID') == null) document.location = "./login.php?se";
    var xmlhttp = createRequest();
    xmlhttp.open('POST', 'notes/delete.php', true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200){
        	    popArray(element_id);
            } else
                alert(document.location = "./login.php?se");
        }
    }
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('id=' + element_id);
}

function stickNote(){
    // if there is a file then we will submit normally
    if(document.forms['note_form'].file.value == ""){
    	    if(readCookie('PHPSESSID') == null) document.location = "./login.php?se";
	    var xmlhttp = createRequest();
	    xmlhttp.open('POST','notes/save.php',true);
	    xmlhttp.onreadystatechange = function() {  
			if (xmlhttp.readyState == 4) {
			    if (xmlhttp.status == 200){
					Effect.Fade('enter-it',{duration:0.20});
					Effect.Fade('save-it',{duration:0.30});
					document.forms['note_form'].reset();
					s('','','');  
					load(editing);
			    }
			    else { document.location = "./login.php?se" }
			}
	   }

	   var priority = document.forms['note_form'].p.value;
	   var filename = encodeURI(document.forms['note_form'].file.value);
	   var note_contents = encodeURI(document.forms['note_form'].note.value);
	   var x_pos = findPosX(document.getElementById(editing));
	   var y_pos = findPosY(document.getElementById(editing))-90;
	   var share = encodeURI(document.forms['note_form'].s.value);

	   xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	   var poststr = "";
	   poststr = "element_id=" + editing;
	   poststr += "&x=" + x_pos;
	   poststr += "&y=" + y_pos;
	   poststr += "&p=" + priority;
	   poststr += "&file=" + filename;
	   poststr += "&note=" + note_contents;
	   poststr += "&s=" + share;
	   xmlhttp.send(poststr);
	   n = "";
	   
	   // remove the welcome message
	   if(document.getElementById("welcome-message"))
	   	document.getElementById("welcome-message").style.display = "none";
	} else {
		// submit normally
		document.forms['note_form'].x.value = findPosX(document.getElementById(editing));
		document.forms['note_form'].y.value = findPosY(document.getElementById(editing))-90;
		document.forms['note_form'].action = "notes/save.php";
		document.forms['note_form'].submit();
		//document.forms['note_form'].reset();
		n = "";
	}
	
}

function cancel() {
	Effect.Fade('enter-it');
	document.forms['note_form'].reset();
	
	if(n == "Y") {
		Effect.Fade(editing);
		editing = "";
		n = "";
	}
}



function s(action,id,uid){
    var xmlhttp = createRequest();
    xmlhttp.open('POST','s.php',true);
    
    xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200){
                    refreshMessages(xmlhttp.responseText);
                    if(action == "A"){
                        document.location = "home.php"; // this will load the new note
                    }
                }
                else { document.location = "./login.php?se" }
            }
    }
   
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    var poststr = "";
    poststr = "a=" + action;
    poststr += "&id=" + id;
    poststr += "&uid=" + uid;
    
    xmlhttp.send(poststr);
}

function refreshMessages(responseText){
	document.getElementById("messages-number").innerHTML = "(" + responseText.substr(0,responseText.indexOf("|")) + ")";
	document.getElementById("messages").innerHTML = responseText.substr(responseText.indexOf("|")+1,responseText.length);
}

function load(element_id){
	var xmlhttp = createRequest();
	xmlhttp.open('POST','notes/load-note.php',true);
	xmlhttp.onreadystatechange = function() {  
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200){
				var response = xmlhttp.responseText;
				var response_array = response.split("<<>>");
				var inner_html = "<div style=\"padding-right:10px;\">" + response_array[1] + "</div>" ;
				inner_html += "";
				if(response_array[3] != ""){
					inner_html += "<div style=\"font-size:9px;position:absolute;top:110px;left:10px;\">"+response_array[3]+"<br>" + response_array[5] + " <a href=\"uploads/" + response_array[4] + "\" style=\"color:#222222;\" target=_blank>Download File</a></div>";
				}
			
				if(response_array[2] == "L"){
					if(window.navigator.appVersion.indexOf('MSIE 6') != -1)
						document.getElementById("inner-"+element_id).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/note-y.png');";
					else
						document.getElementById("inner-"+element_id).style.background="url('images/note-y.png') no-repeat";	
				} else if(response_array[2] == "B"){
					if(window.navigator.appVersion.indexOf('MSIE 6') != -1)
						document.getElementById("inner-"+element_id).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/note-b.png');";
					else
						document.getElementById("inner-"+element_id).style.background="url('images/note-b.png') no-repeat";	
				} else if(response_array[2] == "G"){
					if(window.navigator.appVersion.indexOf('MSIE 6') != -1)
						document.getElementById("inner-"+element_id).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/note-g.png');";
					else
						document.getElementById("inner-"+element_id).style.background="url('images/note-g.png') no-repeat";	
				} else if(response_array[2] == "P"){
					if(window.navigator.appVersion.indexOf('MSIE 6') != -1)
						document.getElementById("inner-"+element_id).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/note-p.png');";
					else
						document.getElementById("inner-"+element_id).style.background="url('images/note-p.png') no-repeat";	
				} else if(response_array[2] == "W"){
					if(window.navigator.appVersion.indexOf('MSIE 6') != -1)
						document.getElementById("inner-"+element_id).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/note-w.png');";
					else
						document.getElementById("inner-"+element_id).style.background="url('images/note-w.png') no-repeat";	
				} else {
					if(window.navigator.appVersion.indexOf('MSIE 6') != -1)
						document.getElementById("inner-"+element_id).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/note-r.png');";
					else
						document.getElementById("inner-"+element_id).style.background="url('images/note-r.png') no-repeat";
				}
				document.getElementById("inner-"+element_id).innerHTML = inner_html;
			}
			else { document.location = "./login.php?se" }
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send("element_id=" + element_id);	
} // end load

function edit(element_id){
	if(readCookie('PHPSESSID') == null) document.location = "./login.php?se";
	var xmlhttp = createRequest();
	xmlhttp.open('POST','notes/load-note.php',true);
	xmlhttp.onreadystatechange = function() {  
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200){
				var response = xmlhttp.responseText;
				var response_array = response.split("<<>>");
				document.forms['note_form'].note.value = response_array[1];
				document.forms['note_form'].p.value = response_array[2];
				//document.forms['note_form'].s.value = response_array[6];
				//if(response_array[2] == "H") document.forms['note_form'].urgent[0].checked = true;
				//if(response_array[2] == "L") document.forms['note_form'].urgent[1].checked = true;
				
				if(response_array[3] != ""){
					document.getElementById("file-upload").style.display="none";
					document.getElementById("file-upload-text").style.display="none";
					document.getElementById("file-upload-head").style.display="none";
					document.getElementById("uploaded-file-head").style.display="inline";
					document.getElementById("uploaded-file").style.display="inline";
					document.getElementById("uploaded-file").innerHTML = "<a href=\"uploads/" + response_array[4] + "\" target=_blank style=\"color:#222222;\">"+response_array[3]+"</a> | <a href=\"#\" style=\"color:#222222;\" onClick=\"delete_file('" + response_array[4] + "');\">delete</a>";
				} else {
					document.getElementById("file-upload").style.display="inline";
					document.getElementById("file-upload-text").style.display="inline";
					document.getElementById("file-upload-head").style.display="inline";
					document.getElementById("uploaded-file-head").style.display="none";
					document.getElementById("uploaded-file").style.display="none";
				}
				Effect.Appear("enter-it",{duration:1.0,from:0.0,to:1.0});
			}
			else { document.location = "./login.php?se" }
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send("element_id=" + element_id);	
}

function delete_file(fid){
	if(readCookie('PHPSESSID') == null) document.location = "./login.php?se";
	var xmlhttp = createRequest();
	xmlhttp.open('POST','notes/delete-file.php',true);
	
	xmlhttp.onreadystatechange = function() {  
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200){
				document.getElementById('file-upload').style.display="inline";
				document.getElementById('file-upload-text').style.display="inline";
				document.getElementById('file-upload-head').style.display="inline";
				document.getElementById('uploaded-file').style.display="none";
				document.getElementById('uploaded-file-head').style.display="none";
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send("fid=" + fid);		
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function addItemToList(li){
	var lastIndex = document.getElementById('s').value.lastIndexOf(",");
	if(lastIndex < 0)
		document.getElementById('s').value = li.id ;
	else {
		document.getElementById('s').value =  document.getElementById('s').value.substr(0,lastIndex+1) + li.id ;
	}
	document.getElementById('s').focus();
			
}

