﻿// For SurfGold Only Function 
function check_id(id,num){
   if(num[0].checked){
     if (CheckID(id,"請輸入正確身份證號碼!")){
     	  return true;
     }else{
        return false;
     }
   }else{
     if(CheckTextValue(id,"10","20",3,"請您填寫正確護照號碼!")){
       return true;
     }else{
       return false;
     }
   }
}


function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function CheckText(inText,errMsg){
	if(inText.value == ""){
		alert("\n\n" + errMsg + "\n\n");
		inText.focus();
		return false;
	}
	return true;
}


function CheckSelect(E,errMsg){
    if(E.value == null || E.value == "")
    {
	    alert("\n\n" + errMsg + "\n\n");
		E.focus();
	    return false;
    }else{
	    return true;
    }
}

function CheckRadioSelect(R,E,errMsg){
	if(R.checked){
		if(E.options[0].selected)
		{
			alert("\n\n" + errMsg + "\n\n");
			E.focus();
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}


function CheckEmail(z,errMsg){

    for ( i=0; i<= z.value.length - 1 ;i++ ){
		x = z.value.charAt(i)
		y = escape( x)
		if ( y != x && y !='%40'){
			alert("\n\n" + errMsg + "\n\n");
			z.focus();
			return false;
			i = z.value.length;
		}
	}
	
	if ( z.value.indexOf('@') == -1 ){
		alert("\n\n" + errMsg + "\n\n");
		z.focus();
		return false;
	}

	if ( z.value.indexOf('.') == -1 ){
		alert("\n\n" + errMsg + "\n\n");
		z.focus();
		return false;
	}

	return true;
}



/* 
dataType :	0: Any character
			1: Must be a numeric, include integer and float
			2: Must be a letter, not include any 0-9
			3: Include only a-z, A-Z And 0-9
			4: mail format check , a-z, A-Z , 0-9 , @ and _ , - , .
			5: Must be a numeric, include 0-9
			7: Must be a numeric, include 0-9 and - ( ) #
*/
function CheckTextValue(inText,minLength,maxLength,dataType,errMsg){
	if((inText.value.length < minLength)||(inText.value.length > maxLength)){
		alert("\n\n" + errMsg + "\n\n");
		inText.focus();
		return false;
	}

	if((dataType == 1) && isNaN(inText.value)){
		alert("\n\n" + errMsg + "\n\n");
		inText.focus();
		return false;
	}

	if(dataType == 2){
		var s;
		var dataString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
		for(var i=0;i<inText.value.length;i++){
			s = inText.value.substring(i,i+1);
			if(dataString.indexOf(s)<0){
				alert("\n\n" + errMsg + "\n\n");
				inText.focus();
				return false;
			}
		}
	}

	if(dataType == 3){
		var s;
		var dataString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
		for(var i=0;i<inText.value.length;i++){
			s = inText.value.substring(i,i+1);
			if(dataString.indexOf(s)<0){
				alert("\n\n" + errMsg + "\n\n");
				inText.focus();
				return false;
			}
		}
	}

	if(dataType == 4){
		var s;
		var dataString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@"
		if(inText.value.indexOf("\@")<1){
			alert("\n\n" + errMsg + "\n\n");
			inText.focus();
			return false;
		}else{
			for(var i=0;i<inText.value.length;i++){
				s = inText.value.substring(i,i+1);
				if(dataString.indexOf(s)<0){
					alert("\n\n" + errMsg + "\n\n");
					inText.focus();
					return false;
				}
			}
		}
	}

    if(dataType == 5){
		var s;
		var dataString = "0123456789"
		for(var i=0;i<inText.value.length;i++){
			s = inText.value.substring(i,i+1);
			if(dataString.indexOf(s)<0){
				alert("\n\n" + errMsg + "\n\n");
				inText.focus();
				return false;
			}
		}
	}
	
    if(dataType == 7){
		var s;
		var dataString = "0123456789()-# "
		for(var i=0;i<inText.value.length;i++){
			s = inText.value.substring(i,i+1);
			if(dataString.indexOf(s)<0){
				alert("\n\n" + errMsg + "\n\n");
				inText.focus();
				return false;
			}
		}
	}
	

	return true;
}

        
function CheckTextValue_1(inText,minLength,maxLength,mustNumeric,mustLetter,errMsg){
	var RE=/[a-zA-Z0-9]{5}/;

	if((inText.value.length < minLength)||(inText.value.length > maxLength)){
		alert("\n\n" + errMsg + "\n\n");
		return false;
	}

	if(!RE.test(inText.value)){
		alert("\n\n" + errMsg + "\n\n");
		return false;
	}
	return true;
}

function CheckRadio(inRadio,errMsg){
	for(var i=0;i<inRadio.length;i++){
		if(inRadio[i].checked){
			return true;
		}
	}
	alert("\n\n" + errMsg + "\n\n");
	inRadio[0].focus();
	return false;
}


function CheckCheckbox(inCheckbox,errMsg){
	for(var i=0;i<inCheckbox.length;i++){
		if(inCheckbox[i].checked){
			return true;
		}
	}
	alert("\n\n" + errMsg + "\n\n");
	inCheckbox[0].focus();
	return false;
}

function CheckCheckbox_No(inCheckbox,no,errMsg){
	check_no = 0;
	for(var i=0;i<inCheckbox.length;i++){
		if(inCheckbox[i].checked){
			check_no = check_no + 1;
			//alert(check_no);
		}
	}
	if (check_no == 3) {
		return true;
	} else {
		alert("\n\n" + errMsg + "\n\n");
		inCheckbox[0].focus();
		return false;
	}
}


function CheckRadioOther(inRadio,inOthertext,errMsg){
	for(var i=0;i<inRadio.length;i++){
		if(inRadio[i].checked){
			if (inRadio[i].value == "other")  {
				if (inOthertext.value != "") {
				return true;
				}
				else{
				alert("\n\n" + errMsg + "\n\n");
				inOthertext.focus();
				return false;
				}
			}
		}
	}
	return true;
}

function CheckOther(inCheckbox,inOthertext,errMsg){
	for(var i=0;i<inCheckbox.length;i++){
		if(inCheckbox[i].checked){
			if (inCheckbox[i].value == "other")  {
				if (inOthertext.value != "") {
				return true;
				}
				else{
				alert("\n\n" + errMsg + "\n\n");
				inOthertext.focus();
				return false;
				}
			}
		}
	}
	return true;
}

function CheckOther_1(inCheckbox,inOthertext,errMsg){
	for(var i=0;i<inCheckbox.length;i++){
		if(inCheckbox[i].checked){
			if (inCheckbox[i].value == "其他方式")  {
				if (inOthertext.value != "") {
				return true;
				}
				else{
				alert("\n\n" + errMsg + "\n\n");
				inOthertext.focus();
				return false;
				}
			}
		}
	}
	return true;
}



function CheckID(ss,errMsg){

	var c, n, i;
	var t= "ABCDEFGHJKLMNPQRSTUVXYWZIO";
	s = ss.value;

	c= s.substring(0,1);
	c= t.indexOf(c.toUpperCase());
	if((s.length!= 10) || (c<0)){
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	n= parseInt(c/10)+ c%10*9+ 1;
	for(i=1; i<9; i++) n= n+ parseInt(s.substring(i,i+1))* (9-i);
	n= (10- (n% 10))% 10;
	if(n!= parseInt(s.substring(9,10))){
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	return true;
}

function CheckProduct(ss,errMsg){
	
	var c, s;
	var t1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var t2 = "0126B";
	var t3 = "0123456789ABCR";
	var t4 = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
	
	s = ss.value;
	
	if (s.length!= 10) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(0,1);
	c = t1.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	
	c = s.substring(1,2);
	c = t1.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(2,3);
	c = t2.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(3,4);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(4,5);
	c = t4.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	return true;
}

function CheckProduct_DJ(ss,errMsg){
	
	var c, s;
	var t1 = "0123456789";
	var t2 = "M";
	var t3 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var t4 = "Y";
	
	s = ss.value;
	
	if (s.length!= 10) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(0,1);
	c = t2.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	
	c = s.substring(1,2);
	c = t4.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(2,3);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(3,4);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}

	c = s.substring(4,5);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	
	c = s.substring(5,6);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	
	c = s.substring(6,7);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	c = s.substring(7,8);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	c = s.substring(8,9);
	c = t3.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	c = s.substring(9,10);
	c = t1.indexOf(c.toUpperCase());
	if ( c < 0 ) {
		alert("\n\n" + errMsg + "\n\n");
		ss.focus();
		return false;
	}
	
	return true;
}
function CheckPassword(inText1,inText2,errMsg){
	if(inText1.value != inText2.value){
		alert("\n\n" + errMsg + "\n\n");
		inText2.focus();
		return false;
	}
	return true;
}

function CheckSelectText_ne(inSelect,inText,no,errMsg){
	if (inSelect.selectedIndex != no) {
		if (inText.value == "") {
			alert("\n\n" + errMsg + "\n\n");
			inText.focus();
			return false;
		}
	}
	return true;
}

function CheckSelectText_e(inSelect,inText,no,errMsg){
	if (inSelect.selectedIndex == no) {
		if (inText.value == "") {
			alert("\n\n" + errMsg + "\n\n");
			inText.focus();
			return false;
		}
	}
	return true;
}

function CheckRadioCheckbox(inRadio,inCheckbox,errMsg){
	if(inRadio.checked){
	for(var i=0;i<inCheckbox.length;i++){
		if(inCheckbox[i].checked){
			return true;
		}
	}
	alert("\n\n" + errMsg + "\n\n");
	return false;
	} else {
	return true;
	}
}

function CheckRadioRadio(inRadio,inRadio1,errMsg){	
	if(inRadio.checked){
	for(var i=0;i<inRadio1.length;i++){
		if(inRadio1[i].checked){
			return true;
		}
	}
	alert("\n\n" + errMsg + "\n\n");
	return false;
	}else{
	return true;
	}
	
}

function CheckRadioRadio_n(inRadio,inRadio1,errMsg){	
	if (!inRadio.checked){
	for(var i=0;i<inRadio1.length;i++){
		if(inRadio1[i].checked){
			return true;
		}
	}
	alert("\n\n" + errMsg + "\n\n");
	return false;
	}else{
	return true;
	}
	
}

function CheckCheckboxText(inCheckbox,inText,errMsg){	
	if(inCheckbox.checked){
	if(inText.value == ""){
		alert("\n\n" + errMsg + "\n\n");
		inText.focus();
		return false;
	}else{
	return true;
	}	
	}else{
	return true;
	}
	
}

function CheckRadioText(inRadio,inText,errMsg){	
	if(inRadio.checked){
	if(inText.value == ""){
		alert("\n\n" + errMsg + "\n\n");
		inText.focus();
		return false;
	}else{
	return true;
	}	
	}else{
	return true;
	}
	
}

function CheckRadioRadioText(inRadio1,inRadio2,inText,errMsg){	
	if(inRadio1.checked){
		if(inRadio2.checked){
			if(inText.value == ""){
				alert("\n\n" + errMsg + "\n\n");
				inText.focus();
				return false;
			}else{
				return true;
			}	
		}else{
			return true;
		}
	}else{
	return true;	
	}
	
}

function CheckDate(year,month,day){
  if (new Number(month.value) == 4 || new Number(month.value) == 6 || new Number(month.value) == 9 || new Number(month.value) == 11) //31日檢驗
  {
    if (new Number(day.value) == 31)
    {
      alert("錯誤: " + new Number(month.value) + "月沒有31天 !");
      month.focus();
      return false;
    }
  }
  else if (new Number(month.value) == 2)
  {
    if(new Number(day.value) > 28 && new Number(year.value) % 4 != 0)
    {
      alert("錯誤: 不是閏年2月不能大於28天 !");
      day.focus();
      return false;
    }
    else if (new Number(day.value) > 29 && new Number(year.value) % 4 == 0)
    {
      alert("錯誤: 閏年2月不能大於29天 !");
      day.focus();
      return false;
    }
  }
  return true;
}


function CheckTextLength(inText,minLength,maxLength,errMsg){
	if((inText.value.length < minLength)||(inText.value.length > maxLength)){
		alert(errMsg);
		inText.focus();
		return false;
	}
	return true;
}
