

$(function(){
	function setCookie(c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}
	function getCookie(c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}

	bgMusic();
	var is_bgMusic = getCookie('is_bg_music');
	if(is_bgMusic.length == 0) {
		jwplayer('bgmusic').play();
	} else {
		$('.bgsound').addClass('bgsoundno');
	}

	$('.bgsound .on').click(function(){
		$(this).parent().removeClass('bgsoundno');
		//if($('#bgmusic').html().length == 0) bgMusic();
		jwplayer('bgmusic').play();
		setCookie('is_bg_music', '', 365);
	});
	$('.bgsound .off').click(function(){
		$(this).parent().addClass('bgsoundno');
		jwplayer('bgmusic').stop();
		setCookie('is_bg_music', 'yes', 365);
		//$('#bgmusic').html('').hide();
	});

	$('#sidenav ul li').click(function(){
		window.location.href = $('a', $(this)).attr('href');
		return false;
	});
});

function bgMusic() {
	jwplayer('bgmusic').setup({
	'flashplayer': 'player.swf',
	'duration': '33',
	'file': 'background_music.mp3',
	'controlbar': 'none',
	'dock': 'false',
	'autostart': 'false',
	'icons': 'false',
	'quality': 'false',
	'repeat': 'always',
	'width': '1',
	'height': '1'
	});
}

function RefreshCaptcha() {
	$("img#captcha_img").attr('src', 'index.php?module=captcha&rnd=' + Math.random());
}

function itsail_form_er(form_id) {
	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 Address = $("input[name='frm[Address]']");
	if(Address.val().length == 0) {
		alert('Please enter your Address');
		Address.focus();
		return false;
	}

	var City = $("input[name='frm[City]']");
	if(City.val().length == 0) {
		alert('Please enter your City');
		City.focus();
		return false;
	}

	var State = $("select[name='frm[State]']");
	if(State.val().length == 0) {
		alert('Please enter your State');
		return false;
	}

	var Zip = $("input[name='frm[Zip]']");
	if(Zip.val().length == 0) {
		alert('Please enter your Zip/Postal Code');
		Zip.focus();
		return false;
	}
	/*var myReg = /^([0-9]{5,})$/;
	if(!(myReg.test(Zip.val()))) {
	alert('Please enter a valid Zip Code');
	Zip.focus();
	return false;
	}*/

	var Phone1 = $("input[name='frm[Phone]']");
	if(Phone1.val().length != 10) {
		alert('Please enter the correctly phone number with 10 digits');
		Phone1.focus();
		return false;
	}

	var BestTime = $("select[name='frm[Best Time To Call]']");
	if(BestTime.val().length == 0) {
		alert('Please select Best Time To Call');
		Hear.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 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");
			//window.location.hash = "top";
			//window.location.href = "index.php?itemid=21";
			if($("input[name='thankyou']").length > 0) {
				window.location.href = $("input[name='thankyou']").val();
			} else {
				alert('The form submited successful');
			}
		} else {
			er_form_img.attr("src", "image/action_stop.gif");
			alert(data);
		}
	}, 'text');

	return false;
}

