function changeLang(lang) {
	var temp_url = location.href;
	if (current_lang != lang) {
		if (temp_url.indexOf("lang=") != -1) {
			temp_url = temp_url.replace("&lang=eng", "");
			temp_url = temp_url.replace("&lang=ch", "");
			temp_url = temp_url.replace("lang=eng", "");
			temp_url = temp_url.replace("lang=ch", "");

			if (temp_url.indexOf("?") != -1) {
				temp_url += "&lang=" + lang;
			} else {
				temp_url += "?lang=" + lang;
			}
		} else {
			if (temp_url.indexOf("?") != -1) {
				temp_url += "&lang=" + lang;
			} else {
				temp_url += "?lang=" + lang;
			}
		}
		if (temp_url.indexOf("pid=") != -1) {

		}
		window.location = temp_url;
	}
}

function nv(eename){
	if(_(eename).value)
		return false;
	else
		return true;
}

function isNumber(eename){
	if(!Number(_(eename).value))
		return false;
	else 
		return true;
}

function _(id){
	if(document.getElementById(id)){
		return document.getElementById(id);
	} else {
		return false;
	}
}

function getEName(eename){
	if(!_(eename))
	obj=eename;
	else
	obj=_(eename);
	
	return obj;
}

function chclass(newcss,obj){
	obj.className = newcss;
}

function addtofavour(){
	window.external.AddFavorite('http://www.bannershop.com.hk','BannerSHOP');
}

function cssChng(obj,cName){
	obj = getEName(obj);
	obj.className = cName;
}

String.prototype.Trim = function() { 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

function input_in(obj, default_value){
	if(obj.value == default_value){
		obj.value = "";
		obj.className = "inputing";
	}
}

function input_out(obj, default_value){
	if(obj.value == ""){
		obj.value = default_value;
		obj.className = "preinput";
	}
}

function check_email(id, flag){
	var email = $('#' + id).val();
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		if(flag == true){
			alert('請輸入正確電郵地址。\nPlease provide a valid email address.');
			email.focus;
		}
		return false;
	}
	return true;
}

function check_int(id, flag){
	var int = $('#' + id).val();
	var filter = /^\d+$/;
	if (!filter.test(int)) {
		if(flag == true){
			alert('請輸入數字。\nPlease fill in Digits.');
			int.focus;
		}
		return false;
	}
	return true;
}

function byteformat($byte){
	$onekilo = 1024;
	if($byte > $onekilo * $onekilo * $onekilo){
		$unit = "GB";
		$k = round($byte / ($onekilo * $onekilo * $onekilo), 2);
	} else if($byte > $onekilo * $onekilo){
		$unit = "MB";
		$k = round($byte / ($onekilo * $onekilo), 2);
	} else if ($byte > $onekilo){
		$unit = "KB";
		$k = round($byte / $onekilo, 2);
	} else {
		$unit = "Bytes";
		$k = $byte;
	}
	return $k + '&nbsp;' + $unit;
}

function to_sqft($width, $length, $unit){
	switch($unit){
		case "cm":
			return ($width * $length) / 900;
        break;
        case "in":
        	return ($width * $length) / 144;
        break;
        case "ft":
			return $width * $length;
        break;
    }
}

function to_ft($length, $unit){
	switch($unit){
		case "cm":
			return $length / 2.5 / 12;
        break;
        case "in":
        	return $length / 12;
        break;
        case "ft":
			return $length;
        break;
    }
}

function to_cm(length, unit){
	switch(unit){
		case "cm":
			return length;
        break;
        case "in":
        	return length * 2.5;
        break;
        case "ft":
        	return length * 2.5 * 12;
        break;
    }
}

function to_in(length, unit){
	switch(unit){
		case "cm":
			return length / 2.5;
        break;
        case "in":
        	return length;
        break;
        case "ft":
        	return length * 12;
        break;
    }
}

function ceil_decimal(float, long){
	var l = 10 * long;
	return (Math.ceil(float * l) / l);
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function gen_textfield(id, value, ml, size, disabled){
	var temp_str = temp_str2 = '';
	if(disabled && disabled == true){
		temp_str2 = 'disabled="disabled"';
	}
	temp_str += '<input type="text" value="' + value + '" id="' + id + '" name="' + id + '" maxlength="' + ml + '" size="' + size + '" ' + temp_str2 + ' />';
	return temp_str;
}

function gen_select_unit(lang, id, selected_index){
	var temp_select_str = new Array('', '', '');
	if(selected_index >= 0){
		temp_select_str[selected_index] = 'SELECTED = "SELECTED"';
	}
	var temp_str = '<select name="unit' + id + '">';
	if(lang == 'ch'){
		temp_str += '<option value="ft" ' + temp_select_str[0] + ' >呎</option>';
		temp_str += '<option value="in" ' + temp_select_str[1] + ' >吋</option>';
		temp_str += '<option value="cm" ' + temp_select_str[2] + ' >厘米</option>';
	} else {
		temp_str += '<option value="ft" ' + temp_select_str[0] + ' >ft</option>';
		temp_str += '<option value="in" ' + temp_select_str[1] + ' >in</option>';
		temp_str += '<option value="cm" ' + temp_select_str[2] + ' >cm</option>';
	}
	temp_str += '</select>';
	return temp_str;
}

function gen_select_laminating(lang, id, selected_index){
	var temp_select_str = new Array('', '');
	if(selected_index >= 0){
		temp_select_str[selected_index] = 'SELECTED = "SELECTED"';
	}
	var temp_str = '<select name="coat' + id + '" >';
	if(lang == 'ch'){
		temp_str += '<option value="Matt" ' + temp_select_str[0] + ' >啞面</option>';
		temp_str += '<option value="Gloss" ' + temp_select_str[1] + ' >光面</option>';
	} else {
		temp_str += '<option value="Matt" ' + temp_select_str[0] + ' >Matt</option>';
		temp_str += '<option value="Gloss" ' + temp_select_str[1] + ' >Gloss</option>';
	}
	temp_str += '</select>';
	
	return temp_str;
}

function get_photo_size(img){
	ie = (document.all) ? true : false;
	if(ie) {
		if(thu.offsetWidth != 0){
			var twidth = thu.offsetWidth;
			var theight = thu.offsetHeight;
		} else {
			img.setStyle({
				height: '100%', width: '100%'
			});
			var twidth = thu.getWidth();
			var theight = thu.getHeight();
		}
	} else {
		var twidth = img.naturalWidth;
		var theight = img.naturalHeight;
		if(twidth == 0){
			twidth = img.width;
		}
		if(theight == 0){
			theight = img.height;
		}
	}
	return new Array(twidth, theight);
}

function change_photo_size(width, height, size_a){
	newxx = width;
	newyy = height;
	twidth = size_a[0];
	theight = size_a[1];
	if (twidth >= theight) {
		var tmpy = theight * newxx / twidth;
		if ( tmpy <= newyy ) {
			newyy = tmpy;
		} else {
			newxx = twidth * newyy / theight;
		}
	} else {
		tmpx = twidth * newyy / theight;
		if ( tmpx <= newxx ) {
			newxx = tmpx;
		} else {
			newyy = theight * newxx / twidth;
		}				
	}
	return new Array(newxx, newyy);
}

function ch_branch(id){
	var location_str = './branches.php?bid=' + id + '&lang=' + current_lang;
	window.location = location_str;
	return false;
}

function change_product(id){
	var location_str = './show_product.php?pid=' + id + '&lang=' + current_lang;
	window.location = location_str;
	return false;
}

function back_product(){
	change_product(1);
}

function go_quick_order(){
	var location_str = './quick_order.php?lang=' + current_lang;
	window.location = location_str;
	return false;
}

function go_teach(){
	var location_str = './teach.php?lang=' + current_lang;
	window.location = location_str;
	return false;
}

function go_profile(){
	var location_str = './profile.php?lang=' + current_lang;
	window.location = location_str;
	return false;
}

function go_cart(){
	var location_str = './cart.php?lang=' + current_lang;
	window.location = location_str;
	return false;
}

function go_information(id){
	var location_str = './information.php?iid=' + id;
	window.location = location_str;
	return false;
}

function go_message(page){
	var location_str = './message.php?page=' + page;
	window.location = location_str;
	return false;
}

function go_download(doc){
	var location_str = './download.php?doc=' + doc;
	window.location = location_str;
	return false;
}

function go_enquiry(ino){
	var location_str = './check_order.php?ino=' + ino;
	window.location = location_str;
	return false;
}

function go_payment(){
	var location_str = './payment.php';
	window.location = location_str;
	return false;
}


function show_csr_photo(id){
	if(id == 0){
		var location_str = './csr.php?nid=3';
	} else {
		var location_str = './csr.php?nid=3&did=' + id;
	}
	window.location = location_str;
	return false;
}

function show_photo_detail(id){
	if(id == 0){
		var location_str = './csr.php?nid=5';
	} else {
		var location_str = './csr.php?nid=5&pid=' + id;
	}
	window.location = location_str;
	return false;
}

function back_index(){
	var location_str = './index.php';
	window.location = location_str;
	return false;
}

function prev_page(page){
	if(page == ''){
		history.go(-1);
	} else {
		var location_str = './index.php';
		window.location = location_str;
		return false;
	}
}

function my_confirm(text, f){
	if(confirm(text)){
		eval(f);
	}
}

function move_top(){
	$('html,body').animate({scrollTop:0}, 'slow');
}

function goTo(id){
   $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

var first_cart = true;
function show_cart(){
	if(cart_show == true){
		ch_display('floating_div', 'none');
		cart_show = false;
	} else {
		//ch_display('floating_div', 'block');
		ch_display_motion('floating_div', 500);
		cart_show = true;
		if(first_cart == true){
			renew_cart();
			first_cart = false;
		}
	}
}

function renew_cart(){
	$('div#floating_div').block({ 
        message: '<img src="./images/colorbox/loading.gif" />', 
        timeout : 2000
    });
	$.post('get_cart_ajax.php', function(data) {
		$('div#floating_div').html(data);
		$('div#floating_div').unblock();
	});
}

function ch_display(id, status){
	$('#' + id).css('display', status);
}

function ch_display_motion(id, time){
	$('#' + id).fadeIn(time);
}

function is_ie6(){
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}

function ch_font_size(id, size){
	$('#' + id).css('font-size', size + 'px');
}

function gen_link(str, href){
	return '<a href="' + href + '" target="blank">' + str + '</a>';
}

function gen_photo(src, title, a){
	var temp_str = '<img src="' + src + '" title="' + title + '" />';
	if(a == true){
		return gen_link(temp_str, src);
	}
	return temp_str;
}

function gen_moveup_button(id, text){
	$('body').append('<div class="moveup_button_div"><input class="moveup_button" type="button" id="' + id + '" value="' + text + '" /></div>');
	var obj = $('#' + id);
	obj.click(function(){
		move_top();
		obj.stop().animate({'opacity': 0 }, 500, 'linear');
	}).hover(function() {
		obj.stop().animate({'opacity': 1 }, 500, 'linear');
	}, function() { 
		obj.stop().animate({'opacity': 0 }, 500, 'linear');
	});
	$(window).scroll(function() {
		obj.css({
			'top': $(window).scrollTop() + $(window).height() - 100
		});
		if ($(window).scrollTop() > 200) {
			obj.stop().animate({'opacity': 1 }, 500, 'linear');
			obj.fadeIn(500);
		} else {
			obj.fadeOut(300);
		}
	});
}
