function WinOpen(url,width,heigth)
{
	window.open(url,'','width='+width+',height='+heigth+',left=100,top=100,scrollbars=yes,resizable=no,toolbar=no,status=no,directories=no,location=no');
}
function WinClose()
{
	window.close();
}

//############## SEND DATA USING AJAX(GET METHOD) - START ##############//

function validate(url,divname,methods,param)
{
	//alert(url + divname + methods + param)
	var xmlHttp ;
	try
    {
         // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	  // Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

     xmlHttp.onreadystatechange=function()
	{
	  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	  {
			var pElement=document.getElementById(divname);
			pElement.innerHTML=xmlHttp.responseText;
	  }
	}
	xmlHttp.open(methods,url,true);
	if(methods == 'POST' || methods == 'Post' )
	{
		xmlHttp.push('Content-type',
        'application/x-www-form-urlencoded');
		xmlHttp.push('Connection', 'close');
	}
	xmlHttp.send(param);
}

//############## SEND DATA USING AJAX(GET METHOD) - END ##############//




//############## UPLOAD IMAGES USING AJAX - START ##############//
var url = "ajax_getvalue.html";
var binary;
var filename;
var mytext;

function upload() {
	filename = document.getElementById('music_file').value;
	mytext = document.getElementById('title').value;
	//document.getElementById('ajaxbutton').disabled = true;

	// request local file read permission
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to read file was denied.");
	}
	
	// open the local file
	var file = Components.classes["@mozilla.org/file/local;1"]
		.createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath( filename );		
	stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
		.createInstance(Components.interfaces.nsIFileInputStream);
	stream.init(file,	0x01, 00004, null);
	var bstream =  Components.classes["@mozilla.org/network/buffered-input-stream;1"]
		.getService();
	bstream.QueryInterface(Components.interfaces.nsIBufferedInputStream);
	bstream.init(stream, 1000);
	bstream.QueryInterface(Components.interfaces.nsIInputStream);
	binary = Components.classes["@mozilla.org/binaryinputstream;1"]
		.createInstance(Components.interfaces.nsIBinaryInputStream);
	binary.setInputStream (stream);

	// start AJAX file upload in 1 second
	window.setTimeout("ajax_upload()", 1000);
}

function ajax_upload() {
	// request more permissions
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to read file was denied.");
	}

	http_request = false;
	http_request = new XMLHttpRequest();
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	// prepare the MIME POST data
	var boundaryString = 'capitano';
	var boundary = '--' + boundaryString;
	var requestbody = boundary + '\n' 
	+ 'Content-Disposition: form-data; name="title"' + '\n' 
	+ '\n' 
	+ mytext + '\n' 
	+ '\n' 
	+ boundary + '\n' 
	+ 'Content-Disposition: form-data; name="music_file"; filename="' 
		+ filename + '"' + '\n' 
	+ 'Content-Type: application/octet-stream' + '\n' 
	+ '\n'
	+ escape(binary.readBytes(binary.available()))
	+ '\n'
	+ boundary;

	document.getElementById('uploading').innerHTML = 
		"requestbody.length=" + requestbody.length;
	
	// do the AJAX request
	http_request.onreadystatechange = requestdone;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "multipart/form-data; \
		boundary=\"" + boundaryString + "\"");
	http_request.setRequestHeader("Connection", "close");
	http_request.setRequestHeader("Content-length", requestbody.length);
	http_request.send(requestbody);

}

function requestdone() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			document.getElementById('uploading').innerHTML = result;            
		} else {
			alert('There was a problem with the request.');
		}
		//document.getElementById('ajaxbutton').disabled = false;
	}
}


//############## UPLOAD IMAGES USING AJAX - END ##############//



//################### POST AJAX -START #####################//
var http_request = false;
   function makePOSTRequest(url, parameters) {
      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;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('msg').innerHTML = result;
         } else {
            alert('There was a problem with the request.');
         }
		 document.getElementById('submitimag').disabled=false;
		  document.getElementById('comment').disabled=false;
      }
   }
   
   function get(obj) {
      var poststr = "mytextarea1=" + encodeURI( document.getElementById("mytextarea1").value ) +
                    "&mytextarea2=" + encodeURI( document.getElementById("mytextarea2").value );
      makePOSTRequest('post.php', poststr);
   }

//################### POST AJAX -END #####################//



function over(row)
{
	row.oldBackgroundColor = row.style.backgroundColor;
	row.style.backgroundColor = '#CCCCCC';
}
function out(row)
{
	row.style.backgroundColor = row.oldBackgroundColor;
}
