/*
* @version : 3.0 (Patch 4)
* @update : 2009-10-01
*/
/* exten jquery function */
function clean_username (username) {
	username = username.toLowerCase();
	return (username || "").replace(/\s/g, "" );
}

function clean_whitespace( text ) { // remove all space
	return (text || "").replace(/\s/g, "" );
}

function clean_url( url ) { // add url protocal
	if(url) {
		var regexp = /(ftp|http|https):\/\/?/;
		if (!regexp.test(url)) {
			url = "http://"+url;
		}
	}
	return (url || "").replace(/\s/g, "" );
}

function clean_email( email ) {
	email = email.toLowerCase();
	return (email || "").replace(/\s/g, "" );
}

function key_digit(e) {
	var KeyCode = (e.keyCode) ? e.keyCode : e.which;
	var CharCode = (e.charCode) ? e.charCode : 0;
	CharCode = (BrowserDetect.browser=="Explorer") ? -1 : CharCode;
	return ((KeyCode == 8) // backspace
		|| (KeyCode == 9) // tab
		|| (KeyCode == 37) // left arrow
		|| (KeyCode == 39) // right arrow
		|| ((KeyCode == 46) && (CharCode == 0)) // delete
		|| (CharCode == 0)
		|| ((KeyCode > 47) && (KeyCode < 58)) // 0 - 9
	);
}

function validateField(field) {
	var error = false;

	// remove whitespace
	$(field).val( jQuery.trim( $(field).val() ) );

	// username field
	if ( $(field).hasClass("username") ) {
		$(field).val( clean_username( $(field).val() ) );
	}
	// email field
	if ( $(field).hasClass("email") ) {
		$(field).val( clean_email( $(field).val() ) );
	}
	// url field
	if ( $(field).hasClass("url") ) {
		$(field).val( clean_email( $(field).val() ) );
	}

	// required fields
	if ($(field).attr("class").indexOf("required") != -1) {
		if (!$(field).val().length)
			error = true;
	}
	// numeric fields
	//console.log( $(field).attr("name") + " : " + $(field).val())
	if ($(field).val().length && $(field).hasClass("numeric") ) {
		if (!/^[0-9]*$/.test($(field).val()))
			error = true;
	}
	// emails
	if ($(field).val().length && $(field).hasClass("email") ) {
		if (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test($(field).val()))
			error = true;
	}
	// url
	if ($(field).val().length && $(field).hasClass("url") ) {
		if (!/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test($(field).val()))
			error = true;
	}

	if (error) {
		$(field).addClass("focus");
	} else {
		$(field).removeClass("focus");
	}

	return !error;
}

//
function validUsername(value) {
	try {
		var rs = (/^[a-zA-Z0-9]{1}([a-zA-Z0-9]|_){5,19}$/.test(value));
		//var rs = (/^[a-zA-Z0-9]{1}([a-zA-Z0-9]|_|-|@|\.){4,18}[a-zA-Z0-9]{1}$/.test(value));
		//var rs = (/^[a-zA-Z0-9]{5,20}$/.test(value));
		return rs;
	} catch (e) {}
	return false;
}

function validPhone(value) {
	try {
		var rs_tel = (/^[0]{1}[0-79]{1}[0-9]{7}$/.test(value));
		var rs_mobile = (/^[0]{1}[8]{1}[0-9]{8}$/.test(value));
		return (rs_tel || rs_mobile);
	} catch (e) {}
	return false;
}

$.fn.clearForm = function() {

	// iterate each matching form
	return this.each(function(){
		// iterate the elements within the form
		$(":input", this).each(function(){
			var type = this.type, tag = this.tagName.toLowerCase();
			if (type == "text" || type == "password" || tag == "textarea")
				this.value = "";
			else if (type == "checkbox" || type == "radio")
				this.checked = false;
			else if (tag == "select")
				this.selectedIndex = -1;
		});
	});
};


function valid_char(str, maxlength) {
	var total=0;
	maxlength = parseInt(maxlength);
	if ( maxlength > 0 ) {
		var str_ir = 'ั'+'ํ'+'่'+'๋'+'ี'+'ึ'+'ื'+'ิ'+'๊'+'้'+'็'+'ุ'+'ู';
		var str_len = 0;
		for(i=0; i<str.length; i++) {
			var char_code = str.substr(i, 1);

			//if (char_code == 'ั' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' || char_code == '' ) {
			if (str_ir.indexOf(char_code) != -1) {
				total++;
			} else {
				str_len++;
			}
			if ( str_len == maxlength ) break;
		}

		return str.substr(0, maxlength+total);
	} else {
		return str;
	}
}

// for Menu
menuHover = function() {
	var cssRule;
	var newSelector;
	for (var i = 0; i < document.styleSheets.length; i++)
		for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
			{
			cssRule = document.styleSheets[i].rules[x];
			if (cssRule.selectorText.indexOf("LI:hover") != -1)
			{
				 newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
				document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
			}
		}
	var getElm = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<getElm.length; i++) {
		getElm[i].onmouseover=function() {
			this.className+=" iehover";
		}
		getElm[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", menuHover);



