//var location = email && firstname &&; 

var validate_through;

function setFocus(){
	document.bookform.location_id.focus();
}

function validate(ele, msgDivId){
	if(ele.nodeName.toLowerCase()=='select'){
		if(ele.selectedIndex == 0) {
			showMsg('This value is required', msgDivId);
		}else{
			hide(msgDivId);
		}
	} 

	if(ele.nodeName.toLowerCase()=='input'){
		if(ele.value==''){
			showMsg('This value is required', msgDivId);
		}else{
			hide(msgDivId);
		}
	}
}

function getTime(){
	if(document.bookform.location_id.selectedIndex==0){
		return false;
	}else if(document.bookform.appday.value==''){
		return false;
	}else if(document.getElementById('dur').selectedIndex==0) {
		return false;
	} else {
		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var day = now.getDate();
		var hour = now.getHours();
		var minutes = now.getMinutes();
		// var sec = now.getSeconds();
		
		document.bookform.actionDatetime.value = year + "-" + month + "-" + day + " " + hour + ":" + minutes;
		document.bookform.action = "actions/select_day.php";
		submitFormData(document.bookform);
	}
}

function showMsg(msg, msgDivId, bool){
	document.getElementById(msgDivId).innerHTML = msg;
	if(typeof(bool)!='undefined' && !bool){//hide time
		var li = document.getElementById('time_li');
		li.innerHTML='';
	}
}

function hide(msgDivId){
	document.getElementById(msgDivId).innerHTML = '';
}

function createLable(){
	var li = document.getElementById('time_li');
	var lable = document.createElement('span');
	lable.innerHTML='Select a time: ';
	li.appendChild(lable);
}

function createTimeSelection(htmlText, leadtime){
	var li = document.getElementById('time_li');
	li.innerHTML = htmlText;
	hide('globalMsg');
	var apptimeSelect = document.getElementById('apptime');

	document.bookform.bookBtn.disabled='';
	if(apptimeSelect.options.length==0){
		showMsg('There are no more appointments available on this date. <br/> '
				+ 'Please be sure to book at least ' + leadtime + ' hours in advance. <br/> '
				+ 'Some days may be fully booked. <br/>'
				+ 'Sorry, there are no appointments available on Sundays. <br/>'
				+ 'Thank you!', 
				'globalMsg', false);
		document.bookform.bookBtn.disabled='disabled';
		document.getElementById('phone_number_li').style.display="none";
	}
}

function showContactInput(){
	document.getElementById('phone_number_li').style.display="";
}

function validate_email(field, msgId)
{
	with (field)
		{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			if(msgId)
			showMsg('Please enter valid email address.', msgId);
			return false;
		} else {
			if(msgId)
				hide(msgId);
			return true;
	
		}
	}

}



function validation(uuid){
	var msgDiv = document.getElementById('globalMsg');
	if(document.bookform.location_id.selectedIndex==0){
		msgDiv.innerHTML='Please select your location.';
		msgDiv.style.display='';
		return false;
	} else if(document.bookform.appday.value==''){
		msgDiv.innerHTML='Please select a day.';
		msgDiv.style.display='';
		return false;
	} else if(document.bookform.dur.selectedIndex==0) {
		msgDiv.innerHTML='Please select a service.';
		msgDiv.style.display='';
		return false;
	} else if(document.bookform.firstname.value==''){
		msgDiv.innerHTML='Please enter your first name.';
		msgDiv.style.display='';
		return false;
	} else if(document.bookform.lastname.value==''){
		msgDiv.innerHTML='Please enter your last name.';
		msgDiv.style.display='';
		return false;	
	} else if(!validate_email(document.bookform.email)){
		msgDiv.innerHTML='Please enter valid email address.';
		msgDiv.style.display='';
		return false;	
	} else if(document.bookform.contact.value==''){
		msgDiv.innerHTML='Please enter phone number, Skype name, or IM name.';
		msgDiv.style.display='';
		return false;	
	} else {
		//get location name
		var tzArr = document.getElementById('location_id').options;
		for(var i=0;i<tzArr.length;i++){
			if(tzArr[i].selected==true){
				document.bookform.location.value=tzArr[i].innerHTML;
				break;
			}
		}
		//get service name
		var serviceArr = document.getElementById('dur').options;
		for(var i=0;i<serviceArr.length; i++){
			if(serviceArr[i].selected==true){
				document.bookform.service.value=serviceArr[i].innerHTML;
				break;
			}
		}
		if(uuid)
			document.bookform.uuid.value=uuid;
		
		document.bookform.action = 'checkout.php';
		document.bookform.saveorder.value='ready';
	}
}





/*	This function is used on the user selecting a location

from the drop-down menu named "LOCATION_ID".  It disables

or enables and checks or unchecks the DST checkbox */



function activateDSTBox(location_id)

{

	switch (location_id)

	{

		case '0':

		case '1':

		case '2':

		case '3':

		case '6':

		case '9':

		case '12':

		case '13':

		case '15':

		case '17':

		case '21':

		case '25':

		case '26':

		case '32':

		case '36':

		case '38':

		case '40':

		case '42':

		case '44':

		case '46':

		case '48':

		case '49':

		case '50':

		case '52':

		case '53':

		case '54':

		case '55':

		case '57':

		case '59':

		case '60':

		case '61':

		case '62':

		case '63':

		case '66':

		case '67':

		case '69':

		case '72':

		case '74':

		case '75':

			document.bookform.isdst.disabled = true;

			document.bookform.isdst.checked = false;

			break;

		default:

			document.bookform.isdst.disabled = false;

			document.bookform.isdst.checked = true;

			break;

	}

	

	return true;

}

