﻿// JavaScript Document
function trim(str){
	var trimmedStr = str;
	if(str.length > 0){
		var iStartIndex = 0;
		var iEndIndex = str.length;
		var i, j;
		for(i = 0; i < str.length; i ++){
			if(str.charAt(i) != " "){
				iStartIndex = i;
				break;
			}	
		}
		for(j = str.length - 1; j >= 0; j --){
			if(str.charAt(j) != " "){
				iEndIndex = j + 1;
				break;
			}	
		}
		if(iStartIndex == 0 && str.charAt(0) == " "){
			trimmedStr = "";
		}
		else{
			trimmedStr = str.substring(iStartIndex, iEndIndex);
		}
	}
	return trimmedStr;
}

function chk_number(f1)
{
	if (f1.value != "")
	{
		var re = f1.value;
		if (!re.match(/^\d+$/))	
		{
			f1.value= "";
			alert("Please enter numbers");
			f1.select();
			f1.focus();
			return false;
		}
	}
}

function isNumber(value){
	if(isInteger(value) || isFloat(value)){
		return true;
	}
	else{
		return false;
	}
}

function CHM(email)
{
	if (email.value != "")
	{
		var re= email.value;
        if (re == "" || re.indexOf("@") == -1 || re.indexOf(".") == -1)
        {
				alert("Please provide correct email addresses in this fields");
				email.select();
		    	email.focus();
				return false;
		}
	}
}

function enterLen(f1, nums)
{
	if (f1.value.length != 0)
	{
		var text_flag = true;
		if (!chk_text(f1))
		{
			text_flag = false;
		}
		
		var expire = nums;
		var str = f1.value;
		var strlen = str.length;
		var intAscii = 0;
		var nanAscii = 0;
		var lens = 0;
		var i;
		var flag = false;
		for(i=0; i < strlen; i++)
		{
			var cChar = str.charAt(i);
			var nums = toAscii(cChar);
			if(nums > 0)
				{
					intAscii++;
				}
			else	{nanAscii ++;}
			lens = intAscii + (nanAscii * 2);
		}	
		//alert("intAscii = "+intAscii+"\n\nnanAscii = "+nanAscii);
		
		if (lens > expire)
		{
			if (!text_flag)
			{
				alert("Please use only letters, numbers and underscores in this field.");
				f1.focus();
				f1.select();
				return false;
			}
			else
			{
				alert("It contains Invalid characters in this field.\n It is limited to " + expire + " letters at maximum .");
				f1.focus();
				f1.select();
				return false;
			}
		}
		else
		{
			if (!text_flag)
			{
				alert("Please use only letters, numbers and underscores in this field.");
				f1.focus();
				f1.select();
				return false;
			}
		}
	}
}

function stringLen(f1)
{
	var str = f1.value;
	var strlen = str.length;
	var intAscii = 0;
	var nanAscii = 0;
	var lens = 0;
	var i;
	var flag = false;
	for(i=0; i < strlen; i++)
	{
		var cChar = str.charAt(i);
		var nums = toAscii(cChar);
		if(nums > 0)
			{
				intAscii++;
			}
		else	{nanAscii ++;}
		
		lens = intAscii + (nanAscii * 2);
	}	
	return (lens);		
}


function toAscii (chars)  
{
	var Ascii_Decimal;
	var symbols = " !\"#$%&'()*+,-./0123456789:;<=>?@"
	var loAZ = "abcdefghijklmnopqrstuvwxyz"
	        symbols+= loAZ.toUpperCase()
	        symbols+= "[\\]^_`"
	        symbols+= loAZ
	        symbols+= "{|}~"
	        //symbols+= ""
	var loc
	    loc = symbols.indexOf(chars)
	    if (loc >-1)
	    {
	 	Ascii_Decimal = 32 + loc;
	    	return (Ascii_Decimal);
	    }
	return(0) // If not in range 32-126 return ZERO
}


function SN(fs)
{
	if (fs.value.length != 0)
	{
		var re= fs.value;
		if (!re.match(/^\d{10,11}$/))
		{
			alert("Please enter 10~11 digit number");
			fs.focus();
			fs.select();
		}
	}
}

function validateHomePhone(fs)
{
	if (fs.value.length != 0)
	{
		var re= fs.value;
		if (!re.match(/^\d{6,8}$/))
		{
			alert("The Phone number entered must be between 6 to 8 digits");
			fs.focus();
			fs.select();
		}
	}
	
}





// check text 

function chk_text(f1)
{
	var re = f1.value;
	if (re.match(/"|'|<|>|#|&/))
	{
		return false
	}
	return true;
}

function chkText(f1)
{
	var re = f1.value;
	if (re.match(/"|'|<|>|#|&/))
	{
		alert("Please use only letters, numbers and underscores in this field.");
		f1.focus();
		f1.select();
		return false
	}
}


function passwdLen(ids, id)
{
	
	if (ids.value !="")
	{
	var ra = ids.value;
		if (!ra.match(/^\w+$/))
		{
			alert("Please enter letters or numbers");
			ids.select();
			ids.focus();
			
		}
		
		
		if(ids.value.length < 4)
		{
			alert("Please enter at least 4 characters");
			ids.select();
			ids.focus();
			
		}

	}
		
}

function regdLen(ids, id)
{
	
	if (ids.value !="")
	{
	var ra = ids.value;
		if (!ra.match(/^\w+$/))
		{
			alert("Please enter letters or numbers");
			ids.select();
			ids.focus();
			
		}
		
		
		if(ids.value.length < 10)
		{
			alert("Please enter at least 10 characters");
			ids.select();
			ids.focus();
			
		}

	}
		
}
function pidCheck(id)
{
	
	if (id.value !="")
	{
	var ra = id.value;
		if (!ra.match(/^\w+$/))
		{
			alert("Please enter letters or numbers");
			id.select();
			id.focus();
			
		}
		
		
		if(id.value.length < 4)
		{
			alert("Please enter at least 4 characters");
			id.select();
			id.focus();
			
		}

	}
		
}
function hiLiteInput( field )
{
	var newcolor = '#ececec';

	if( field.style.backgroundColor == newcolor )
	{
		field.style.backgroundColor = field.getAttribute( 'oldColor' );
	} else {
		field.setAttribute( 'oldColor', field.style.backgroundColor );
		field.style.backgroundColor = newcolor;
	}

	if( field.defaultv == "1" )
	{
		field.value = '';
		field.defaultv = 0;
	}
}
