// JavaScript Document
/*********** CONTACTS FUNCTIONS*********/
function getConnected(keypress) {
	var divload = document.getElementById('divload');
	var divmsg = document.getElementById('divmsg');
		divmsg.innerHTML = '';
	var divbox = document.getElementById('divbox');
		divbox.style.display = 'none';
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var mailfrom = document.getElementById('email');
	var address1 = document.getElementById('address1');
	var address2 = document.getElementById('address2');
	var city = document.getElementById('city');
	var country = document.getElementById('country');
	var zipcode = document.getElementById('zipcode');
	var phone = document.getElementById('phone');
	var answer_1 = document.getElementById('answer_1');
	var answer_2 = document.getElementById('answer_2');
	var answer_3 = document.getElementById('answer_3');
	
	var answers = '';
    if (answer_1.checked) {
        answers = answer_1.value;
    } else if (answer_2.checked) {
        answers = answer_2.value;
    } else if (answer_3.checked) {
        answers = answer_3.value;
    }
    				
	if (firstname.value != '' && firstname.value != 'undefined' && firstname.value != null) {
    if (lastname.value != '' && lastname.value != 'undefined' && lastname.value != null) {
        if (mailfrom.value != '' && mailfrom.value != 'undefined' && mailfrom.value != null) {
        if (checkEmail(mailfrom.value)) {
		if (phone.value != '' && phone.value != 'undefined' && phone.value != null) {
		if (checkPhone(phone.value)) {
		    if (address1.value != '' && address1.value != 'undefined' && address1.value != null) {
		    if (city.value != '' && city.value != 'undefined' && city.value != null) {
		    if (country.value != '' && country.value != 'undefined' && country.value != null) {
		    if (zipcode.value != '' && zipcode.value != 'undefined' && zipcode.value != null) {
			if (checkZipcode(zipcode.value)) {
				        if (answers != '') {
                            
	                        var btnsend = document.getElementById('btnsend'); //Radio buttons
	                            btnsend.disabled = true;
								
								divmsg.innerHTML = '';
								divbox.style.display = 'none';
								divload.style.display = 'block';
								
														
						getData('setData.aspx?destination=divmsg&firstname='+firstname.value+'&lastname='+lastname.value+'&mailfrom='+mailfrom.value+'&address1='+address1.value+'&address2='+address2.value+'&city='+city.value+'&country='+country.value+'&zipcode='+zipcode.value+'&phone='+phone.value+"&answers="+answers);	
				
	                    } else {
							divload.style.display = 'none';
		                    divbox.style.display = 'block'; // visibility = 'visible';
		                    divmsg.innerHTML = 'Debe seleccionar una respuesta.';
		                    answer_1.focus();
	                    }
                } else {
					divload.style.display = 'none';
                    divbox.style.display = 'block';
                    divmsg.innerHTML = 'El c&oacute;digo postal tiene un formato incorrecto.';	
                    zipcode.focus();		
                }
                } else {
					divload.style.display = 'none';
                    divbox.style.display = 'block';
                    divmsg.innerHTML = 'El c&oacute;digo postal es requerido.';	
                    zipcode.focus();		
                }
            } else {
				divload.style.display = 'none';
	            divbox.style.display = 'block';
	            divmsg.innerHTML = 'El pa&iacute;s es requerido.';
	            country.focus();			
            }
            } else {
				divload.style.display = 'none';
	            divbox.style.display = 'block';
	            divmsg.innerHTML = 'El pueblo es requerido.';
	            city.focus();			
            }
            } else {
				divload.style.display = 'none';
	            divbox.style.display = 'block';
	            divmsg.innerHTML = 'Su direcci&oacute;n postal es requerida.';
	            address1.focus();			
            }
		} else {
			divload.style.display = 'none';
			divbox.style.display = 'block'; // visibility = 'visible';
			divmsg.innerHTML = 'Su tel&eacute;fono tiene un formato incorrecto.';
			phone.focus();			    
			}
		} else {
			divload.style.display = 'none';
			divbox.style.display = 'block'; // visibility = 'visible';
			divmsg.innerHTML = 'Su tel&eacute;fono es requerido.';
			phone.focus();
		}
        } else {
			divload.style.display = 'none';
            divbox.style.display = 'block';
            divmsg.innerHTML = 'Su email tiene un formato incorrecto.';	
            mailfrom.focus();		
        }
        } else {
			divload.style.display = 'none';
            divbox.style.display = 'block';
            divmsg.innerHTML = 'Su email es requerido.';	
            mailfrom.focus();		
        }
    } else {
		divload.style.display = 'none';
	    divbox.style.display = 'block'; // visibility = 'visible';
	    divmsg.innerHTML = 'Su apellido es requerido.';	
	    lastname.focus();
    }
	} else {
		divload.style.display = 'none';
		divbox.style.display = 'block'; // visibility = 'visible';
		divmsg.innerHTML = 'Su nombre es requerido.';	
		firstname.focus();
	}
}

function setData(dest, msg, err) {
	var divload = document.getElementById('divload');
		divload.style.display = 'none';
	var divbox = document.getElementById('divbox');
		divbox.style.display = 'block';
	var divmsg = document.getElementById(dest);
		divmsg.innerHTML = msg;
	var btnsend = document.getElementById('btnsend'); //Radio buttons
	    btnsend.disabled = false;	 
	
	if (err == 'False'){
	    
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var mailfrom = document.getElementById('email');
	var address1 = document.getElementById('address1');
	var address2 = document.getElementById('address2');
	var city = document.getElementById('city');
	var country = document.getElementById('country');
	var zipcode = document.getElementById('zipcode');
	var phone = document.getElementById('phone');
	var answer_1 = document.getElementById('answer_1');
	var answer_2 = document.getElementById('answer_2');
	var answer_3 = document.getElementById('answer_3');
	    //var employees = document.getElementById('Empleados'); //Radio buttons
	           
        firstname.value = '';
        lastname.value = '';
        mailfrom.value = '';
        address1.value = '';
        address2.value = '';
        city.value = '';
        country.value = '';
        zipcode.value = '';
        phone.value = '';
	    answer_1.checked = false;
	    answer_2.checked = false;
	    answer_3.checked = false;
        
    
	}
		
}

/*********** /CONTACT FUNCTIONS *********/

/*********** FUNCTION **********/
function getLists(d, f){							
	getData('getData.aspx?d='+d+'&f='+f);	
}

function getDetail(src, srcid){							
	getData('getData.aspx?d=divDetail&f='+src+'&q='+srcid);	
}
/*********** /FUNCTION **********/

/*********** CITIES FUNCTION **********/
function getCities(){							
	getData('getData.aspx?d=divCities&f=cities');
	
}
/*********** /CITIES FUNCTION **********/

/*********** LOADDATA FUNCTION **********/
function loadData(dest, arr, msg, err){
	var src = '';
	var arrid = 0;
	
	switch(dest){
			case 'divNews':
				src = 'news';
				break;
			case 'divArticles':
				src = 'articles';
				break;
			case 'divEvents':
				src = 'events';
				break;	
	}
	
	var data = "<table width=\"100%\" border=\"0\" cellspacing=\"4\" cellpadding=\"0\">";
	
	if (err == 'False'){
		//var arrnew = new Array();
		    //alert(arrcount);
			

		if (arr != '' && arr != 'undefined' && arr != null) {
		var arrcount = arr.length;	
			arrnew = arr;
		
			if (arrcount > 0 ) {
				for (i=0; i < arrcount; i++){
				var arrnew = arr[i];
					arrid = arrnew[0];
					arrtitle = arrnew[1];
					id = dest+"_"+i; // array [0]
					
					data += "  <tr>";
					data += "    <td width=\"5%\" height=\"20\" align=\"right\" valign=\"bottom\"><img src=\"images/selector.gif\" alt=\"&gt;\" width=\"14\" height=\"20\" style=\"display: none;\" id=\""+id+"\" /></td>";
					data += "    <td width=\"95%\" align=\"left\" valign=\"middle\"><a href=\"details.aspx?d="+dest+"&f="+src+"&q="+arrid+"\" onmouseover=\"show('"+id+"')\" onmouseout=\"hide('"+id+"')\">"+arrtitle+"</a></td>";
					data += "  </tr>";
					//data += arrnew[i]+"<br/>";	
				}	
			} else {
					data += "  <tr>";
					data += "    <td width=\"5%\" height=\"20\" align=\"right\" valign=\"bottom\"><img src=\"images/selector.gif\" alt=\"&gt;\" width=\"14\" height=\"20\" style=\"display: none;\" /></td>";
					data += "    <td width=\"95%\" align=\"left\" valign=\"middle\">No hay data disponible.</td>";
					data += "  </tr>";
			}
			
		}			 
	} else {
		
		data += '<tr><td align="center">'+msg+'</td></tr>';
		
	}
	
		data += "</table>";
		
		var divdest = document.getElementById(dest);
			divdest.style.display = 'block';
			divdest.innerHTML = data;
		/*var divload = document.getElementById('divload'); 
			divload.style.display = 'none';*/
	
			
}
/*********** /LOADDATA FUNCTION **********/


/*********** LOAD DROPDOWN DATA FUNCTION **********/
function loadDropDownData(dest, arr, msg, err){
	
	if (err == 'False'){
		
		if (arr != '' && arr != 'undefined' && arr != null) {
		var arrcount = arr[0].length;	
			arrnew = arr;
		    //alert(arrcount);
		var data = '<select border="1" cellspacing="3">';

			if (arrcount > 0 ) {
				for (i=0; i < arrcount; i++){
					var arrvalue = arrnew[0];	
					var arrtext = arrnew[1];
					
					data += '<option value="'+arrvalue+'">';
					data += 	arrtext;
					data += '</option>';
				}	
			} else {
				data += '<option value="-1">No hay disponibles</option>';
			}
					data += '</select>';
					
			var divdest = document.getElementById(dest);
				divdest.style.display = 'block';
				divdest.innerHTML = data;
		}		
	} else {
		
		var divbox = document.getElementById('divbox');
			divbox.style.display = 'block';
		var divmsg = document.getElementById('divmsg');
			divmsg.innerHTML = msg;
	}
		/*var divload = document.getElementById('divload'); 
			divload.style.display = 'none';*/
	
			
}
/*********** /LOAD DROPDOWN DATA FUNCTION **********/


/*********** LOAD DETAILDATA FUNCTION **********/
function loadDetail(dest, src, arr, msg, err){
	var data = '<div style="padding-bottom: 10px; background-color: #e5e5e5; text-align: left; " >';
	
	if (err == 'False'){
			
		switch(src){
		case 'newsdetail':
				
			if (arr != '' && arr != 'undefined' && arr != null) {
			var	src = 'newsdetail';	
			var arrcount = arr[0].length;	
				arrnew = arr;
			
				if (arrcount > 0 ) {
					//for (i=0; i < arrcount; i++){
						var arrid = arrnew[0];
						var arrtitle = arrnew[1];	
						var arrbrief = arrnew[2];
						var arrurl = arrnew[3];
						var arrdate = arrnew[4];
						
						data += '<div style="text-align: left; margin-left: 10px; margin-right: 10px;">';
						data += '<div style="font-weight: bold; font-size: 14px; padding-bottom: 5px;">'+arrtitle+'</div>';
						data += '<div style="font-size: 10px; font-style: italic; padding-bottom: 5px;">'+arrdate+'</div>';
						data += '<div style=" padding-bottom: 5px;">'+arrbrief+'</div>';
						data += '<div style=" padding-bottom: 5px;"><a href="'+arrurl+'" title="Ver noticia" target="_blank">Ver noticia</a></div>';
						data += '</div>';
						
					//}							
				} else {
					data += '<div>No hay disponibles</div>';
				}
			} else {
				data += '<div>No hay disponibles.</div>';
			}
			
			break;
			
		case 'articlesdetail':
		
			if (arr != '' && arr != 'undefined' && arr != null) {
			var	src = 'articlesdetail';
			var arrcount = arr[0].length;	
				arrnew = arr;
			
				if (arrcount > 0 ) {
					//for (i=0; i < arrcount; i++){
						var arrid = arrnew[0];
						var arrtitle = arrnew[1];	
						var arrbrief = arrnew[2];
						var arrurl = arrnew[3];
						var arrdate = arrnew[4];
						
						data += '<div style="text-align: left; margin-left: 10px; margin-right: 10px;">';
						data += '<div style="font-weight: bold; font-size: 14px; padding-bottom: 5px;">'+arrtitle+'</div>';
						data += '<div style="font-size: 10px; font-style: italic; padding-bottom: 5px;">'+arrdate+'</div>';
						data += '<div style=" padding-bottom: 5px;">'+arrbrief+'</div>';
						data += '<div style=" padding-bottom: 5px;"><a href="'+arrurl+'" title="Ver noticia" target="_blank">Ver articulo</a></div>';
						data += '</div>';
						
					//}							
				} else {
					data += '<div>No hay disponibles</div>';
				}
			} else {
				data += '<div>No hay disponibles.</div>';
			}
			
			break;
			
		case 'eventsdetail':
			if (arr != '' && arr != 'undefined' && arr != null) {
			var	src = 'eventsdetail';
			var arrcount = arr[0].length;	
				arrnew = arr;
			
				if (arrcount > 0 ) {
					//for (i=0; i < arrcount; i++){
						var arrid = arrnew[0];
						var arrtitle = arrnew[1];	
						var arrbrief = arrnew[2];
						var arrcontactname = arrnew[3];
						var arrcontactphone = arrnew[4];
						var arrcontactemail = arrnew[5];
						var arrurl = arrnew[6];
						var arrstartdate = arrnew[7];
						var arrenddate = arrnew[8];
						
						data += '<div style="text-align: left; margin-left: 10px; margin-right: 10px;">';
						data += '<div style="font-size: 14px; font-weight: bold; padding-bottom: 5px;">'+arrtitle+'</div>';
						data += '<div style="font-size: 10px; font-style: italic; padding-bottom: 5px;">Comienza: '+arrstartdate+' Termina: '+arrenddate+'</div>';
						data += '<div style=" padding-bottom: 5px;">'+arrbrief+'</div>';
						data += '<div style=" padding-bottom: 5px;">Contacto: '+arrcontactname+'</div>';
						data += '<div style=" padding-bottom: 5px;">Telefono: '+arrcontactphone+'</div>';
						data += '<div style=" padding-bottom: 5px;">Email: <a href=mailto:"'+arrcontactemail+'">'+arrcontactemail+'</a></div>';
						data += '<div style=" padding-bottom: 5px;"><a href="'+arrurl+'" title="Ver noticia" target="_blank">Mas detalles</a></div>';
						data += '</div>';
						
					//}							
				} else {
					data += '<div>No hay disponibles</div>';
				}
			} else {
				data += '<div>No hay disponibles.</div>';
			}
			
			break;	
		}	
	} else {
		
		data += "<div>"+msg+"</div>";
		
	}
		data += '</div>';
		
	var divdest = document.getElementById(dest);
		divdest.style.display = 'block';
		divdest.innerHTML = data;
	/*var divload = document.getElementById('divload'); 
		divload.style.display = 'none';*/
	
			
}
/*********** /LOAD DETAILDATA FUNCTION **********/

/*********** CALLBACK FUNCTION *********/
function getData (dataSource) {
	var XMLHttpRequestObject = false;
	
	if (window.XMLHttpRequest) {
		XMLHttpRequestObject = new XMLHttpRequest();
		
	   if (XMLHttpRequestObject.overrideMimeType) {
	  		XMLHttpRequestObject.overrideMimeType('text/html');
			//XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			// See note below about this line
		}

	}
	else if (window.ActiveXObject) {
		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (XMLHttpRequestObject) {
		XMLHttpRequestObject.open("POST", dataSource, true);
		
		XMLHttpRequestObject.onreadystatechange = function() {
			//try {
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
					
				var setdata_fn = new Object();
					setdata_fn = XMLHttpRequestObject.responseText;
					eval(setdata_fn);
					
				} else if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status != 200){
							
					var divbox = document.getElementById('divbox');
						divbox.style.display = 'block';
					var divmsg = document.getElementById('divmsg');
						divmsg.style.display = 'block';
						divmsg.innerHTML = 'Hubo un error no se pudo procesar la informaci&oacute;n, favor trate m&aacute;s tarde.<br/>';
					/*var divload = document.getElementById('divload');
						divload.style.display = 'none';	*/
						
		        }				
				
            /*} 
			catch( e ) {
					var divload = document.getElementById('divload');
						divload.style.display = 'none';				
					var divbox = document.getElementById('divbox');
						divbox.style.display = 'block';
					var divmsg = document.getElementById('divmsg');
						divmsg.style.display = 'block';
						divmsg.innerHTML = 'Hubo un error no se pudo procesar la informaci&oacute;n, favor trate m&aacute;s tarde.<br/>';	
				divmsg.innerHTML += 'Caught Exception: ' + e.description;
			}*/

		}
				
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    XMLHttpRequestObject.send(dataSource);
		//XMLHttpRequestObject.send(null);	

	}
	
}

/*********** /CALLBACK FUNCTION *********/

/*********** MORE FUNCTIONS *********/
function replaceAll(text, strA, strB)
{
    while ( text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}

function checkEmail(email) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	else {
		return false;
	}
}

function checkPhone(phone) {
var filter = /\d{3}\-\d{3}\-\d{4}/;
	if (filter.test(phone)) {
		return true;
	}
	else {
		return false;
	}
}

function checkZipcode(zipcode) {
var filter = /\d{5,}/;
	if (filter.test(zipcode)) {
		return true;
	}
	else {
		return false;
	}
}

function checkDate(sdate) {
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sdate)) {
		var dArr = sdate.split("/");
		var d = new Date(sdate);
			
		return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2];
	}
	else {
		return false;
	}
}
/*********** /MORE FUNCTIONS *********/