/*
 * @filename global.js
 * @author Sothea Nim (info@sotheanim.com)
 * @date July 2009
 * @description the js contained in this file is used for sotheanim.com
 */

$(document).ready(function() {
	$("input").attr('autocomplete','off');
	/*tabs('snimtabs', 'maincontent', 'css-tabs', 'css-panes');*/
	$('#captchaStatus').hide();
	$('#msg').hide();
	$('label.required').append('<strong class="required">*</strong>');
	form_validator();
	inputFocus();
});/* COOKIE */
function createCookie(name, value, days, cpath)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	cpath = (cpath) ? cpath : '/';
	document.cookie = name+"="+value+expires+"; path="+cpath;
}
/* get cookie */
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
/* erase cookie */
function eraseCookie(name)
{
	createCookie(name, "", -1);
}/* switch search pane on demand */
function tabs(cName, container, tab, panes)
{
	var first_id = $('.'+container+' .'+panes+' div').attr('id');
	hidepane(readCookie(cName), first_id, container, panes, tab);
	setActiveTab(readCookie(cName), first_id, container, tab);
	$('.'+container+' .'+tab+' a').click(function() {
		var thisid = $(this).attr('href');
		thisid = thisid.substr(1, thisid.length);
		createCookie(cName, thisid, 365);
		setActiveTab(readCookie(cName), thisid, container, tab);
		showpane(thisid, container, panes);
		return false;
	}).next().hide();
}

/* set active class name */
function setActiveTab(cVal, first_id, container, tab) {
	if( !cVal ) {
		$('.'+container+' .'+tab+' a').each(function(n, i) {
			tmpid = $(this).attr('href');
			tmpid = tmpid.substr(1, tmpid.length);
			if( tmpid===first_id) {
				var classname = $(this).attr('class');
				if( !classname.match('current') ) {
					$(this).addClass('current');
				}
			}
		});
	} else {
		$('.'+container+' .'+tab+' a').each(function(n, i) {
			tmpid = $(this).attr('href');
			tmpid = tmpid.substr(1, tmpid.length);
			if( tmpid===cVal ) {
				var classname = $(this).attr('class');
				if( !classname.match('current') ) {
					$(this).addClass('current');
				}
			} else {
				$(this).removeClass('current');
			}
		});
	}
}

/* show panel */
function showpane(id, container, panes) {
	$('.'+container+' .'+panes).children().each(function(n, i) {
  	if( this.id===id ) {
  		$('#'+this.id).show();
  	} else {
  		$('#'+this.id).hide();
  	}
	});
}

/* hide panel if no cookie, but show the first tab pane */
function hidepane(cVal, first_id, container, panes, tab) {
	if( cVal )
	{
		var i=false;
		$('.'+container+' .'+panes).children().each(function(n, i) {
			if( this.id===cVal ) {
				$('#'+this.id).show(); i=true;
			} else {
				$('#'+this.id).hide();
			}
		});
	}
	else
	{
		$('.'+container+' .'+panes).children().each(function(n, i) {
			if( this.id===first_id ) {
				$('#'+this.id).show();
			} else {
				$('#'+this.id).hide();
			}
		});
	}
}
/* remember last tab */
function rememberTab() {
	$('.css-tabs a').click(function(){
		thisid = $(this).attr('href');
		createCookie('snimtab', thisid, 365);
	});
}var invalid_field = 'invalid';
var required_field = 'required_field';

function form_validator()
{
	var str='';
	var found_invalid = false;
	
	$('form').submit(function() {
		var formid = $(this).attr('id');
		$('form#'+formid+' .'+required_field).each(function(n, i) {
			
			val = $(this).val();
			classname = $(this).attr('class');
			fieldname = $(this).attr('name');
			
			if( val.length===0 )
			{
				$(this).addClass(invalid_field);
				found_invalid=false;
				return false;
			}
			else if (fieldname.match('email'))
			{
				if( !isValidEmailAddress(val) )
				{
					$(this).addClass(invalid_field);
					found_invalid=false;
					return false;
				}
				else
				{
					str += fieldname+'='+val+'&';
					found_invalid=true;
				}
			}
			else
			{
				str += fieldname+'='+val+'&';
				found_invalid=true;
			}
		});
		if(found_invalid)
		{
			if( !validateCaptcha() )
			{
				str='';
				found_invalid=false;
				return false;
			}
			sendmail(str);
			clearForm(formid);
			return false;
		}
		return false;
	});
}

function validateCaptcha()
{
	$('#captchaStatus').show();
	var ajaxcaptcha = 'sotheanim-lib/recaptcha/ajaxcaptcha.php';
	challengeField = $("input#recaptcha_challenge_field").val();
	responseField = $("input#recaptcha_response_field").val();
	data = "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField;
	var html = jqueryajax(ajaxcaptcha, data);
	if(html == 'success') {
		$("#captchaStatus").html("success");
		return true;
	} else {
		$("#captchaStatus").html("Your captcha is incorrect (ERROR: "+html+"). Please try again");
		Recaptcha.reload();
		return false;
	}
}

function isValidEmailAddress(emailAddress)
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	if( pattern.test(emailAddress) )
	{
		var seg = emailAddress.split('@');
		var ajaxcaptcha = 'sotheanim-lib/urls/checkurl.php';
		var data = 'url='+seg[1];
		var result = jqueryajax(ajaxcaptcha, data);
		return ( result==1 ) ? true : false;
	}
	else
	{
		return false;
	}
}

function inputFocus()
{
	$('.'+required_field).focus(function() {
		classname = $(this).attr('class');
		val = $(this).val();
		fieldname = $(this).attr('name');
		if( classname.match(invalid_field) ) {
			$(this).removeClass(invalid_field);
		}
	});
	$('.'+required_field).blur(function(){
		val = $(this).val();
		if( val.length===0 ) $(this).addClass(invalid_field);
		else if( fieldname.match('email') ) {
			if( !isValidEmailAddress(val) ) {
				$(this).addClass(invalid_field);
			}
		}
	});
}

function clearForm(id)
{
	$('form#'+id).each(function() {
		var thistype = this.type;
		var thistag = this.tagName.toLowerCase();
		if( thistype==='text' || thistype==='password' || thistag === 'textarea' ) this.value='';
		else if (type == 'checkbox' || type == 'radio') this.checked = false;
		else if( thistag === 'select') this.selectedIndex = -1;
	});
}function jqueryajax(myurl, mydata)
{
	var html = $.ajax({
		type: "POST",
		url: myurl,
		data: mydata,
		async: false
	}).responseText;
	return html;
}

function sendmail(str)
{
	var success = 'Thank you. I will get in touch with you shortly';
	var fail = 'Sorry, but there was an error!';
	myurl = 'sotheanim-lib/send/mail.php';
	var result = jqueryajax(myurl, str);
	$('#msg').show();
	if( result==1 )
	{
		$('#msg').html(success);
	}
	else
	{
		$('#msg').html(fail);
	}
}