$(document).ready(function(){
	$("ul#menu_list>li").mouseover(function(){
		$(this).children(".submenu").show();
    }).mouseout(function(){
		$(this).children(".submenu").hide();
    });
	$("table#course_info ul li a").click(function(){
		var lnk = $(this).attr("href");
		var id = lnk.substr(lnk.indexOf("=")+1, lnk.length);
		$.ajax({
			type:"GET",
			url:"ajax/getContent.php",
			data:"id="+id,
			success:function(xml){
				$("td#detail").html(xml);
			}
		});
		return false;
	});
});
function joinMailingList(){
	var data = "";
	var error = 0;
	$("#mailingform input")	.each(function(){
		if($(this).parent().prev().children(".mustfill").val()!=undefined && ($(this).val() =='' || !allValidCharsIncludingSpace($(this).val()))){
			$(this).parent().prev().css("color", "#f00");
			error++;
		}else{
			$(this).parent().prev().css("color", "#12175E");
			if($(this).attr('name') == 'txtEmail'){
				if(!isValidEmail($(this).val())){
					$(this).parent().prev().css("color", "#f00");
					error++;
				}else{
					$(this).parent().prev().css("color", "#12175E");
					data += $(this).attr('name')+'='+$(this).val()+"&";	
				}
			}else{
				data += $(this).attr('name')+'='+$(this).val()+"&";
			}
		}
	});
	if(error==0){
		$("#status").css("color", "#66cc33").html("Please wait while processing your request.");
		$.ajax({
			type:"POST",
			url:"ajax/mailing.php",
			data:data,
			success:function(xml){
				if(xml=='0'){
					$("#mailingbody").html('');
					$("#status").html("<p style='font-size:14px;'><strong>Thanks for joining our mailing list.</strong></p>");	
					setTimeout("tb_remove()", 2000);
				}else{
					$("#status").css("color", "#f00").html("We're unable to process your request at this time.<br /> Please come later and try again.");		
				}
			}
		});	
	}else{
		$("#status").css("color", "#f00").html("To join the mailing list, please provide all of the required information.");
	}
}
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;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	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 allValidCharsIncludingSpace(str) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_ ";
  for (var i=0; i < str.length; i++) {
    var letter = str.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}