// Recommend This to A Friend =================================================
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// 檢查Buyer 註冊資料表 =======================================================
function CheckRegisterFrm(frm, authnum, type) {
    if (type == "Company") {
        if (!checkNull(frm.t2_account.value)) {
            alert('Notice! The field of ID was still blank.');
            frm.t2_account.focus();
            return false;
        } else if (!checkLength(frm.t2_account.value,3,10)) {
            alert('The ID characters must be 3 ~ 10.');
			frm.t2_account.value = "";
            frm.t2_account.focus();
            return false;
        } else if (!checkPassword_2(frm.t2_account.value)) {
            alert('Your ID are not in correct format.');
			frm.t2_account.value = "";
            frm.t2_account.focus();
            return false;
        }
        if (!checkPassword_1(frm.t2_password.value, frm._password_.value)) {
            frm.t2_password.value = "";
            frm._password_.value = "";
            frm.t2_password.focus();
            return false;
        } else if (!checkLength(frm.t2_password.value, 6, 10)) {
            alert('The Password characters must be 6 ~ 10.');
            frm.t2_password.value = "";
            frm._password_.value = "";
            frm.t2_password.focus();
            return false;
        } else if (!checkPasswordRule(frm.t2_account, frm.t2_password)) {
            alert("Your Password entries did not match the correct format.");
            frm.t2_password.value = "";
            frm._password_.value = "";
            frm.t2_password.focus();
            return false;
        }
        if (!checkNull(frm.t2_first_name.value)) {
            alert('Notice! The field of First Name was still blank.');
            frm.t2_first_name.focus();
            return false;
        }
        if (!checkNull(frm.t2_last_name.value)) {
            alert('Notice! The field of Last Name was still blank.');
            frm.t2_last_name.focus();
            return false;
        }
        if (!checkNull(frm.t2_email.value)) {
            alert('Notice! The field of E-mail was still blank.');
            frm.t2_email.focus();
            return false;
        } else if (!checkEmail(frm.t2_email.value)) {    
            frm.t2_email.value = "";
            frm.t2_email.focus();
            return false;
        }
        if (!checkNull(frm.t2_company_name.value)) {
            alert('Notice! The field of Company Name was still blank.');
            frm.t2_company_name.focus();
            return false;
        }
/*2007-10-09 mark by Ayling -for 世宗
        if (!checkNull(frm.t2_url.value)) {
            alert('Notice! The field of URL was still blank.');
            frm.t2_url.focus();
            return false;
        }
*/
		if (!nullCheckBox(frm, 't5_company_type__id[]', 'Company Type')) {
            document.getElementById('_comtype').focus();
            return false;
		} 
        if (!checkSelect(frm.t2_country__id)) {
            alert('Notice! The field of Country must be selected.');
            frm.t2_country__id.focus();
            return false;
        }
        if (!checkNull(frm.t2_profile.value)) {
            alert('Notice! The field of Company Profile was still blank.');
            frm.t2_profile.focus();
            return false;
        }
		/*
        if (authnum != frm.authinput.value) {
			alert('Please enter the correct letters in the confirm text.');
			frm.authinput.value = "";
			frm.authinput.focus();
			return false;
        }
		*/
		// edit by wheat @2007-08-09
		if( frm.authinput.value == "" || (base64encode(utf16to8(frm.authinput.value)) != authnum) ) {
			alert('Please enter the correct letters in the confirm text.');
			frm.authinput.value = "";
			frm.authinput.focus();
			return false;
		}

		frm.t2_tel.value = frm._tel1_.value +'-'+ frm._tel2_.value +'-'+ frm._tel3_.value;
		frm.t2_fax.value = frm._fax1_.value +'-'+ frm._fax2_.value +'-'+ frm._fax3_.value;
    }
}

// add by livy @061005
function cutstr(str, len)	{
	return str.length>len?str.substring(0, len):str;
}

// 檢查Buyer 後台- 基本/進階資料維護 ==========================================
function CheckMyProfileFrm(frm, type) {

    if (type == "basic") {
        if (!checkNull(frm.t2_first_name.value)) {
            alert('Notice! The field of First Name was still blank.');
            frm.t2_first_name.focus();
            return false;
        }

        if (!checkNull(frm.t2_last_name.value)) {
            alert('Notice! The field of Last Name was still blank.');
            frm.t2_last_name.focus();
            return false;
        }

        if (!checkNull(frm.t2_company_name.value)) {
            alert('Notice! The field of Company Name was still blank.');
            frm.t2_company_name.focus();
            return false;
        }

		if (!nullCheckBox(frm, 't5_company_type__id[]', 'Company Type')) {
            return false;
		} 

        if (!checkSelect(frm.t2_country__id)) {
            alert('Notice! The field of Country must be selected.');
            frm.t2_country__id.focus();
            return false;
        }

        if (!checkNull(frm.t2_city.value)) {
            alert('Notice! The field of City was still blank.');
            frm.t2_city.focus();
            return false;
        }

        if (!checkNull(frm.t2_address.value)) {
            alert('Notice! The field of Address was still blank.');
            frm.t2_address.focus();
            return false;
        }
/*2007-10-11 mark by Ayling -for 世宗
        if (!checkNull(frm.t2_province_state.value)) {
            alert('Notice! The field of Province was still blank.');
            frm.t2_province_state.focus();
            return false;
        }

        if (!checkNull(frm.t2_zip_code.value)) {
            alert('Notice! The field of Zip Code was still blank.');
            frm.t2_zip_code.focus();
            return false;
        } else if (!checkNum(frm.t2_zip_code.value, 'Zip Code')) {
			frm.t2_zip_code.value = "";
            frm.t2_zip_code.focus();
            return false;
        }
*/
        if (!checkNull(frm._tel1_.value)) {
            alert('Notice! The field of Business Tel No. was still blank.');
            frm._tel1_.focus();
            return false;
        } else if (!checkNum(frm._tel1_.value, 'Tel No')) {
			frm._tel1_.value = "";
            frm._tel1_.focus();
            return false;
        }

        if (!checkNull(frm._tel2_.value)) {
            alert('Notice! The field of Business Tel No. was still blank.');
            frm._tel2_.focus();
            return false;
        } else if (!checkNum(frm._tel2_.value, 'Tel No')) {
			frm._tel2_.value = "";
            frm._tel2_.focus();
            return false;
        }

        if (!checkNull(frm._tel3_.value)) {
            alert('Notice! The field of Business Tel No. was still blank.');
            frm._tel3_.focus();
            return false;
        } else if (!checkNum(frm._tel3_.value, 'Tel No')) {
			frm._tel3_.value = "";
            frm._tel3_.focus();
            return false;
        }
        frm.t2_tel.value = frm._tel1_.value +'-'+ frm._tel2_.value +'-'+ frm._tel3_.value;

        /*if (!checkNull(frm._fax1_.value)) {
            alert('Notice! The field of Business Fax No. was still blank.');
            frm._fax1_.focus();
            return false;
        } else */if (!checkNum(frm._fax1_.value, 'Fax No')) {
			frm._fax1_.value = "";
            frm._fax1_.focus();
            return false;
        }

        /*if (!checkNull(frm._fax2_.value)) {
            alert('Notice! The field of Business Fax No. was still blank.');
            frm._fax2_.focus();
            return false;
        } else */if (!checkNum(frm._fax2_.value, 'Fax No')) {
			frm._fax2_.value = "";
            frm._fax2_.focus();
            return false;
        }

        /*if (!checkNull(frm._fax3_.value)) {
            alert('Notice! The field of Business Fax No. was still blank.');
            frm._fax3_.focus();
            return false;
        } else */if (!checkNum(frm._fax3_.value, 'Fax No')) {
			frm._fax3_.value = "";
            frm._fax3_.focus();
            return false;
        }
		if ((checkNull(frm._fax1_.value)) && (checkNull(frm._fax2_.value)) && (checkNull(frm._fax3_.value))) {
			frm.t2_fax.value = frm._fax1_.value +'-'+ frm._fax2_.value +'-'+ frm._fax3_.value;
		} else {
		    frm.t2_fax.value = " ";
		}

        if (!checkNum(frm.t2_mobile.value, 'Mobile No')) {
			frm.t2_mobile.value = "";
            frm.t2_mobile.focus();
            return false;
        }
/* 2007-10-09 mark by Ayling -for 世宗
		// add by livy @061005
		if (!checkNull(frm.t2_url.value)) {
			alert('Notice! The field of Company URL was still blank.');
			frm.t2_url.focus();
			return false;
		}
*/
/*
        if (!checkNull(frm.industry_category.value)) {
            alert('Notice! The field of Industry Category was still blank.');
            frm.industry_category.focus();
            return false;
        }
*/
        if (!checkNull(frm.t2_profile.value)) {
            alert('Notice! The field of Company Profile was still blank.');
            frm.t2_profile.focus();
            return false;
        }
		
		// add by livy @061005
		frm.t2_profile.value= cutstr(frm.t2_profile.value, 400);
		
    } else {
	}
}


// 檢查Buyer id 是否重覆 ======================================================
/*
function checkID (frm, url) {
	if (!checkNull(frm.t2_email.value)) {
		alert('Notice! The field of ID was still blank.');
		frm.t2_email.focus();
	} else if (!checkEmail(frm.t2_email.value)) {
        frm.t2_email.select();
		frm.t2_email.focus();
	} else {
	    window.open(url+'?mod=check_buyerId&checkid='+frm.t2_email.value,'Check','toolbar=no,width=250,height=150,directories=no,status=no,scrollbars=no,resize=no,menubar=no');
	}
}
*/
function checkID (frm, url) {
	if (!checkNull(frm.t2_account.value)) {
		alert('Notice! The field of ID was still blank.');
		frm.t2_account.focus();
	} else {
	    window.open(url+'?mod=check_buyerId&checkid='+frm.t2_account.value,'Check','toolbar=no,width=250,height=150,directories=no,status=no,scrollbars=no,resize=no,menubar=no');
	}
}


// 檢查Buyer login ============================================================
function CheckLoginFrm (frm) {
	if (!checkNull(frm.t2_account.value)) {
		alert('Notice! The field of ID was still blank.');
		frm.t2_account.focus();
		return false;
	}
	if (frm.t2_account.value=="(your id)") {
		alert('Notice! The field of ID was still blank.');
		frm.t2_account.focus();
		return false;
	}
	if (!checkNull(frm.t2_password.value)) {
		alert('Notice! The field of Password was still blank.');
		frm.t2_password.focus();
		return false;
	}
	return true;
}


// 檢查Buyer Change Password ==================================================
function CheckChangePwdFrm(frm) {

	if (!checkNull(frm.ori_password.value)) {
		alert('Notice! The field of Original Password was still blank.');
		frm.ori_password.focus();
		return false;
	}

	if (!checkPassword_1(frm.t2_password.value, frm._password_.value)) {
		frm.t2_password.value = "";
		frm._password_.value = "";
		frm.t2_password.focus();
		return false;
	} else if (!checkLength(frm.t2_password.value, 3, 10)) {
		alert('The Password characters must be 3 ~ 10');
		frm.t2_password.value = "";
		frm._password_.value = "";
		frm.t2_password.focus();
		return false;
	} else if (!checkPassword_2(frm.t2_password.value)) {
		alert("Your Password entries did not match the correct format.");
		frm.t2_password.value = "";
		frm._password_.value = "";
		frm.t2_password.focus();
		return false;
	}

	if (frm.old_password.value != frm.ori_password.value)	{
		alert("Your Original Password is error.");
		frm.ori_password.value = "";
		frm.ori_password.focus();
		return false;
	}

	return true;
}


function checkEmail(control) {

    ind = control.indexOf ('@', 0);
    if (control.length==0) {
        alert('Notice! The field of E-Mail was still blank.');
        return false;
    } else if (ind==-1) {
//        alert("錯誤：必須包含「@」。");
        alert("Your email address entries did not match the correct format.");
        return false;
    } else if (ind==0) {
//        alert("錯誤：「@」之前不可為空字串。");
        alert("Your email address entries did not match the correct format.");
        return false;
    } else if (ind==control.length-1) {
//        alert("錯誤：「@」之後不可為空字串。");
        alert("Your email address entries did not match the correct format.");
        return false;
    } else {
        return true;
    }
}


function checkPassword_1(pwd1, pwd2) {

    if (pwd1.length==0) {
        alert('Notice! The field of Password was still blank.');
        return false;
    } else if (pwd2.length==0) {
        alert('Notice! The field of Confirm Password was still blank.');
        return false;
    } else if (pwd1 != pwd2) {
        alert("Password and Confirm Password must be the same.");
        return false;
    } else {
        return true;
    }
}


function checkPassword_2(pwd) {
    for( idx = 0 ; idx <pwd.length ; idx++ ) {
        if( !( ( pwd.charAt(idx)>= 'A' && pwd.charAt(idx) <= 'Z' ) || ( pwd.charAt(idx)>= 'a' && pwd.charAt(idx) <= 'z' ) || ( pwd.charAt(idx)>= '0' && pwd.charAt(idx) <= '9' ) ) ) {
            return false;
        }
    }
    return true;
}


function checkLength(column, limit1, limit2) {
    if (column.length < limit1 || column.length > limit2) {
        return false;
    }
    return true;
}


function checkNum (column, columnName) {
	for (i=0; i<column.length; i++)	{
		c = column.charAt(i);
		if ("0123456789".indexOf(c,0)<0) {
			alert('Please enter numbers into the ' + columnName);
			return false;
		}
	}
	return true; 
/*
	var FT = /[0-9]/;
	if(FT.test(column)==false)	{
		return false;
	} else {
        return true;    
    }
*/
}


// 去除字串前後空白 2005/10/25
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
function checkNull (column) {
	column = column.trim();
	if( column.length == 0 ) {
		return false;
	} else {
		return true;
	}		
}


function checkSelect (select) {
	if( select.options[0].selected == true ) {
		return false;
	} else {
		return true;
	}
}


function nullCheckBox (frm, column, columnName) {
	var len = frm.elements.length;
	var flag = 0;
	for (var i = 0; i < len; i++) { 
		var e = frm.elements[i]; 
		if (e.name == column) { 
			if (e.checked)
				flag++;		
		} 
	} 
	if (flag==0) {
		alert('Notice! The field of ' + columnName + ' was still blank.');
		return false;
	}
	return true;
}


function nullCheckRadio (frm, column) {
	var len = frm.elements.length;
	var flag = 0;
	for (var i = 0; i < len; i++) { 
		var e = frm.elements[i]; 
		if (e.name == column) { 
			if (e.checked)
				flag++;		
		} 
	} 
	if (flag==0)  return false;
	return true;
}


function checkWordNumber (frmName, column, fieldName, limit) {
    if (eval("document." + frmName + "." + column + ".value.length") > limit) {
        alert('The ' + fieldName + ' is over ' + limit + ' characters!');
        eval("document." + frmName + "." + column + ".value = document." + frmName + "." + column + ".value.substr(0,limit)");
    }
}


function checkAccept(accpetfrm, column) {
	eval("document." +accpetfrm + ".submit();");
/*
    if (eval("document." +accpetfrm + "." + column + ".checked")) {
        eval("document."+accpetfrm + ".submit();");
    } else {
		alert('If you want to be the B2BChinaSources member, \n\nyou have to confirm B2B China Sources Terms of Service Agreement.');
	}
*/
}


// 忘記密碼
function ForgotPwd(frm) {
	if (!checkNull(frm.t2_account.value)) {
		alert('Notice! The field of ID was still blank.');
		frm.t2_account.focus();
		return false;
	}
	if (!checkNull(frm.t2_email.value)) {
		alert('Notice! The field of Email address was still blank.');
		frm.t2_email.focus();
		return false;
	}
	return true;
}


// 密碼規則(限用6~10字元, 英文/數位皆需使用, 不可包含帳號, 只限輸入A~Z, a~z, 0~9, 請勿輸入空白或其他符號)
function checkPasswordRule(account, password) {
    var pwd = password.value.trim();    // 去除前後空白
    var acc = account.value;
//    if(pwd.indexOf( acc )>= 0)  return false;   // 不可含帳號
    if(pwd.indexOf( account.value )>= 0)  return false;   // 不可含帳號
    if( pwd.length == 0 ) return false;   // 未填資料 
    else password.value = pwd;
    for( idx = 0 ; idx <pwd.length ; idx++ ) {  // 只限輸入A~Z, a~z, 0~9
        if(!((pwd.charAt(idx)>='A' && pwd.charAt(idx)<='Z') || (pwd.charAt(idx)>='a' && pwd.charAt(idx)<='z') || (pwd.charAt(idx)>='0' && pwd.charAt(idx)<='9'))) {
            return false;
        }
    }
    if(!/.*[a-zA-Z]+.*/.test(pwd))  return false;    // 至少要一英文
    if(!/.*\d+.*/.test(pwd))  return false;   // 至少要一數字
    return true;
}


// Javascript (Base64 encode/decode) (Ayling 2008-04-23) ===========================
function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	c1 = str.charCodeAt(i++) & 0xff;
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt((c1 & 0x3) << 4);
	    out += "==";
	    break;
	}
	c2 = str.charCodeAt(i++);
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	    out += base64EncodeChars.charAt((c2 & 0xF) << 2);
	    out += "=";
	    break;
	}
	c3 = str.charCodeAt(i++);
	out += base64EncodeChars.charAt(c1 >> 2);
	out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
	out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}
function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;
    var base64DecodeChars = new Array(
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
        -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	/* c1 */
	do {
	    c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c1 == -1);
	if(c1 == -1)
	    break;

	/* c2 */
	do {
	    c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c2 == -1);
	if(c2 == -1)
	    break;

	out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

	/* c3 */
	do {
	    c3 = str.charCodeAt(i++) & 0xff;
	    if(c3 == 61)
		return out;
	    c3 = base64DecodeChars[c3];
	} while(i < len && c3 == -1);
	if(c3 == -1)
	    break;

	out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

	/* c4 */
	do {
	    c4 = str.charCodeAt(i++) & 0xff;
	    if(c4 == 61)
		return out;
	    c4 = base64DecodeChars[c4];
	} while(i < len && c4 == -1);
	if(c4 == -1)
	    break;
	out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}


// Javascript UTF-8 <=> UTF-16 convertion (Ayling 2008-04-23) ======================
function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        }
    }
    return out;
}
function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while(i < len) {
	c = str.charCodeAt(i++);
	switch(c >> 4)
	{ 
          case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
            // 0xxxxxxx
            out += str.charAt(i-1);
            break;
          case 12: case 13:
            // 110x xxxx   10xx xxxx
            char2 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
            break;
          case 14:
            // 1110 xxxx  10xx xxxx  10xx xxxx
            char2 = str.charCodeAt(i++);
            char3 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x0F) << 12) |
                           ((char2 & 0x3F) << 6) |
                           ((char3 & 0x3F) << 0));
            break;
        }
    }

    return out;
}