﻿function getObj(id) {
    return document.getElementById(id);
}

function clearNoNum(obj)
{
	//先把非数字的都替换掉，除了数字和.
	obj.value = obj.value.replace(/[^\d.]/g,"");
	//必须保证第一个为数字而不是.
	obj.value = obj.value.replace(/^\./g,"");
	//保证只有出现一个.而没有多个.
	obj.value = obj.value.replace(/\.{2,}/g,".");
	//保证.只出现一次，而不能出现两次以上

	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}

String.prototype.Trim = function()
{ 
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function showObj(id) {
    getObj(id).style.display = "";
}

function hideObj(id) {
    getObj(id).style.display = "none";
}

function changeClassName(obj, className) {
    obj.className = className;
}

function openWindow(page) {
    window.open(page, '', 'width=800, height=600, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no, status=no');
}

function openPage(page) {
    window.navigate(page);
}

function product_cat_onclick(id) {
    if (getObj(id + "_0").className == "product_cat_2") {
        hideAllProduceCat();
        changeClassName(getObj(id + "_0"), "product_cat_2_0");
    }
    else {
        hideAllProduceCat();
        changeClassName(getObj(id + "_0"), "product_cat_2");
    }
}

function hideAllProduceCat() {
    for (i = 1; i <= 15; i++) {
        if (getObj("c" + i + "_0") != null) {
            changeClassName(getObj("c" + i + "_0"), "product_cat_2");
        }
    }
}

function product_cat_onmouseover(id) {
	hideAllProduceCat();
	changeClassName(getObj(id + "_0"), "product_cat_2_0");
}

function product_sub_cat_onmouseover(id) {
	hideAllProduceCat();
	changeClassName(getObj(id), "product_cat_2_0");
}

function resizeImages(id,maxWidth){
	var imgs=getObj(id).getElementsByTagName("img");
	for(var i=0; i<imgs.length; i++)
	{
		if(imgs[i].width>maxWidth || imgs[i].style.width>maxWidth)
		{
			imgs[i].height=imgs[i].height*(maxWidth/imgs[i].width);
			imgs[i].style.height=imgs[i].height*(maxWidth/imgs[i].width);
			imgs[i].width=maxWidth;
			imgs[i].style.width=maxWidth;
		}
	}
}

function resizeTables(id,maxWidth){
	var tables=getObj(id).getElementsByTagName("table");
	for(var i=0; i<tables.length; i++)
	{
		if(tables[i].width>maxWidth || tables[i].style.width>maxWidth)
		{
			tables[i].height=imgs[i].height*(maxWidth/imgs[i].width);
			tables[i].style.height=imgs[i].height*(maxWidth/imgs[i].width);
			tables[i].width=maxWidth;
			tables[i].style.width=maxWidth;
		}
	}
}

function resizeImage_1(id,maxWidth,maxHeight){
	var obj=getObj(id);
	if(obj.width>maxWidth || obj.height>maxHeight || obj.style.width>maxWidth || obj.style.height>maxHeight){
		obj.width=maxWidth;
		obj.style.width=maxWidth;
		obj.height=maxHeight;
		obj.style.height=maxHeight;
	}	
}

function validator_1(){
	var name=document.getElementById("name");
	if(name.value.Trim()==""){
		alert("\"Product Name\" can not be empty!");
		return false;
	}
	var ord=document.getElementById("ord");
	if(ord.value.Trim()==""){
		alert("\"Order\" can not be empty!");
		return false;
	}
	if(!validateNumber(ord)){
		alert("\"Order\" must be a number!");
		return false;
	}
	var img=document.getElementById("img");
	if(img.value.Trim()==""){
		return confirm("You have not uploaded the \"Product Thumbnail\" !\nAre you sure to continue?");
	}
}

function validator_2(){
	var name=document.getElementById("name");
	if(name.value.Trim()==""){
		alert("\"Category Name\" can not be empty!");
		return false;
	}
	var ord=document.getElementById("ord");
	if(ord.value.Trim()==""){
		alert("\"Order\" can not be empty!");
		return false;
	}	
	if(!validateNumber(ord)){
		alert("\"Order\" must be a number!");
		return false;
	}
	var img1=document.getElementById("img1");
	if(img1.value.Trim()==""){
		return confirm("You have not uploaded the \"Banner Image\"!\nAre you sure to continue?");
	}
}

function validateNumber(target)
{
    if(target != null)
    {
        var objRe = /^[0-9]+(.[0-9]{2})?$/;
        var value = target.value.Trim() == "" ? "" : target.value.Trim();
        target.value = value;
        if(target.value == "")
        {
            return false;
        }
        if(!objRe.test(value))
        {            
            target.focus();
            return false;
        }
    }
    return true;
}