/* randomizer */
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};


/* codigo para ventana popup */
var newWin = null;
function popUp(strURL, strType, strHeight, strWidth) 
{
	var windowname = rand(100);
	
	if (newWin != null && !newWin.closed)
		newWin.close();
		var strOptions="";
	
	if (strType=="console")
		strOptions="resizable,height="+
		strHeight+",width="+strWidth;
	 
	if (strType=="fixed")
		strOptions="status,height="+
		strHeight+",width="+strWidth;
	 
	if (strType=="elastic")
		strOptions="toolbar,menubar,scrollbars,"+
		"resizable,location,height="+
		strHeight+",width="+strWidth;
	
	if (strType=="scroll")
		strOptions="scrollbars,"+
		"resizable,height="+
		strHeight+",width="+strWidth;
	
	newWin = window.open(strURL, windowname, strOptions);
	newWin.focus();
}

function breakout_of_frame()
{
  // see http://www.thesitewizard.com/archive/framebreak.shtml
  // for an explanation of this script and how to use it on your
  // own website
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}

function focusfirst(formname) 
{
	if (formname)
	{
		document.forms[formname].elements[0].focus();
		document.forms[formname].elements[0].select();
	}
	else
	{
		document.forms[0].elements[0].focus();
		document.forms[0].elements[0].select();
	}
}

function focussecond() 
{
	document.forms[0].elements[1].focus();
	document.forms[0].elements[1].select();
}

/* --------------------------- */

function CheckAll(formname)
{
	count = document.forms[formname].elements.length;
    for (i=0; i < count; i++) 
	{
		document.forms[formname].elements[i].checked = 1;
	}
}

function UncheckAll(formname)
{
	count = document.forms[formname].elements.length;
    for (i=0; i < count; i++) 
	{
		document.forms[formname].elements[i].checked = 0; 
	}
}



function getCheckedItems (formname, action)
{
	var checkboxes = new Array();
	var a_php;
	var thispath;
	
	count = document.forms[formname].elements.length;
    for (i=0; i < count; i++) 
	{
		if (document.forms[formname].elements[i].type == "checkbox")
		{
			if (document.forms[formname].elements[i].checked == 1)
			{
				checkboxes[i] = document.forms[formname].elements[i].value;
			}
		}
	}
	
	a_php = escape(js_array_to_php_array(checkboxes));
	thispath = location.pathname;
	window.location = thispath + "?action=" + action + "&" + "jsarray=" + a_php;
}


function countCheckedItems (formname)
{
	var checkboxes = new Array();
	var a_php;
	var thispath;
	var f = 0;	
	count = document.forms[formname].elements.length;
    for (i=0; i < count; i++) 
	{
		if (document.forms[formname].elements[i].type == "checkbox")
		{
			if (document.forms[formname].elements[i].checked == 1)
			{
				checkboxes[i] = document.forms[formname].elements[i].value;
				f++;
			}
		}
	}

	a_php = escape(js_array_to_php_array(checkboxes));
	return f;
}

function js_array_to_php_array (a)
// This converts a javascript array to a string in PHP serialized format.
// This is useful for passing arrays to PHP. On the PHP side you can 
// unserialize this string from a cookie or request variable. For example,
// assuming you used javascript to set a cookie called "php_array"
// to the value of a javascript array then you can restore the cookie 
// from PHP like this:
//    <?php
//    session_start();
//    $my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
//    print_r ($my_array);
//    ?>
// This automatically converts both keys and values to strings.
// The return string is not URL escaped, so you must call the
// Javascript "escape()" function before you pass this string to PHP.
{
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;
}

// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}

// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

function JsPopUpBox( sHtml , sTitle , nTop , nLeft ) {
/* 
* JsPopUpBox v.0.3
* http://maurizio.mavida.com/
* based on great jQuery  - http://jquery.com/
 
Use: 
*** direct mode ***
JsPopUpBox( "Hello world") // only popup body without title
JsPopUpBox( "Hello world","popup titile") // body e title
JsPopUpBox( "Hello world","popup titl",10,10)  // set top and left position

*** jquery  mode ***
var objpopup = JsPopUpBox( "Hello world" , "title") 
objpopup.css("top","50px")
objpopup.css("left","50px")
objpopup.fadeIn("slow")

*/
	var aPopup = new Array("");
	
	var PopUpCss = function () {

			$("#PopUpBox").css({
				"position"		:	"absolute",
				"background-color"	:	"#ccc",
				"top"			: 	"10px",
				"left"			: 	"10px",
				"height"		:	"250px",
				"width"		:	"400px",
				"z-index"		:	"100",
				"border"		:	"1px solid #000"
			});


			$("#PopUpBox #PopUpHead,#PopUpBox #PopUpClose").css({
				"background-color"	:	"#77829f",
				"height"		:	"18px",
				"padding-top"		:	"2px",
				"border-bottom"	:	"1px solid #000",
				"font-family"		:	"verdana",
				"color"		:	"#fff"
			});

			$("#PopUpBox #PopUpHead").css({
				"font-size"		: 	"14px",
				"text-align"		: 	"left",
				"padding-left"	: 	"10px",
				"cursor"		:	"move",
				"width"		:	"370px"
			});
			
			$("#PopUpBox #PopUpClose").css({
				"font-size"		:	"16px",
				"text-align"		:	"center",
				"font-weight"		:	"bolder",
				"cursor"		:	"pointer",
				"width"		:	"20px"
			});
			
		}	

	var PopUpMouseDown = function (e) {
			dragEnable = false
			
			if (!e) var e = window.event;
			objDrag = (e.target) ? e.target : e.srcElement

			if ( objDrag.className=="PopUpBox_drag"  ){

				dragEnable = true
				objDrag = objDrag.parentNode
				
				x = e.clientX - objDrag.style.left.replace('px','')
				y = e.clientY - objDrag.style.top.replace('px','')
				
				//abilito l'evento onmousemove e onmouseup
				$(document.body).mousemove(PopUpMouseMove)
				$(document.body).mouseup(PopUpMouseUp)

			} else {
				// esco dalla la modalit? di drag
				dragEnable = false
				objDrag = null
				$(document.body).unmousemove(PopUpMouseMove)
				$(document.body).unmouseup(PopUpMouseUp)
			}
			//return false;
		}

	var PopUpMouseMove = function (e) {
			
			if (dragEnable){
				if (!e) var e = window.event;
				objDrag.style.left = (e.clientX -x )+"px"
				objDrag.style.top = (e.clientY  -y)+"px"
			}
			return false;
		}

	var PopUpMouseUp = function (e) {
			// esco dalla la modalit? di drag
			dragEnable = false
			
			$(document.body).unmousemove(PopUpMouseMove)
			$(document.body).unmouseup(PopUpMouseUp)
			return false;
		}
		
	
	if ( $("#PopUpBox").size() == 0 ) {
		aPopup.push("<div id='PopUpBox'>\n")
		aPopup.push("<div class='PopUpBox_drag' style='float:left' id='PopUpHead' >")
		aPopup.push(sTitle)
		aPopup.push("</div>\n<div style='float:right' id='PopUpClose'>x</div>\n")
		aPopup.push("<div style='clear:both' id='PopUpText'></div>\n")
		aPopup.push("</div>\n")
		$(document.body).append( aPopup.join("") )
		PopUpCss()
		$("#PopUpBox #PopUpHead").mousedown( PopUpMouseDown )
		$("#PopUpBox #PopUpClose").click( function() { $("#PopUpBox").remove(); } )
		
	}
	
	if (nTop != undefined) $("#PopUpBox").css("top",top+"px"); 
	if (nLeft != undefined) $("#PopUpBox").css("left",left+"px"); 

	$("#PopUpBox #PopUpText").get(0).innerHTML = sHtml
	
	return $("#PopUpBox")
}

/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;

function GB_show(caption, url, height, width) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img src='close.gif' alt='Close window'/></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
  $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}

// alerta para registrarse
function logincheck()
{
	var c = confirm("Tienes que ser un miembro registrado usar esta funcionalidad. ¿Quieres registrarte? Es gratis!");
	if (c)
	{
		window.location = "/registrar.php";	
	}
	
	return false;
}

function checkLogin()
{
	// halar galleta
	var mbr = $.cookie("logincookie[0]");
	if (!mbr)
	{
		var c = confirm("Necesitas registrate para hacer uso de esta funcionalidad. ¿Quieres registrarte? Es gratis!");
		if (c)	{		window.location = "/registrar.php";		}
		return false;
	}
}

