// JavaScript Document

function add() { 
if (window.sidebar&&window.sidebar.addPanel) 
window.sidebar.addPanel("Roca","http://www.rocaperu.com",""); 
else 
window.external.AddFavorite("http://www.rocaperu.com","Roca") 
};

function Imgsrc(myUrl, vNumI) {
	newimg = new Image();
	newimg.src = myUrl;
	document.getElementById('Img_src' + vNumI).src = newimg.src;
}

function command() {
	switch (command.arguments[0]) {
		case "link" :
			var lnk = "";
			switch( command.arguments[1] ) {	
				case "lifesciences" :
					lnk = "http://www.gelifesciences.com/";
				break;
				case "ge" :
					lnk = "http://www.gehealthcare.com/";
				break;
				
				case "datex":
					lnk = "http://www.gehealthcare.com/";
				break;
				
				case "ohmedamedical" :
					lnk = "http://www.gehealthcare.com/";
				break;
				
				case "zeiss" :
					lnk = "http://www.zeiss.de/surgical";
				break;
				
				case "stryker" :
					lnk = "http://www.stryker.com/";
				break;
				
				case "elekta" :
					lnk = "http://www.elekta.com/";
				break;
				
				case "getinge" :
					lnk = "http://www.getinge.com/";
				break;
				
				case "merivaara" :
					lnk = "http://www.merivaara.com/";
				break;
				
				case "ptw" :
					lnk = "http://www.ptw.de/";
				break;
				
				case "nucletron":
					lnk = "http://www.nucletron.com/";
				break;
			};
			if (!(lnk == "" || lnk == null)) {
					window.open (lnk, "_blank");
			};
		break;
		
		case "enviar" :
			var msg = "";
			msg += isEmpty(command.arguments[1].nombre) ? "Ingrese sus nombres y apellidos.\n" : "";
			msg += validatePhone(command.arguments[1].telefono,'');
			msg += isValidOptionSelected(command.arguments[1].ubicacion) ? "" : "Seleccione una ubicaci\xF3n.\n";
			msg += isEmail(command.arguments[1].correo.value) ? "" : "Ingrese un correo electr\xF3nico v\xE1lido.\n";
			msg += isEmpty(command.arguments[1].consulta) ? "Ingrese su consulta o pedido.\n" : "";
			msg += (isValidChars(command.arguments[1].consulta) || isValidChars(command.arguments[1].nombre )) ? "" : "Uno o m\xE1s campos contienen caracteres inv\xE1lidos.\n";
			
			if (msg != '') { alert(msg); }
			else { command.arguments[1].submit(); };
		break;
		
		case "soporte" :
			var msg = "";
			msg += isEmpty(command.arguments[1].nombre) ? "Ingrese los nombres y apellidos de la persona quien reporta el incidente.\n" : "";
			msg += validatePhone(command.arguments[1].telefono,'Ingrese su n\xFAmero de tel\xE9fono m\xF3vil.\n');
			msg += isEmail(command.arguments[1].correo.value) ? "" : "Ingrese un correo electr\xF3nico v\xE1lido.\n";
			msg += isEmpty(command.arguments[1].institucion) ? "Ingrese el nombre de la Instituci\xF3n.\n" : "";
			msg += validatePhone(command.arguments[1].telefono_institucion,'Ingrese un n\xFAmero telef\xF3nico de la instituci\xF3n.\n');
			msg += isEmpty(command.arguments[1].equipo) ? "Ingrese el nombre del equipo.\n" : "";
			msg += isEmpty(command.arguments[1].marca) ? "Ingrese la marca del equipo.\n" : "";
			msg += isEmpty(command.arguments[1].modelo) ? "Ingrese el modelo del equipo.\n" : "";
			msg += isEmpty(command.arguments[1].serie) ? "Ingrese la serie del equipo.\n" : "";
			msg += isEmpty(command.arguments[1].descripcion_incidente) ? "Ingrese la descripci\xF3n del incidente.\n" : "";
			msg += (isValidChars(command.arguments[1].descripcion_incidente) || isValidChars(command.arguments[1].nombre) || isValidChars(command.arguments[1].institucion) || isValidChars(command.arguments[1].equipo) || isValidChars(command.arguments[1].marca) || isValidChars(command.arguments[1].modelo) || isValidChars(command.arguments[1].serie) ) ? "" : "Uno o m\xE1s campos contienen caracteres inv\xE1lidos.\n";
			if (msg != '') { alert(msg); }
			else { command.arguments[1].submit(); };
		break;
		
		case "recomienda" :
			var msg = "";
			msg += isEmpty(command.arguments[1].remitente) ? "Ingrese sus nombres y apellidos.\n" : "";
			msg += isEmail(command.arguments[1].correo_remitente.value) ? "" : "Ingrese su correo electr\xF3nico v\xE1lido.\n";
			msg += isEmpty(command.arguments[1].destinatario) ? "Ingrese los nombres y apellidos del destinatario.\n" : "";
			msg += isEmail(command.arguments[1].correo_destinatario.value) ? "" : "Ingrese el correo electr\xF3nico v\xE1lido del remitente.\n";
			if (msg != '') { alert(msg); }
			else { command.arguments[1].submit(); };
		break;
		
		case "select" :
			if (command.arguments[1].selectedIndex >0) {
			command.arguments[1].form.submittype.value = "1";
			command.arguments[1].form.selecttype.value = command.arguments[1].name;
			command.arguments[1].form.submit();
			};
		break;
		
		case "buscar" :
			var msg = "";
			msg += isEmpty(command.arguments[1].query) ? "Ingrese un texto en el campo de b\xFAsqueda\n" : "";
			msg += (isValidChars(command.arguments[1].query)) ? "" : "El campo de b\xFAsqueda contiene caracteres inv\xE1lidos.\n";
			if (msg != '') { alert(msg); }
			else {
				command.arguments[1].submittype.value = "0";
				command.arguments[1].submit();
			};
		break;
		
		case "detalle" :
			window.document.location.href = "producto-detalle.php?cp=" + command.arguments[1];
		break;
		
		case "prod_especialidad" :
			var frm = command.arguments[1].form;
			var obj = command.arguments[1];
			var idx = obj.selectedIndex;
			var cp = obj.options[idx].value;
			if (idx > 0 ) {
				window.document.location.href = "producto-detalle.php?cp=" + cp;
			};
		break;
		
		case "prodxespecialidad" :
			var frm = window.document.productos;
			frm.especialidad.value = command.arguments[1];
			frm.submit();
		break;
		
		
	};
};

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
};

function isEmpty(mytext) {
var re = /^\s{1,}$/g; //match any white space including space, tab, form-feed, etc.
if ((mytext.value.length==0) || (mytext.value==null) || (mytext.value=='')|| ((mytext.value.search(re)) > -1)) {
return true; }
else { return false; }
};

function validatePhone(fld, msg) {
    var error = "";
	var msgdefault = "Ingrese un n\xFAmero telef\xF3nico.\n";
	var msgtxt = (msg == null || msg == '') ? msgdefault : msg;
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = msgtxt;
    } else if (isNaN(parseInt(stripped))) {
        error = "El n\xFAmero telef\xF3nico contiene caracteres no v\xE1lidos.\n";
    };
    return error;
}

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

function isValidChars(fld) {
	var error = true;
    var illegalChars= /[\*%\(\)\<\>\\\/\"\[\]]/;
	if (fld.value.match(illegalChars)) {
	   error = false;
	};
	return error;
};

function isValidOptionSelected(fld){
	var frm = fld.form;
	var idx = fld.selectedIndex;
	if (fld.options[idx].value == '') { return false; }
	else { return true; };
};
