$(document).ready(function(){
    //autocomplete school
    var data = "Auburn High School,Auburn Mountainview High School,Auburn Riverside High School,West Auburn High SchoolBellevue High School,Interlake High School,International School,Newport High School,Robinswood High School,Sammamish High School,Enumclaw High School,Decatur High School,Federal Way High School,Harry S. Truman High School,Thomas Jefferson High School,Todd Beamer High School,Academy of Citizenship and Empowerment,Aviation High School,Evergreen High School,Global Connections High School,Highline High School,Mount Rainier High School,Odyssey — The Essential School,Kent-Meridian High School,Kentlake High School,Kentridge High School,Kentwood High School,Issaquah High School,Liberty Senior High School,Skyline High School,Tiger Mountain Community High,BEST High School,Eastlake High School,International Community School,Juanita High School,Lake Washington High School,Redmond High School,Mercer Island High School,Bothell High School,Inglemoor High School,Woodinville High School,Black River High School,Hazen High School,Lindbergh High School,Renton High School,Cedarcrest High School,Ballard High School,The Center School,Grover Cleveland High School,Chief Sealth High School,Franklin High School,Garfield High School,Nathan Hale High School,Ingraham High School,Lincoln High School,John Marshall Alternative School,Middle College High School,The Nova Project,Rainier Beach High School,Roosevelt High School,Seahawks Academy,South Lake High School,West Seattle High School,Shorecrest High School,Shorewood High School,Skykomish High School,Mount Si High School,Two Rivers School,Tahoma Senior High School,Maple Valley High School,Foster High School,Vashon Island High School,Auburn Adventist Academy,Bear Creek School,Bellevue Christian School,Bishop Blanchet High School,The Bush School,Cedar Park Christian School,Christian Faith School,Chrysalis School,Dartmoor School Bellevue,Dartmoor School Issaquah,Dartmoor School Woodinville,Eastside Catholic School,Eastside Preparatory School,Evergreen Lutheran High School,Forest Ridge School of the Sacred Heart,Holy Names Academy,John F Kennedy Memorial High School,Kent View Christian Senior High School,King's Schools,Lakeside School,Northwest School,Northwest Yeshiva High School,O'Dea High School,Overlake High School,Puget Sound Adventist Academy,St. Christopher Academy,Seattle Academy of Arts and Sciences,Seattle Christian Schools,Seattle Lutheran High School,Seattle Preparatory School,Seattle Urban Academy,Seattle Waldorf School,University Preparatory Academy,Washington Academy of Performing Arts,University of British Columbia,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,University of Washington Bothell,University of Washington North Sound,University of Washington Tacoma, 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('<span style="color:#00FF99">✔</span>');
}

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; <span class="en">Please check the box.</span><span class="cn">請選取空格以示確定。</span>');
        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">The email format is invalid.</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() == "0" || $("#month").val() == "0" || $("#year").val() == "0"  ) {
        message($("#day"), $("#span-birthday"), '&lt; <span class="en">Your date of birth is incomplete.</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;
    }

    //set language
    if ($.cookie("lang") == "cn") {
        cn();
    } else {
        en();
    }

    return isValid;
}
