function checkEmail()
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.form1.email.value))
    {
        return (true);
    }
}

function isValidEmail(str) {
    return (str.indexOf(".") > 0) && (str.indexOf("@") > 0);
}

function validate_num(){
    var digits="0123456789- ";
    var temp;

    for (var i=0;i<document.form1.tel_c.value.length;i++){
        temp=document.form1.tel_c.value.substring(i,i+1);
        if (digits.indexOf(temp)==-1){
            alert("錯誤輸入電話國家號碼！");
            return (false);
        }
    }
	
    for (var i=0;i<document.form1.tel_n.value.length;i++){
        temp=document.form1.tel_n.value.substring(i,i+1);
        if (digits.indexOf(temp)==-1){
            alert("錯誤輸入電話號碼！");
            return (false);
        }
    }
	
    for (var i=0;i<document.form1.fax_c.value.length;i++){
        temp=document.form1.fax_c.value.substring(i,i+1);
        if (digits.indexOf(temp)==-1){
            alert("錯誤輸入傳真國家號碼！");
            return (false);
        }
    }
	   
    for (var i=0;i<document.form1.fax_n.value.length;i++){
        temp=document.form1.fax_n.value.substring(i,i+1);
        if (digits.indexOf(temp)==-1){
            alert("錯誤輸入傳真號碼！");
            return (false);
        }
    }
   
    return (true);
}

function CheckRadio(ThisRadio) {
    selected = false;
    for (i=0; i<ThisRadio.length; i++) {
        if (ThisRadio[i].checked)
            selected = true;
    }
    return selected;
}

function CheckQuestion(i, NoOfQuestion) {
    //NoOfQuestion
    ThisChecked = false;
    k = i+NoOfQuestion;
    for (i=i; i<k; i++) {
        if (document.form1.elements[i].checked) {
            ThisChecked = true;
        }
    }
    return ThisChecked;
}
function compulsory_index() {
    missinginfo = "";
    if (document.form1.firstname.value == "") {
        missinginfo += "\n     -  名字";
    }
    if (document.form1.lastname.value == "") {
        missinginfo += "\n     -  姓氏";
    }
    if (document.form1.email.value == "") {
        missinginfo += "\n     -  電郵";
    }
    if (missinginfo != "") {
        missinginfo ="_____________________________________________________\n" +
        "錯誤輸入：\n" +
        missinginfo + "\n_____________________________________________________" +
        "\n請重新輸入！";
        alert(missinginfo);
        return false;
    } else {
        if (checkEmail()) {
            return true;
        } else {
            alert("錯誤電郵！請重新輸入．");
            return false;
        }
    }
}

function compulsory(noQuestion){
    missinginfo = "";
    if (!CheckRadio(document.form1.salutation)) {
        missinginfo += "\n     -  稱謂";
    }
	
    if (document.form1.job_title.value == "") {
        missinginfo += "\n     -  職銜";
    }
	
    if (document.form1.company.value == "") {
        missinginfo += "\n     -  公司名稱";
    }

    if (document.form1.address1.value == "") {
        missinginfo += "\n     -  地址";
    }

    if(!(document.form1.country.value=='HK'||document.form1.country.value=='MO')){

        if (document.form1.city.value == "") {
            missinginfo += "\n     -  城市";
        }

        if (document.form1.zip.value == "") {
            missinginfo += "\n     -  郵遞區號";
        }
        
    }
	
    if (document.form1.country.value == "0") {
        missinginfo += "\n     -  國家";
    }
    
    if(!arguments[0]) // for no question
    {
        i = 0;
        /*
	while (document.form1.elements[i].name != "product_category_01") {
		i++;
	}
	product_category = i;

	while (document.form1.elements[i].name != "business_nature_01") {
		i++;
	}
	business_nature = i;

	while (document.form1.elements[i].name != "q2_01") {
		i++;
	}
	question2 = i;
*/
        while (document.form1.elements[i].name != "q3_01") {
            i++;
        }
        question3 = i;

        while (document.form1.elements[i].name != "q4_01") {
            i++;
        }
        question4 = i;

        /*
	if (!CheckQuestion(product_category, 8)) {
		missinginfo += "\n     -  Product Category";
	} else {
		if (document.form1.product_category_08.checked && document.form1.product_category_08_other.value == "") {
			missinginfo += "\n     -  Please Specify in Product Category - Others";
		}
	}

	if (!CheckQuestion(business_nature, 8)) {
		missinginfo += "\n     -  Business Nature";
	} else {
		if (document.form1.business_nature_08.checked && document.form1.business_nature_08_other.value == "") {
			missinginfo += "\n     -  Please Specify in Business Nature - Others";
		}
	}
*/
        if (!CheckRadio(document.form1.q1)) {
            missinginfo += "\n     -  問題一";
        } else {
            if (document.form1.q1_44.checked && document.form1.q1_other.value == "") {
                missinginfo += "\n     -  請列明問題一 業務性質 - 其他";
            }
        }

        if (!CheckRadio(document.form1.q2)) {
            missinginfo += "\n     -  問題二";
        }

        if (!CheckRadio(document.form1.q3)) {
            missinginfo += "\n     -  問題三";
        }
	
        if (document.form1.q3[0].checked) {
            if (!CheckQuestion(question3, 12)) {
                missinginfo += "\n     -  問題三";
            }
            if (document.form1.q3_12.checked && document.form1.q3_12_other.value == "") {
                missinginfo += "\n     -  請列明問題三 - 其他";
            }
            if (!CheckRadio(document.form1.q8)) {
                missinginfo += "\n     -  問題 3.1";
            }else{
                if (document.form1.q8_05.checked && document.form1.q8_other.value == "") {
                    missinginfo += "\n     -  請列明問題3.1 - 其他";
                }
            }
        }

        if (!CheckQuestion(question4, 10)) {
            missinginfo += "\n     -  問題四";
        } else {
            if (document.form1.q4_04.checked && document.form1.q4_04_other.value == "") {
                missinginfo += "\n     -  請列明問題四 - 廣告";
            }
            if (document.form1.q4_07.checked && document.form1.q4_07_other.value == "") {
                missinginfo += "\n     -  請列明問題四 - 新聞媒體";
            }
            if (document.form1.q4_08.checked && document.form1.q4_08_other.value == "") {
                missinginfo += "\n     -  請列明問題四 - 其他";
            }
        }

        if (!CheckRadio(document.form1.q5)) {
            missinginfo += "\n     -  問題五";
        } else {
            if (document.form1.q5[1].checked && document.form1.q5_yes.value == "") {
                missinginfo += "\n     -  請列明問題五 - 次數";
            }
        }
   
        if (!CheckRadio(document.form1.q6)) {
            missinginfo += "\n     -  問題六";
        }
    }
    if (missinginfo != "")
    {
        //alert(missinginfo.substring(0,35));
        //alert(missinginfo.substring(0,17));
        if (missinginfo.substring(0, 35) == "\n     -  請填寫" || missinginfo.substring(0, 17) == "\n     -  問題" ) {
            missinginfo ="________________________________________________________\n" +
            "錯誤輸入：\n" +
            missinginfo + "\n_____________________________________________________" +
            "\n請重新輸入！";
        }
        else {
            missinginfo ="_____________________________________________________\n" +
            "錯誤輸入：\n" +
            missinginfo + "\n_____________________________________________________" +
            "\n請重新輸入！";
        }
        alert(missinginfo);
        return false;
    }
	
    else{
        return validate_num();
    }

}
