// Open new window
function OpenWin(url){
  newWin = window.open(url,"winname","toolbar=no, location=no, status=no, menubar=no, width=500, height=300");
} 


//Check for required fields
function valid(theForm, cols) {
  var el = document.forms[0].elements;

//  for(var i = 0 ; i < el.length ; ++i) {
  for(var i = 0 ; i < cols ; ++i) {	
    var itemchecked = false;
    if ((el[i].type == "radio") || (el[i].type == "checkbox")) {
      var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
      for(var j = 0 ; j < radiogroup.length ; ++j) {
				if(radiogroup[j].checked) {
					itemchecked = true;
					break;
				}
      }
   } else if (el[i].type == "text") {
      if (el[i].value.length>0) {
   		 	itemchecked = true;
      }
   } else {
	    if (el[i].value.length>0) {
    		itemchecked = true;
    	}
   }

    if(!itemchecked) {
      //alert("請輸入: "+el[i].name+".");
      alert("請輸入.");
      if(el[i].focus)
        el[i].focus();
      return false;
    }
  }
  return true;
}


//Check for Validate number
function isNumber(field){
	validstr="0123456789";
  	idx=0;
  	strtxt = field.value;
  	if (strtxt==null || strtxt==""){
  		return false;
  	}
  	while (idx < strtxt.length){
  		if (validstr.indexOf(strtxt.charAt(idx)) == -1 || strtxt.charAt(idx++) == " ")
			return false;
  	}
  	return true;
}


//Check for Validate phone number
function isPhoneNumber(field){
	validstr="0123456789()-#";
  	idx=0;
  	strtxt = field.value;
  	if (strtxt==null || strtxt==""){
  		return false;
  	}
  	while (idx < strtxt.length){
  		if (validstr.indexOf(strtxt.charAt(idx)) == -1 || strtxt.charAt(idx++) == " ")
			return false;
  	}
  	return true;
}
function checkMouse(emailObj){
 idx=0;
 count = 0;
 exist = "false";
 strtxt = emailObj.value;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == "@"){
		exist = "true";
	}
 }
 if(exist == "true")
 	return true;
 else
 	return false;
}

function checkDot(emailObj){
 idx=0;
 exist = "false";
 strtxt = emailObj.value;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == "."){
		exist = "true";
	}
 }
 if(exist == "true")
 	return true;
 else
 	return false;
}

function checkSemicolon(emailObj){
 idx=0;
 strtxt = emailObj.value;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == ";")
		return false;
 }
 return true;
}

function checkSpace(emailObj){
 idx=0;
 strtxt = emailObj.value;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == " ")
		return false;
 }
 return true;
}

function checkDoubleDot(emailObj){
 idx=0;
 strtxt = emailObj.value;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx) == "." && strtxt.charAt(idx+1) == "."){
		return false;
	}
	idx++;
 }
 return true;
}

function checkMouseDotOrDotMouse(emailObj){
 idx=0;
 strtxt = emailObj.value;
 while (idx < strtxt.length){
	if ((strtxt.charAt(idx) == "@" && strtxt.charAt(idx+1) == ".")||(strtxt.charAt(idx) == "." && strtxt.charAt(idx+1) == "@")){
		return false;
	}
	idx++;
 }
 return true;
}
function checkEmail(emailObj){
    var emailType = /\b\w+[@]\w+\.{1}\w+\b/;
    strtxt = emailObj.value;
     if (strtxt==""){
	    alert ("電子郵件信箱 請不要空白");
        emailObj.focus();
        return false;
     }else if(!strtxt=="" && (!checkMouse(emailObj) || !checkDot(emailObj))){
 	    alert ("請輸入正確格式的電子郵件信箱");
        emailObj.focus();
        return false;
     }else if(strtxt.charAt(strtxt.length-1)=="@"){
        alert ("電子郵件信箱的結尾不能為( @ )小老鼠");
        emailObj.focus();
        return false;
     }else if(strtxt.charAt(0)=="@"){
        alert ("電子郵件信箱的開頭不能為( @ )小老鼠");
        emailObj.focus();
        return false;
     }else if(strtxt.charAt(strtxt.length-1)=="."){
        alert ("電子郵件信箱的結尾不能為( . )小數點");
        emailObj.focus();
        return false;
     }else if(strtxt.charAt(0)=="."){
        alert ("電子郵件信箱的開頭不能為( . )小數點");
        emailObj.focus();
        return false;
     }else if(!checkDoubleDot(emailObj)){
        alert ("電子郵件信箱不可含有連續小數點");
        emailObj.focus();
        return false;
     }else if(!checkMouseDotOrDotMouse(emailObj)){
        alert ("電子郵件信箱中小數點不可緊鄰( @ )小老鼠");
        emailObj.focus();
        return false;
     }
     return true;
}

function ChecktheForm(theForm){
//    if (!checkEmail(theForm.receiver)){
//	    theForm.receiver.focus();
//        return false;
//    }
		if (theForm.fromName.value.length<1){
        alert("請輸入您的大名.");
				theForm.fromName.focus();
        return false;
		}
    if (!checkEmail(theForm.fromEmail)){
        theForm.fromEmail.focus();
        return false;
    }
    //theForm.submit();
		return true;
}

