// JavaScript Document

// http object
var http = AJAX(); 
var loading = ' <div class="indent-main"><div class="bg"><div style="margin:15px 0 25px 0; text-align:center;"><br><br><img src="images/loader.gif" /><br><br><br></div></div></div>';

// XMLHttpRequest function to initiate request object
function AJAX(){ 
	var http = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		http = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			http = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			http = false;
		}
	}
	@end @*/
	if (!http && typeof XMLHttpRequest!='undefined') {
		http = new XMLHttpRequest();
	}
	return http;
}

/* Handle Content Returned from HTTP Request*/
function handleProducts(){
	if(http.readyState == 4){ 
		var response = http.responseText;
		document.getElementById('content').innerHTML = response;
	}
}

// Get Content
function getContent(pNo){
	if(pNo != ""){
		document.getElementById('content').innerHTML = loading;
		http.open('get', 'content.php?page=' + pNo);
		http.onreadystatechange = handleProducts;
		http.send(null);
		//handleURL(pNo); // adds #page to url
	}
}

// Get Gallery
function getGallery(id){
	if(id == ""){ id = 8;} // 14 denotes the id of current first gallery
	
	document.getElementById('content').innerHTML = loading;
	http.open('get', 'gallery.php?gal_id=' + id);
	http.onreadystatechange = handleProducts;
	http.send(null);
	//handleURL('gallery'); // adds #page to url
	
}

// URL Handding...
function handleURL(page){
	var str = window.location.href;
	var index = str.indexOf('#');
	var temp = '#' + page;
	
	if(index < 0){ // if there is no '#' in the url, then add it
		window.location.href += temp;
	}
	else{ // else add '#' to the specific location
		window.location.href = str.substring(0,index) + temp;
	}
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
E-Mail Form Script
Created 7/22/06
by Ismael L. Ruiz
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Variables
var email_regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var name_regex = /^[a-zA-Z \.]+$/
var message_regex = /^[a-zA-Z0-9 _\.\-\n\r\?\!\$\,\;\:\"\'\)\(\^]+$/ 
var code_regex = /^[0-9A-Za-z]{6}$/

/* Handle Content Returned from HTTP Request*/
function handleEmail(){
	if(http.readyState == 4){ 
		var response = http.responseText;
		document.getElementById('msg').innerHTML = response;
		document.getElementById('email_form').style.display = 'none';
	}
}

/* Send email */
function sendEmail(){
	// declare variable to hold form object
	var myForm = document.forms.email_form;		
	// Hide message 
	showMessage(false);
	// disable button
	myForm.send.disabled = true;
	// Check form
	if(checkWholeForm(myForm)){
		// replace new line character
		var message = myForm.message.value;
		message = message.replace(/\n/g, "<br/>");
		// Send message
		http.open('get', 'send_email.php?name=' + myForm.name.value + '&email=' + myForm.email.value + '&message=' + message + '&code=' + myForm.code.value);
		http.onreadystatechange = handleEmail;
		http.send(null);
		// Let the user know the message was sent
		feedBack('Sending message...');
	}else{
		//feedBack('Please fill required fields!');
		myForm.send.disabled = false; // enable button
	}
}

// Check if the form has been filled
function checkWholeForm(theForm){
	var str = "";
	// check name
	if(isEmpty(theForm.name.value)){
		str += '<li>Name is required!</li>';
		theForm.name.className = 'error'; // highlight textbox
	}else if (name_regex.test(theForm.name.value) == false){
		str += '<li>Name contains invalid characters!</li>';
		theForm.name.className = 'error'; // highlight textbox
	}
	// check email
	if(isEmpty(theForm.email.value)){
		str += '<li>E-mail is required!</li>';
		theForm.email.className = 'error'; // highlight textbox
	}else if(email_regex.test(theForm.email.value) == false){
		str += '<li>E-mail is invalid!</li>';
		theForm.email.className = 'error'; // highlight textbox
	}
	// check message
	if(isEmpty(theForm.message.value)){
		str += '<li>Message is required!</li>';
		theForm.message.className = 'error'; // highlight textbox
	}else if (message_regex.test(theForm.message.value) == false) {
		str += '<li>Message contains invalid characters!</li>';
		theForm.message.className = 'error'; // highlight textbox
	}
	// check code
	if(isEmpty(theForm.code.value)){
		str += '<li>Verification Code is required!</li>';
		theForm.code.className = 'error'; // highlight textbox
	}else if(code_regex.test(theForm.code.value) == false){
		str += '<li>Code is not valid!</li>';
		theForm.code.className = 'error'; // highlight textbox
	}
	// check concatinated string
	if (str != "") {
	   feedBack(str); // show error message
	   return false; // form is not valid
	   // theForm.btn_send.disabled = false; // enable button
	}return true; // form is valid
}

// Make sure fields are not empty
function isEmpty(str){
	if(str.length == 0){
		return true; // string is empty
	}return false; // string is not empty
}

// Restore button
function restoreButton(){
	document.getElementById('email_form').style.display = 'block';
	document.forms.email_form.send.disabled = false;
	showMessage(false);
}

// show message
function showMessage(show){
	if(show){document.getElementById('msg').style.display="block";}
	else{document.getElementById('msg').style.display="none";}
}

// Send message to the user
function feedBack(str){
	// Show message div
	showMessage(true);
	// create message
	document.getElementById('msg').innerHTML = str;
}

// Remove invalid characters
function removeInvalid(val, ex) {
	var strPass = val.value;
	var strLength = strPass.length;
	var lchar = val.value.charAt((strLength) - 1);
	if(lchar.search(ex) == -1) {
		var tst = val.value.substring(0, (strLength) - 1);
		val.value = tst;
	}
}
