/*
* @version : 4.4 (Patch 7)
* @update : 2010-04-09
*/
/* 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 FormatCurrency(num) {
	//alert("currency format");
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	//eturn (((sign)?'':'-') + '$' + num + '.' + cents);
	return (((sign)?'':'-') + num);
}

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
	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 resizeSWF( type, value, id ){
	value = Math.ceil(value);
	//if (id.substr(0,7) == "tooltip") alert('resizeSWF : ' + type + ' , ' + value + ' , ' + id);
	//console.log('test : ' + type + ' : ' + value + ' : ' + id);

	if ( type == "width" ) {
		$("#"+id).children().attr("width", value);
	} else if ( type == "height" ) {
		$("#"+id).children().attr("height", value);
	}
}

function is_login() {
	if ( $("a#btHomeLogout, div#topMenu a#mnLogout, a#Pizza_mnLogout").length > 0 ) {
		return true;
	} else {
		return false;
	}
}

function popupTerm(){
	if(tb_show) {
		tb_show("", "pizza_term.html?keepThis=true&TB_iframe=true&height=500&width=650", "");
	}
}

