﻿/*
 * Funciones de formularios
 */
 
/*function uploadfile(pNameField, pCtrlName, pFileName, pPath){
var sFeatures = 'toolbar=0,status=0,width=350,height=150';
	window.open('uploadfile.php?nf='+pNameField+'&cn=' + pCtrlName+'&fn='+pFileName+'&pt='+pPath+'', 'UploadFile', sFeatures);
}*/

/**
 * Checks/unchecks all rows
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 * @param   string   basename of the element
 * @param   integer  min element count
 * @param   integer  max element count
 *
 * @return  boolean  always true
 */

// modified 2004-05-08 by Michael Keck <mail_at_michaelkeck_dot_de>
// - set the other checkboxes (if available) too
function setCheckboxesRange(the_form, do_check, basename)
{
	for (var i = 0; i < document.forms[the_form].elements[basename].length; i++) {
        if (typeof(document.forms[the_form].elements[basename][i]) != 'undefined') {
            document.forms[the_form].elements[basename][i].checked = do_check;
        }
/*        if (typeof(document.forms[the_form].elements[basename + i + 'r']) != 'undefined') {
            document.forms[the_form].elements[basename + i + 'r'].checked = do_check;
        }*/
    }

    return true;
} // end of the 'setCheckboxesRange()' function


function setCheckbox(the_form, basename, the_value)
{
    for (var i = 0; i < document.forms[the_form].elements[basename].length; i++) {
        if (typeof(document.forms[the_form].elements[basename][i]) != 'undefined') {
	        if (document.forms[the_form].elements[basename][i].value == the_value) {
	            document.forms[the_form].elements[basename][i].checked = !document.forms[the_form].elements[basename][i].checked;
			}
        }
/*        if (typeof(document.forms[the_form].elements[basename + i + 'r']) != 'undefined') {
            document.forms[the_form].elements[basename + i + 'r'].checked = do_check;
        }*/
    }

    return true;
} // end of the 'setCheckboxesRange()' function


function logout(){
	if(confirm('Do you really want to logout?')){
		window.location.href = "inc/logout.php";
	}
}

function initPage() {
	$('divPage').className = (screen.width >= 1024)?"wideScreen":"";
	$nBodyHeight = $('divBody').getHeight();
	if(($('divTop').getHeight() + $nBodyHeight) > $('divLeftMenu').getHeight())
		$('divLeftMenu').style.height = ($('divTop').getHeight() + $nBodyHeight) + "px";
	if($nBodyHeight > $('divBanners').getHeight())
		$('divBanners').style.height = $('divBody').getHeight() + "px";
}

function ratePage(nPage, nRate){
	for(var i = 0; i <= 4; i++){
		$('rate' + i).src = (i <= nRate)?"img/rate_on.gif":"img/rate_off.gif";
	}

	new Ajax.Request('rate_page.php', {
							parameters: {page: nPage, rate: nRate},
						 	onComplete: function(result, oJson){
								$('tdRateMessage').innerHTML = oJson.msg;
								$('divRateMeter').style.width = oJson.rate + "px";
							},
							onException: function(e, m){ alert(m);}
	});
}

function sendFriend(sUrl){
	var sQryString = $H({url: sUrl, title: document.title}).toQueryString();
	window.location.href = "send_friend.php?" + sQryString;
}

function submitForm(oForm){
	
	if(isValid(oForm))
		oForm.submit();
	else{
		return false;
	}
}

function loginClient(oForm){
	
	if(isValid(oForm)){
		new Ajax.Request("inc/dologin.php", {
		  method: 'post',
		  parameters: { login: $F('login'), 
						pass: $F('pass')  
		  },
		  onFailure: function(transport) {
		    alert("Error");
		  },
		  onSuccess: function(transport) {
			//alert(transport.responseText);
		    window.location = transport.responseText;
		  }
		});
	}
	return false;
	
}

function printImage(sImage)
{
	document.getElementById("divPrint").innerHTML = "<img src=\"" + sImage + "\">";
	window.print();
}

function isValid(oForm){
	
	switch(oForm.name){
		case "frmServices":
			if(isWhitespace(oForm['company'].value)){
				alert("Error invalid company name");
				oForm['company'].focus();
				return false;
			}
			if(isWhitespace(oForm['person'].value)){
				alert("Error invalid person name");
				oForm['person'].focus();
				return false;
			}
			if(!isEmail(oForm['email'].value)){
				alert("Error wrong email format");
				oForm['email'].focus();
				return false;
			}
			
			oForm['act'].value = "consult_service";
		break;
		case "frmWork":
			if(isWhitespace(oForm['cv'].value)){
				alert("Error el curriculum no puede estar vacio");
				oForm['cv'].focus();
				return false;
			}
			oForm['act'].value = "send_cv";
		break;
		case "frmSubscribe":
			if(!isEmail(oForm['email'].value)){
				alert("¡Error formato de e-mail incorrecto!");
				oForm['email'].focus();
				return false;
			}
			
			oForm['act'].value = "do_subscribe";
		break;
		case "frmLogin":
			if(isWhitespace(oForm['login'].value)){
				alert("¡Error el nombre de usuario no puede estar vacio!");
				oForm['login'].focus();
				return false;
			}
			if(isWhitespace(oForm['pass'].value)){
				alert("¡Error la contraseña no puede estar vacia!");
				oForm['pass'].focus();
				return false;
			}
			
		break;
	}
	return true;
}