//Javascript File
var host_address = "http://" + window.location.hostname + "/";

function BookmarkLink(){
	if(window.sidebar){ // Mozilla Firefox Bookmark
		window.sidebar.addPanel(document.title, location.href,"");
	}else if(window.external){ // IE Favorite
		window.external.AddFavorite(location.href,document.title);
	}
}

function HomePage(){
	var ua = navigator.userAgent.toLowerCase();
	
	if(ua.indexOf("opera") != -1){ 
	
	} else if(ua.indexOf("msie") != -1){
		HowTo = "Dirijase a 'Herramientas>Opciones' y coloque 'www.miestilofotolog.com.ar' donde dice 'Pagina de Inicio' y luego aplique los cambios.";
	} else if(ua.indexOf("safari") != -1){ 

	} else if(ua.indexOf("firefox") != -1){ 
		HowTo = "Dirijase a 'Herramientas>Opciones' y coloque 'www.miestilofotolog.com.ar' donde dice 'Pagina de Inicio' y luego aplique los cambios.";
	}
	
	window.alert(HowTo);
}

//-----for the login procedure----------
function frmLogin(where) {
	var username = document.getElementById("username").value;
	var pass = document.getElementById("pass").value;
	
	if(username != ""){
		if(pass != ""){
			//divisory = where; //declared en ajax.js
			//var poststr = "username=" + encodeURIComponent(username) +"&pass=" + encodeURIComponent(pass);
			//hacerPOSTRequest(host_address + 'inc/login.php', poststr);
                $.post(host_address + 'inc/login.php',
                    {username: encodeURIComponent(username) , pass: encodeURIComponent(pass)},
                   function(msg){
                       $("#post").html(msg);
                   }
                 );
		} else {
			window.alert("Por favor, introduzca su clave e intente nuevamente.");
		}
	} else {
		window.alert("Por favor, introduzca su ID e intente nuevamente.");	
	}
}

function empty_frmLogin(all){
	if (all == "yes"){ //everything to null
		document.getElementById("username").value = "";
		document.getElementById("pass").value = "";
	} else if (all == "no") { //just the password
		document.getElementById("pass").value = "";
	}
}

//-----login and redirect back to page------
function frmLogin_redirectBack(host_address,redirect) {
	var username = document.getElementById("username").value;
	var pass = document.getElementById("pass").value;
	
	if(username != ""){
		if(pass != ""){
			divisory = 'colTwo'; //declared en ajax.js
			var poststr = "username=" + encodeURIComponent(username) + 
						"&pass=" + encodeURIComponent(pass) +
						"&redirect=" + encodeURIComponent(redirect);
			hacerPOSTRequest(host_address + 'inc/login.php', poststr);
		} else {
			window.alert("Por favor, introduzca su clave e intente nuevamente.");
		}
	} else {
		window.alert("Por favor, introduzca su ID e intente nuevamente.");	
	}
}

//-----Form Checks----------
function checkFrmAccount(){
	var pass = document.frmAccountInfo.newPass.value;
	var pass_r = document.frmAccountInfo.newPass_r.value;
	var illegalChars = /\W/;
	
	if(pass != ""){
		if(pass == pass_r){
			if(!illegalChars.test(pass)){
				if(pass.length >= 6){
					return true;
				} else {
					window.alert("La clave debe tener por lo menos 6 digitos.");
					document.frmAccountInfo.newPass.focus();
					return false;
				}
			} else {
				window.alert("La clave solamente debe contener letras, numeros y guion bajo.");
				document.frmAccountInfo.newPass.focus();
				return false;
			}
		} else {
			window.alert("Las claves no coinciden.");
			document.frmAccountInfo.newPass.focus();
			return false;
		}
	}
}

function checkFrmRegister() {
	
	var username = document.frmRegister.username.value;
	var pass = document.frmRegister.pass.value;
	var pass_r = document.frmRegister.pass_r.value;
	var email = document.frmRegister.email.value;
	var email_r = document.frmRegister.email_r.value;
	var country = document.frmRegister.country.value;
	var state = document.frmRegister.state.value;
	
	var i;
	for(i=0;i<document.frmRegister.sex.length;i++){
    	if(document.frmRegister.sex[i].checked)
    		break;
    }
	var sex = document.frmRegister.sex[i].value;

	var bday = document.frmRegister.day.value;
	var bmonth = document.frmRegister.month.value;
	var byear = document.frmRegister.year.value;
	var captcha = document.frmRegister.captcha.value;
	var illegalChars = /\W/;
 	 // allow only letters, numbers, and underscores

	if(username != ""){
		if(pass != ""){
			if(pass_r != ""){
				if(email != ""){
					if(email_r != ""){
						if(country != "0--Seleccione uno-"){
							if(state != ""){
								if(captcha != ""){
									if(email == email_r){
										if(!illegalChars.test(username)){
											if(isValidEmail(email)){
												if(!illegalChars.test(pass)){
													if(pass == pass_r){
														if(pass.length >= 6){
															if(checkDate(bday, bmonth, byear)){
																return true;
															} else {
																window.alert("La fecha ingresada no existe.");
																document.frmRegister.day.focus();
																return false;
															}
														} else {
															window.alert("La clave debe tener por lo menos 6 digitos.");
															document.frmRegister.pass.focus();
															return false;
														}
													} else {
														window.alert("Las claves ingresadas son diferentes.");
														document.frmRegister.pass.focus();
														return false;
													}
												} else {
													window.alert("La clave solamente debe contener letras, numeros y guion bajo.");
													document.frmRegister.pass.focus();
													return false;
												}
											} else {
												window.alert("Email invalido.");
												document.frmRegister.email.focus();
												return false;	
											}
										} else {
											window.alert("El nombre de usuario solamente debe contener letras, numeros y guion bajo.");
											document.frmRegister.username.focus();
											return false;	
										}
									} else {
										window.alert("Los emails ingresados son diferentes.");
										document.frmRegister.email.focus();
										return false;
									}
								} else {
									window.alert("Por favor, introduzca lo que ve en la imagen.");
									document.frmRegister.captcha.focus();
									return false;
								}
							} else {
								window.alert("Por favor, introduzca la provincia donde habita.");
								document.frmRegister.state.focus();
								return false;
							}
						} else {
							window.alert("Por favor, seleccione un pais.");
							document.frmRegister.country.focus();
							return false;
						}
					} else {
						window.alert("Por favor, repita su email.");
						document.frmRegister.email_r.focus();
						return false;
					}
				} else {
					window.alert("Por favor, introduzca su email.");
					document.frmRegister.email.focus();
					return false;
				}
			} else {
				window.alert("Por favor, repita su clave.");
				document.frmRegister.pass_r.focus();
				return false;
			}
		} else {
			window.alert("Por favor, introduzca su clave.");
			document.frmRegister.pass.focus();
			return false;
		}
	} else {
		window.alert("Por favor, introduzca su nombre de usuario.");
		document.frmRegister.username.focus();
		return false;
		//event.returnValue=false;	
	}

}

function checkFrmUpload() {
	var file = document.frmUpload.file.value;
	var title = document.frmUpload.title.value;
	var desc = document.frmUpload.description.value;
	
	if(file != ""){
		if(title != ""){
			if(desc != ""){
				return true;
				document.frmUpload.submit.disabled = true;
			} else {
				window.alert("Debe ingresar alguna descripcion.");
				document.frmUpload.description.focus();
				document.frmUpload.submit.disabled = false;
				return false;
			}
		} else {
			window.alert("Debe ingresar algun titulo.");
			document.frmUpload.title.focus();
			document.frmUpload.submit.disabled = false;
			return false;
		}
	} else {
		window.alert("Debe seleccionar algun archivo.");
		document.frmUpload.file.focus();
		document.frmUpload.submit.disabled = false;
		return false;
	}
}

function checkFrmComment() {
	var comment = document.frmComment.comment.value;
	
	if(comment != ""){
		return true;
		document.frmComment.submit.disabled = true;
	} else {
		window.alert("Debe ingresar algun texto.");
		document.frmComment.comment.focus();
		document.frmComment.submit.disabled = false;
		return false;
	}
}

function frmDoComment() {
	var comment = document.frmComment.comment.value;
	var u_id = document.frmComment.u_id.value;
	var u_c_username = document.frmComment.u_c_username.value;
	var p_id = document.frmComment.p_id.value;
	var qtycomments = document.frmComment.qtycomments.value;
	var page_username = document.frmComment.page_username.value;
	
	if(comment != ""){
		if(u_id != ""){
			if(u_c_username != ""){
				if(p_id != ""){
                    document.frmComment.submit.disabled = true;
                    $.post(host_address + 'inc/doComment.php',
                            {comment: comment , 
                                u_id: u_id,
                                u_c_username: u_c_username,
                                p_id: p_id,
                                qtycomments: qtycomments,
                                page_username: page_username
                            },
                            function(msg){
                                $("#msgs").html(msg);
                                document.frmComment.submit.disabled = false;
                                document.frmComment.comment.value = "";
                            }
                        );

					/*divisory = 'msgs'; //declared en ajax.js
					var poststr = "comment=" + encodeURIComponent(comment) + 
								"&u_id=" + encodeURIComponent(u_id) +
								"&u_c_username=" + encodeURIComponent(u_c_username) +
								"&p_id=" + encodeURIComponent(p_id) +
								"&qtycomments=" + encodeURIComponent(qtycomments) +
								"&page_username=" + encodeURIComponent(page_username);
								document.frmComment.submit.disabled = true;
					if(hacerPOSTRequest(host_address + 'inc/doComment.php', poststr)){
						document.frmComment.submit.disabled = false;
						document.frmComment.comment.value = "";
					}*/
				}
			}
		}
	}
}

//to open any file with AJAX
function Abrir_ajax(file, where){
	if(receiveReq.readyState ==  4 || receiveReq.readyState == 0){
		receiveReq.open("GET", file, true);
		divisory=where; //declared in ajax.js
		receiveReq.onreadystatechange = handle;
		receiveReq.send(null);
	}
}

//-----PopUp-----
function Abrir_ventana(pagina, ancho, alto, cambio_tam){
	if(ancho == null){
		ancho = 800;	
	}
	if(alto == null){
		alto = 530;
	}
	if(cambio_tam == null){
		cambio_tam = 'no'	
	}
	var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable="+cambio_tam+", width="+ancho+", height="+alto+", top=85, left=140";
	window.open(pagina,"",opciones);
}

//-----Register-----
function new_captcha(){
	document.frmRegister.img_captcha.src = './inc/captcha.php';
}

//-----Functions-----
function eraseCmnt(cmntID, u_id, page_username){
	$.post(host_address + 'inc/doErase.php',
		{
			cmntID: cmntID,
			u_id: u_id,
			page_username: page_username
		},
		function(msg){
			$("#msgs").html(msg);
		}
    	);
}

function checkDate(day, month, year){
	var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	if (!day || !month || !year)
		return false;

	if ((year % 4) == 0)
		monthLength[1] = 29;

	if (day > monthLength[month-1]){
		return false;
	} else {
		return true;
	}
}

function onlyNumbers(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}

function isValidEmail(str){
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

//for correct displaying of PNG on IE 5-6
function PNGie_background(){
	if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){
		for(var i=root.all.length-1,obj=null;(obj=root.all[i]);i--){
			if(obj.currentStyle.backgroundImage.match(/\.png/i)!==null){
				var mode='scale';
				var bg=obj.currentStyle.backgroundImage;
				var src=bg.substring(5,bg.length-2);
				if(obj.currentStyle.backgroundRepeat=='no-repeat'){
					mode='crop';
				}
				obj.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='"+mode+"')";
			}
		}		
	}
}

//PNGie_background();

function checkLength(txtArea, maxlength){
	var n = txtArea.value.length;
	if (n>=maxlength){
		txtArea.value = txtArea.value.substring(0, maxlength);
		return false;
	}
}

function checkAndShowLength(txtArea, where, maxlength){
	var n = txtArea.value.length;
	var length = txtArea.value.length;
	where.innerHTML=maxlength-length;
	if (length >= maxlength){
		txtArea.value = txtArea.value.substr(0, maxlength);
		where.innerHTML = 0;
	}
}

/*-----Picture Scroller-----*/
scrollStep=4;
	
timerLeft="";
timerRight="";

function toLeft(id){
	document.getElementById(id).scrollLeft=0;
}
	
function scrollDivLeft(id){
	clearTimeout(timerRight); 
	document.getElementById(id).scrollLeft+=scrollStep;
	timerRight=setTimeout("scrollDivLeft('"+id+"')",10);
}
	
function scrollDivRight(id){
	clearTimeout(timerLeft);
	document.getElementById(id).scrollLeft-=scrollStep;
	timerLeft=setTimeout("scrollDivRight('"+id+"')",10);
}
	
function toRight(id){
	document.getElementById(id).scrollLeft=document.getElementById(id).scrollWidth;
}
	
function stopMe(){
	clearTimeout(timerRight);
	clearTimeout(timerLeft);
}