function submitSigninForm(form) {
  if (checkSigninForm(form)) {
	var keepLoginCBEl = document.getElementById("keepLoginCB");
	var keepLoginEl = document.getElementById("keepLogin_page");
	
	if( keepLoginCBEl != null && keepLoginEl != null ) {
		if( keepLoginCBEl.checked ) {
			keepLoginEl.value = "1";
		}
	}
  
    form.submit();
  }
}

function checkSigninForm(form) {
  if (form.user_name.value == "") {
     alert(label_member_alert_input_username);
     form.user_name.focus();
     return(false);
  }
  if (form.user_name.value.length > 20 || form.user_name.value.length < 4) {
     alert(label_member_alert_length_username);
     form.user_name.focus();
     return(false);
  }

  if (form.password.value == "") {
     alert(label_member_alert_input_password);
     form.password.focus();
     return(false);
  }

  return(true);
}

function submitRegForm(form) {
  if (checkRegForm(form)) {
    form.submit();
  }
}

function checkRegForm(form) {
  if (trim(form.user_name.value) == "") {
     alert(label_member_alert_input_username);
     form.user_name.focus();
     return(false);
  }
  if (form.user_name.value.search(/^[0-9a-zA-Z\-_ ]+$/) == -1)
  {
     alert(label_member_alert_format_username);
     form.user_name.focus();
     return(false);
  }
  if (form.user_name.value.length > 20 || form.user_name.value.length < 4) {
     alert(label_member_alert_length_username);
     form.user_name.focus();
     return(false);
  }

  form.password.value = form.password.value;
  if (form.password.value.length == 0)
  {
	alert(label_member_alert_input_password);
     form.password.focus();
     return(false);
  }
  if (form.password.value!="" && form.password.value.search(/^[0-9a-zA-Z]+$/) == -1)
  {
     alert(label_member_alert_format_password);
     form.password.focus();
     return(false);
  }
  if (form.password.value != form.confirm_password.value) {
     alert(label_member_alert_correct_confirm_password);
     form.confirm_password.focus();
     return(false);
  }
  if (form.password.value!="" && form.password.value.length < 6) {
     alert(label_member_alert_length_password);
     form.password.focus();
     return(false);
  }

  if (form.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
     alert(label_member_alert_format_email);
     form.email.focus();
     return(false);
  }
  
  if (trim(form.nickname.value) == "") {
     alert(label_member_alert_input_nickname);
     form.nickname.focus();
     return(false);
  }
  
  if (form.nickname.value.length > 20) {
     alert(label_member_alert_length_nickname);
     form.nickname.focus();
     return(false);
  }
  
  //if( form.im_alert.checked ) {
	//if( trim(form.im_name.value) == "" ) {
	//	alert(label_member_alert_input_im_name);
	//	return(false);
	//}
  //}

  return(true);
}

function submitMembershipForm(form) {
  if (checkMembershipForm(form)) {
    form.submit();
  }
}

function checkMembershipForm(form) {
  if (form.password.value!="" && form.password.value.search(/^[0-9a-zA-Z]+$/) == -1)
  {
     alert(label_member_alert_format_password);
     form.password.focus();
     return(false);
  }
  if (form.password.value != form.confirm_password.value) {
     alert(label_member_alert_correct_confirm_password);
     form.confirm_password.focus();
     return(false);
  }
  if (form.password.value!="" && form.password.value.length < 6) {
     alert(label_member_alert_length_password);
     form.password.focus();
     return(false);
  }

  if (trim(form.homephone.value) != "") {
    if (!isInteger(form.homephone.value)) {
       alert(label_member_alert_format_homephone);
       form.homephone.focus();
       return(false);
    }
  }

  if (trim(form.cellphone.value) != "") {
    if (!isInteger(form.cellphone.value)) {
       alert(label_member_alert_format_cellphone);
       form.cellphone.focus();
       return(false);
    }
  }
  
  /*if( form.im_alert.checked ) {
	if( trim(form.im_name.value) == "" ) {
		alert(label_member_alert_input_im_name);
		return(false);
	}
  }*/
  
  var nicknameEl = document.getElementById("nickname");
  var s = nicknameEl.value;
  s = trim(s);
 
  if( s.length == 0 ) {
		alert(label_member_alert_input_nickname);
	    form.nickname.focus();
	    return(false);
  } else if( s.length > 20 ) {
		alert(label_member_alert_length_nickname);
	    form.nickname.focus();
	    return(false);
  }
  
  if (trim(form.bMon.value) != "" || trim(form.bYear.value) != "") {
    if (!isInteger(form.bMon.value) || (form.bMon.value < 1) || (form.bMon.value > 12)) {
       alert(label_member_alert_format_birthday);
       form.bMon.focus();
       return(false);
    } else if (!isInteger(form.bYear.value) || (form.bYear.length < 4)) {
       alert(label_member_alert_format_birthday);
       form.bYear.focus();
       return(false);
    } else {
        if (form.bMon.value.length == 1)
          form.birthday.value = "0" + form.bMon.value + "/" + form.bYear.value;
	else
	  form.birthday.value = form.bMon.value + "/" + form.bYear.value;
    }
  }
  
  return(true);
}

function testInput()
{
	alert(1);
	return false;
}

function ForceNumericInput(This, event, AllowDot, AllowMinus)
{
	var code = event.keyCode;
	switch(code)
	{
		case 8:     // backspace
		case 37:    // left arrow
		case 39:    // right arrow
		case 46:    // delete
		event.returnValue=true;
		return;
	}
	if(code == 189)     // minus sign
	{
		if(AllowMinus == false)
		{
			event.returnValue=false;
			return false;
		}
		
		// wait until the element has been updated to see if the minus is in the right spot
		var s = "ForceNumericInput(document.getElementById('"+This.id+"'))";
		setTimeout(s, 250);
		return;
	}
	if(AllowDot && code == 190)
	{
		if(This.value.indexOf(".") >= 0)
		{
			// don't allow more than one dot
			event.returnValue=false;
			return false;
		}
		event.returnValue=true;
		return;
	}
	if(event.shiftKey)
	{
		event.returnValue=false;
		return false;
	}
	// allow character of between 0 and 9
	if((code >= 48 && code <= 57) || (code >= 96 && code <= 105))
	{
		event.returnValue=true;
		return;
	}
	event.returnValue=false;
	return false;
}	

function isFloat(s)
{   
	var dotAppeard = false;
	var i;
	
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
		
		if( c == "." ) {
			if( i === 0 || i == (s.length - 1) ) {
				return false;
			} else if( dotAppeard ) {
				return false;
			}
		
			dotAppeard = true;
		} else if ( ((c < "0") || (c > "9")) ) {
			return false;	
		}
    }
    // All characters are numbers.
    return true;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function trim(TRIM_VALUE)
{
  if (TRIM_VALUE.length < 1){
    return"";
  }
  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);
  if (TRIM_VALUE==""){
    return "";
  } else{
    return TRIM_VALUE;
  }
} //End Function

function RTrim(VALUE)
{
  var w_space = String.fromCharCode(32);
  var v_length = VALUE.length;
  var strTemp = "";
  if (v_length < 0){
    return"";
  }
  var iTemp = v_length -1;

  while(iTemp > -1){
    if (VALUE.charAt(iTemp) == w_space) {
    } else {
      strTemp = VALUE.substring(0,iTemp +1);
      break;
    }
    iTemp = iTemp-1;
  } //End While

  return strTemp;
} //End Function

function LTrim(VALUE)
{
  var w_space = String.fromCharCode(32);
  if (v_length < 1) {
    return "";
  }
  var v_length = VALUE.length;
  var strTemp = "";

  var iTemp = 0;

  while (iTemp < v_length) {
   if (VALUE.charAt(iTemp) == w_space) {
   } else {
     strTemp = VALUE.substring(iTemp,v_length);
     break;
   }
   iTemp = iTemp + 1;
  } //End While
  return strTemp;
} //End Function

/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    try{
	
	document.cookie= name + "=" + escape(value) +
        	((expires) ? "; expires=" + expires.toGMTString() : "") +
        	((path) ? "; path=" + path : "") +
        	((domain) ? "; domain=" + domain : "") +
        	((secure) ? "; secure" : "");
	}
	catch(x) {
		alert(x.message);
	}
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    	try{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
	} catch( x ) {
		alert(x.message);
	}
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function closeMemo() {
  var hideobj = document.getElementById("memo");
  if (hideobj != null) {
    hideobj.style.visibility='hidden';
    hideobj.style.display='none';
  }
}
function openMemo() {
  var hideobj = document.getElementById("memo");
  if (hideobj != null) {
    hideobj.style.visibility='visible';
    hideobj.style.display='block';
  }
}

function isValidPeriod( start, end ) {
	if( start == null || end == null || start.length != 10 || end.length != 10 ) {
		return false;
	}

	var startY = start.substring(0, 4);
	var endY = end.substring(0, 4);
	
	if( startY > endY ) {
		alert(label_match_end_date_must_larger_than_start_date);
		return false;
	} else if( startY < endY ) {
		return true;
	}
	
	var startM = start.substring(5, 2);
	var endM = end.substring(5, 2);
	
	if( startM > endM ) {
		alert(label_match_end_date_must_larger_than_start_date);
		return false;
	} else if( startM < endM ) {
		return true;
	}
	
	var startD = start.substring(8);
	var endD = end.substring(8);
	
	if( startD > endD ) {
		alert(label_match_end_date_must_larger_than_start_date);
		return false;
	} else if( startD < endD ) {
		return true;
	}
	
	return true;
}

function removeFocus( el ) {
	if( el.blur ) {
		el.blur();
	}
}

function colorToHex(color) {
    if (color.substr(0, 1) == '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    
    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);
    
    var rgb = blue | (green << 8) | (red << 16);
    return digits[1] + '#' + rgb.toString(16);
};

function changeColour(elementId, colour1, colour2) {
    var interval = 500;
    var element = document.getElementById(elementId);
    if (element != null) {
        var color = element.style.color;
        if (color) {
            var colorx = colorToHex(color);
            element.style.color = (colorx == colour1) ? colour2 : colour1;
        } else {
          element.style.color = colour2;
        }
        setTimeout("changeColour('" + elementId + "','" + colour1 + "','" + colour2 + "')", interval);
    }
} 

