$(document).ready(function(){
    //autocomplete school
    var data = "Bellevue College,Bellingham Technical College,Big Bend Community College,Cascadia Community College,Centralia College,Clark College,Clover Park Technical College,Columbia Basin College,Institute for Extended Learning,Spokane Community College,Spokane Falls Community College,Edmonds Community College,Everett Community College,Green River Community College,Highline Community College,Lake Washington Technical College,Lower Columbia College,Olympic College,Peninsula College,Pierce College,North Seattle Community College,Seattle Central Community College,Seattle Vocational Institute,South Seattle Community College,Shoreline Community College,South Puget Sound Community College,Tacoma Community College,Walla Walla Community College,Whatcom Community College,Yakima Valley Community College,Central Washington University,Eastern Washington University,The Evergreen State College,Troy University, Fort Lewis,University of Washington,Washington State University,Western Washington University,School of Visual Concepts,University of Puget Sound,Walla Walla University,Whitman College,Whitworth University,Antioch University,Argosy University/Seattle,The Art Institute of Seattle,Bastyr University,City University of Seattle,Cornish College of the Arts,DeVry University,DigiPen Institute of Technology,Faith Evangelical Lutheran Seminary,Gonzaga University,Heritage College,Northwest Theological Seminary,Northwest University,Pacific Lutheran University,St. Martin's University,Seattle Bible College,Seattle Pacific University,Seattle University,Matteo Ricci College,Trinity Lutheran College".split(",");
    $("#school").autocomplete(data);
});

//check special characters (English)
function specialChars(str) {
    for (var i = 0; i < str.length; i++) {
        if ( 
            ( str.charCodeAt(i) <= 64 && str.charCodeAt(i) != 32 && str.charCodeAt(i) != 45)  || 
            ( str.charCodeAt(i) >= 91 && str.charCodeAt(i) <= 96  ) ||
            ( str.charCodeAt(i) >= 123 && str.charCodeAt(i) <= 127  ) ||
            ( str.charCodeAt(i) >= 155 && str.charCodeAt(i) <= 159 ) ||
            (str.charCodeAt(i) >= 161 && str.charCodeAt(i) != 173 
            && str.charCodeAt(i) != 150 && str.charCodeAt(i) != 151)
        ) { return true; }
    }
    return false;
}

//check for valid Chinese
function checkChinese(str) {
    if ( str.length == 1 ) { return false; }
    for (var i = 0; i < str.length; i++) {
        if ( str.charCodeAt(i) <= 12353 || str.charCodeAt(i) >= 65071 ) { return false; }
    }
    return true;
}

//check for valid email
function checkEmail(str) {
    var a = str.indexOf("@");
    var b = str.indexOf(".", a);
    var c = str.indexOf(",");
    var d = str.indexOf(" ");
    var e = str.lastIndexOf(".") + 1;

    if ( (a > 0) && (b > (1+1)) && (c == -1) && (d == -1) && (str.length-e >= 2) && (str.length-e <= 3)) { 
        return true;
    } else {
        return false;
    }
}

//alert message
function message(obj, pos, str) {
    pos.html(str);
    obj.focus();
}

function clearError(obj, pos) {
    pos.html('');
}

function checkForm() {
    var isValid = true;
    var phone = true;
    
	//trims values
	$("#first-name").val(jQuery.trim($("#first-name").val()));
	$("#last-name").val(jQuery.trim($("#last-name").val()));
    $("#school").val(jQuery.trim($("#school").val()));
	$("#email").val(jQuery.trim($("#email").val()));
    $("#song-title").val(jQuery.trim($("#song-title").val()));
	$("#song-artist").val(jQuery.trim($("#song-artist").val()));
    
    //agree
    if ( !$("#agree").is(':checked') ) {
        message($("#agree"), $("#span-agree"), '&lt; !');
        isValid = false;
	} else { clearError($("#agree"), $("#span-agree")) }
    
    //song selection
 	if ( $("#song-artist").val().length < 1 ) {
		message($("#song-artist"), $("#span-song-artist"), '&lt; <span class="en">Please give the artist of your song.</span><span class="cn">請填上所選歌曲歌手。</span>');
		isValid = false;
	} else { clearError($("#song-artist"), $("#span-song-artist")) }   
    if ( $("#song-title").val().length < 1 ) {
		message($("#song-title"), $("#span-song-title"), '&lt; <span class="en">Please write the name of your song.</span><span class="cn">請填上所選歌曲名字。</span>');
		isValid = false;
	} else { clearError($("#song-title"), $("#span-song-title")) }
    
    //phone
    if ( $("#phone-4").val().length < 4 ) {
        message($("#phone-4"), $("#span-phone"), '&lt; <span class="en">Please enter your complete phone number.</span><span class="cn">請輸入你的電話號碼。</span>');
        phone = false;
    }
    if ( $("#phone-3").val().length < 3 ) {
        message($("#phone-3"), $("#span-phone"), '&lt; <span class="en">Please enter your complete phone number.</span><span class="cn">請輸入你的電話號碼。</span>');
        phone = false;
    }
    if ( $("#phone-area").val().length < 3 ) {
        message($("#phone-area"), $("#span-phone"), '&lt; <span class="en">Please enter your complete phone number.</span><span class="cn">請輸入你的電話號碼。</span>');
        phone = false;
    }
    if ( !/^-?\d+$/.test($("#phone-4").val()) && $("#phone-4").val().length > 0 ) {
        message($("#phone-4"), $("#span-phone"), '&lt; <span class="en">Please only put numbers for your phone number.</span><span class="cn">請填上數字。</span>');
        phone = false;
    }
    if ( !/^-?\d+$/.test($("#phone-3").val()) && $("#phone-3").val().length > 0) {
        message($("#phone-3"), $("#span-phone"), '&lt; <span class="en">Please only put numbers for your phone number.</span><span class="cn">請填上數字。</span>');
        phone = false;
    }
    if ( !/^-?\d+$/.test($("#phone-area").val()) && $("#phone-area").val().length > 0) {
        message($("#phone-area"), $("#span-phone"), '&lt; <span class="en">Please only put numbers for your phone number.</span><span class="cn">請填上數字。</span>');
        phone = false;
    }
    if (phone) {
        $("#span-phone").html('');
    } else {
        isValid = false;
    } 
    
    //email
    if ( $("#email").val().length < 1 ) {
        message($("#email"), $("#span-email"), '&lt; <span class="en">Please enter your email address.</span><span class="cn">請填上你的電郵。</span>');
        isValid = false;
    } else if ( !checkEmail($("#email").val()) ) {
        message($("#email"), $("#span-email"), '&lt; <span class="en">Please enter an exact email address.</span><span class="cn">請填上你的主要通知電郵。</span>');
        isValid = false;
    } else { clearError($("#email"), $("#span-email")) }
    
    //school
    if ( $("#school").val() == "" ) {
        message($("#school"), $("#span-school"), '&lt; <span class="en">Please specify your school.</span><span class="cn">請選擇你的大學。</span>');
        isValid = false;
    } else { clearError($("#school"), $("#span-school")) }
    
    //birthday
    if ( $("#day").val() == "" || $("#month").val() == "" || $("#year").val() == ""  ) {
        message($("#day"), $("#span-birthday"), '&lt; <span class="en">Please specify your date of birth.</span><span class="cn">請選擇你的出生日期。</span>');
        isValid = false;
    } else { clearError($("#birthday"), $("#span-birthday")) }
    
    //last name
    if ( $("#last-name").val().length < 1 ) {
        message($("#last-name"), $("#span-last-name"), '&lt; <span class="en">Please fill out your last name.</span><span class="cn">請填上你的英文姓。</span>');
        isValid = false;
    } else if ( specialChars($("#last-name").val()) ) {
        message($("#last-name"), $("#span-last-name"), '&lt; <span class="en">Your last name contains invalid characters.</span><span class="cn">你的姓有無效字元。</span>');
        isValid = false;
    } else { clearError($("#last-name"), $("#span-last-name")) }
    
    //first name
    if ( $("#first-name").val().length < 1 ) {
        message($("#first-name"), $("#span-first-name"), '&lt; <span class="en">Please fill out your first name.</span><span class="cn">請填上你的英文名。</span>');
        isValid = false;
    } else if ( specialChars($("#first-name").val()) ) {
        message($("#first-name"), $("#span-first-name"), '&lt; <span class="en">Your name contains invalid characters.</span><span class="cn">你的名有無效字元。</span>');
        isValid = false;
    } else { clearError($("#first-name"), $("#span-first-name")) }
    

    //isValid = false;
    if ( isValid ) {
        $("submit").disable = true;
    }
    
    en();
    
    return isValid;
}