$(function(){
	if(UserAgent.isTouchDevice()){
		$('body').addClass('isTouchDevice');
	}
	else{
		$('body').addClass('nonTouchDevice');
	}
	if(UserAgent.isIphoneOS4()){
		$('body').addClass('isIphoneOS4');
	}
	else{
		$('body').addClass('nonIphoneOS4');
	}
});

var UserAgent = {
	isTouchDevice : function() {
		return ('ontouchstart' in window);
	}
	,isIphoneOS4 : function() {
		var ua = navigator.userAgent.toString();
		return (ua.indexOf('iPhone OS 4_') > -1);
	}	
}

/*** Utils ***/
function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

/*** Form ***/
function submitForm(theform) {
	if(!validateRequired(theform.name.value)){
		alert("Please enter your name");
		return false;
	}
	if(!validateRequired(theform.company.value)){
		alert("Please enter your company name");
		return false;
	}
	if(!validateTel(theform.phone.value)){
		alert("Please enter valid telephone number");
		return false;
	}
	if(!validateEmail(theform.email.value)){
		alert("Please enter valid email address");
		return false;
	}
	$.ajax({
			url: "http://m.hot-mob.com/enquiry.php",
			context: document.body,
			type: "post",
			data: ({
					type: theform.type.value,
					name: theform.name.value,
					company: theform.company.value,
					phone: theform.phone.value,
					email: theform.email.value
			}),
			success: function(data){
				if(data == "ok"){
					alert("We will contact you shortly, Thank you!");
					clearForm(theform);
				}
			},
			error: function(data) {
				alert("ERROR: Please try again later");
				clearForm(theform);
			}
	});
}

function validateRequired(val) {
	if(trim(val) != ""){
		return true;
	}
	return false;
}

function validateTel(val) {
	if(val.match(/^([235679]{1})([0-9]+)$/)){
		return true;
	}
	return false;
}

function validateEmail(val) {
	if(val.match(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)[.]([a-zA-Z]{2,4})$/)){
		return true;
	}
	return false;
}

function clearForm(theform) {
	$(theform).find("input").each(function() {$(this).val("")});
}

function subscribeForm() {
	var email = prompt("Please enter your email address");
	if(email != null && validateEmail(email)){
		$.ajax({
			url: "http://m.hot-mob.com/subscribe.php",
			context: document.body,
			type: "post",
			data: ({
					type: "Newsletter",
					name: email
			}),
			success: function(data){
				if(data == "ok"){
					alert("You will receive our next newsletter soon!");
					clearForm(theform);
				}
			},
			error: function(data) {
				alert("ERROR: Please try again later");
				clearForm(theform);
			}
		});
	}
}

