// JavaScript Document
$(function(){
	var mainNavOn = $('#mainNav .wrap');
	var mainNavDl = $('#mainNav dl');
	var mainNavDd = $('#mainNav dl dd');
	mainNavOn.css('background-position', 'left -'+((mainNavId-1)*55)+'px');
	mainNavDd.hover(function(){
		mainNavDl.css('background-position', 'left -'+($(this).index()*55)+'px');
	}, function(){
		mainNavDl.css('background-position', 'left 60px');
	});
});

function icmsTestPalyer(id, filename, fileext) {
	var so = new SWFObject('player.swf', 'icmsPlayerArea', 281, 231, '9');
	so.addParam('allowfullscreen', 'true');
	so.addParam('allowscriptaccess', 'always');
	so.addParam('wmode', 'transparent');
	so.addVariable('autostart', 'false');
	so.addVariable('stretching', 'exactfit');
	so.addVariable('dock', 'false');
	so.addVariable('icons', 'false');
	so.addVariable('file', 'upload/video/' + filename + fileext);
	so.addVariable('image', 'upload/video/' + filename + '.jpg');
	so.write('testimonials_' + id);
}
function RefreshCaptcha() {
	$("img#captcha_img").attr('src', 'index.php?module=captcha&rnd=' + Math.random());
}

//form_id 1; thank you 38
//form_id 2; thank you 40
//form_id 3; thank you 39
function itsail_form_er(form_id) {
	//alert(form_id + $("#itsail_form_" + form_id).serialize());
	//return;
	if(form_id != 2) {
		var firstname = $("input[name='frm[First Name]']");
		if(firstname.val().length == 0) {
			alert('Please enter your First Name');
			firstname.focus();
			return false;
		}
		if(!(/^[A-Za-z]+$/.test(firstname.val()))) {
			alert('Please enter letters only in First Name');
			return false;
			firstname.focus();
		}

		var lastname = $("input[name='frm[Last Name]']");
		if(lastname.val().length == 0) {
			alert('Please enter your Last Name');
			lastname.focus();
			return false;
		}
		if(!(/^[A-Za-z]+$/.test(lastname.val()))) {
			alert('Please enter letters only in Last Name');
			lastname.focus();
			return false;
		}

		var phone1 = $("input[name='frm[Phone Part 1]']");
		if(phone1.val().length !=3) {
			alert('Please enter your phone part one with 3 digits');
			phone1.focus();
			return false;
		}
		if(!(/^[0-9/.-]{3}$/.test(phone1.val()))) {
			alert('Please enter number with 3 digits');
			phone1.focus();
			return false;
		}

		var phone2 = $("input[name='frm[Phone Part 2]']");
		if(phone2.val().length != 3) {
			alert('Please enter your phone part two with 3 digits');
			phone2.focus();
			return false;
		}
		if(!(/^[0-9/.-]{3}$/.test(phone2.val()))) {
			alert('Please enter number with 3 digits');
			phone2.focus();
			return false;
		}

		var phone3 = $("input[name='frm[Phone Part 3]']");
		if(phone3.val().length != 4) {
			alert('Please enter your phone part three with 4 digits');
			phone3.focus();
			return false;
		}
		if(!(/^[0-9/.-]{4}$/.test(phone3.val()))) {
			alert('Please enter number with 4 digits');
			phone3.focus();
			return false;
		}

		var email = $("input[name='frm[Email]']");
		if(email.val().length == 0) {
			alert('Please enter your email');
			email.focus();
			return false;
		}
		var myReg = /^([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
		if(!(myReg.test(email.val()))) {
			alert('Please enter a valid email address');
			email.focus();
			return false;
		}

		/*var zip = $("input[name='frm[Zip]']");
		if(zip.val().length < 5) {
			alert('Please enter the correctly zip code with minimum of 5 characters');
			zip.focus();
			return false;
		}*/

		var captcha = $("input[name='frm[captcha]']");
		if(captcha.val().length != 4) {
			RefreshCaptcha();
			alert('Please enter the correctly security captcha');
			captcha.focus();
			return false;
		}
	}
	
	var er_form_img = $("#er_form_img");
	er_form_img.attr("src", "image/action_loading.gif");
	$.post('index.php', $("#itsail_form_" + form_id).serialize(), function(data){
		if(data.substring(0, 8) == 'success:') {
			er_form_img.attr("src", "image/action_accept.gif");
			if(form_id == 1) {
				window.location.href = "index.php?itemid=38";
			} else if(form_id == 2) {
				window.location.href = "index.php?itemid=40";
			} else if(form_id == 3) {
				window.location.href = "index.php?itemid=39";
			}
		} else {
			er_form_img.attr("src", "image/action_stop.gif");
			alert(data);
		}
	}, 'text');
}

function blog_submit(id, itemid) {
	var blog_captcha = $('#blog_captcha').val();
	if(blog_captcha.length != 4) {
		alert('Please input security code');
		return false;
	}

	var blog_username = $('#blog_username').val();
	if(blog_username.length == 0) {
		alert('Please input your name');
		return false;
	}

	var blog_comment = $('#blog_comment').val();
	if(blog_comment.length == 0) {
		alert('Please input your comment');
		return false;
	}

	var str = 'action=comment&inajax=1&&id=' + id + '&itemid=' + itemid + '&blog_captcha=' + blog_captcha + '&blog_comment=' + blog_comment + '&blog_username=' + blog_username;
	$.post('index.php', str, function(data){
		//alert(data);
		if(data.substring(0, 6) == 'error:') {
			alert(data);
		} else {
			$('#blog_comment_list').html(data + $('#blog_comment_list').html());
			alert('Your comment has been added.');
		}
	});
}
