function doLogin(sitePath){
	//alert(sitePath);
	//var poststr = "username=" + encodeURI( document.getElementById("clubinhoLoginUsername").value ) + "&senha=" + encodeURI( document.getElementById("clubinhoLoginPassword").value )+"&tipo=livre";
	var poststr = "tipo=0&username=" + encodeURI( document.getElementById("clubinhoLoginUsername").value ) + "&senha=" + encodeURI( document.getElementById("clubinhoLoginPassword").value );
	//alert(poststr);
	//makePOSTRequestLogin(sitePath + "app/core/internal/login.html", poststr);
	makePOSTRequestLogin(sitePath + "app/core/internal/login.html", poststr);
	
}

var http_request = false;

function makePOSTRequestLogin(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;
   }
   //alert(url+'?'+parameters);
   http_request.onreadystatechange = alertContentsLogin;
   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);
   
   http_request.onreadystatechange=function() {			
			if (http_request.readyState==1){
				//document.getElementById('miolo').innerHTML = '<div style="font-family:Verdana; font-weight:bold; font-size:11pt; text-align:center; padding:3px;">Aguarde...</div>';
			}
			if (http_request.readyState==4){
				//alert(http_request.responseText);
				if(http_request.responseText != "error"){
					if(http_request.responseText == "livre"){
						location.href="core/galeria_livre.html";
					} else {
						location.href="core/atelie.html";
					}
					
					//var content = http_request.responseText;
					
					/*content = reverse(parameters);
					var pos = content.indexOf('=');
					content = reverse(content.substring(0,pos));
					//alert(content);
					if(content == "livre"){
						location.href="galeria_livre.html";
					}*/
				} else {
					alert("Login incorreto.");
				}
				//document.getElementById('miolo').innerHTML = ajax.responseText;
			}
		}
	http_request.send(null);
}

function reverse(val) {
  var inp = val;
  var outp = 0;

  for (i = 0; i <= inp.length; i++) {
    outp = inp.charAt (i) + outp;
  }
  
  return outp.substring(0,outp.length-1);
} 

function alertContentsLogin() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        // alert(http_request.responseText);
         result = http_request.responseText;
         
         if(result == "error"){
        	 document.getElementById("resultLoginError").innerHTML = "*Dados incorretos";
         }else {
        	 document.getElementById("formLogin").innerHTML = "";
        	 document.getElementById("resultLogin").innerHTML = result;
         }
         
         document.getElementById('resultLogin').style = "display:block;";   
         document.getElementById('resultLogin').innerHTML = result;            
      } else {
         alert('There was a problem with the request.');
      }
   }
}

function formataNome(nome, apenasPrimeiro, idContainer){
	
	if(apenasPrimeiro){
		
		var nomes = null;
		
		nomes = explode(" ", trim(nome),0);
		document.getElementById(idContainer).innerHTML = "" + nomes[0];
		
	}

}

function explode(delimiter, string, limit) {
   
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function trim(str){return str.replace(/^\s+|\s+$/g,"");}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


