function jgo(thelocation) {
	document.location.href = thelocation;
}

function doInvoice(workshopId) {
	window.open("invoice.asp?workshopId="+workshopId, "emailpopup", "width=650,height=650,toolbar=1,scrollbars=0")
}

function imgpopup(photo_id,workshopId) {
	window.open("photo.asp?photo_id="+photo_id+"&workshopId="+workshopId, "photopopup", "width=570,height=620,toolbar=0,scrollbars=0")
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    }
	
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_'";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function applyValidate() {
	if (document.sendform.name.value == '' || document.sendform.email.value == '') {
		alert("You must fill in the mandatory fields.");
		return false;
	}else{
		if (! isValidEmail(document.sendform.email.value)) {
        	alert("Please enter a valid email address");
        	return false;
    	}else{
			document.sendform.submitbut.value = 'Please wait...'
			return true;
		}
	}
}

function bookValidate() {
	if (document.bookingform.name.value == '' || document.bookingform.email.value == '') {
		alert("Please fill in your name and email address");
		return false;
	}else{
		if (! isValidEmail(document.bookingform.email.value)) {
        	alert("Please enter a valid email address");
        	return false;
    	}else{
			document.bookingform.submitbut.value = 'Please wait...'
			return true;
		}
	}
}

function holVal() {
	if (document.editform.hol_startdate.value == '' || document.editform.hol_enddate.value == '' || document.editform.hol_reason.value == '') {
		alert("Please fill in the date fields and 'reason' field.");
		return false;
	}else if(document.editform.hol_daysofweek[0].checked == false && document.editform.hol_daysofweek[1].checked == false && document.editform.hol_daysofweek[2].checked == false && document.editform.hol_daysofweek[3].checked == false && document.editform.hol_daysofweek[4].checked == false && document.editform.hol_daysofweek[5].checked == false && document.editform.hol_daysofweek[6].checked == false){
		alert("Please select days of the week this applies to.");
		return false;
	}else{
		return true;
	}
}

function musicPopup(id) {
	window.open("musicpopup_"+id+".html", "musicpopup", "width=295,height=401,toolbar=0,scrollbars=0")
}

function checkInvoice() {
	if (document.editform.address1.value == '' || document.editform.towncity.value == '' || document.editform.postcode.value == '' || document.editform.feedback_daygo.value == '' || document.editform.feedback_ontime.value == '' || document.editform.feedback_incidents.value == '' || document.editform.feedback_equipment.value == '') {
		alert("Please fill in all the required fields");
		return false;
	}else{
		//check max tickboxes
		var total = 0;
		var max = document.editform.workshops.length;
		for (var idx = 0; idx < max; idx++) {
		if (eval("document.editform.workshops[" + idx + "].checked") == true) {
		    total += 1;
		   }
		}
		
		if(total > 10) {
			alert("Only 10 workshops can be put on one invoice. Please de-select some.")
			return false;
		}else{
			return true;
		}
	}
}




function invoiceWorkshopChange() {
	var workshops = document.getElementsByName("workshops");
	var workshopstring;
	workshopstring = "";
	for(i=0; i<workshops.length; i++) {
		if(workshops[i].checked == true) {
			workshopstring = workshopstring + document.getElementById("workshoptext_"+workshops[i].value).value + "\r";
		}
	}
	document.editform.feedback_ontime.value = workshopstring;
}

function daveWatch() {
	window.open("davewatch.asp", "dwpopup", "width=920,height=595,toolbar=1,scrollbars=0")
}

function salesCalendar() {
	window.open("calendar.asp", "calpopup", "width=920,height=625,toolbar=1,scrollbars=1")
}

function getInfo(appId) {
	window.open('/sales/popinfo.asp?appId='+appId, 'apppopup', 'width=370,height=430,toolbar=0,scrollbars=1')
}
