﻿/* Global Variable */
var cookiePId = "ProvinceId";
var cookieCId = "CityId";
var cookieAId = "AreaId";
var cookiePName = "ProvinceName";
var cookieCName = "CityName";
var cookieAName = "AreaName";

var globalVar = 
{
    gProvinceId : getCookie(cookiePId),
    gCityId : getCookie(cookieCId),
    gAreaId : getCookie(cookieAId),
    gProvinceName : getCookie(cookiePName),
    gCityName : getCookie(cookieCName),
    gAreaName : getCookie(cookieAName)
}

if(globalVar.gCityId==null)
{
    globalVar.gProvinceId = 7;
    globalVar.gCityId = 39;
    globalVar.gAreaId = 0;
    globalVar.gProvinceName = "江苏省";
    globalVar.gCityName = "苏州市";
    globalVar.gAreaName = "";
}


// init province select object
var optionsProvince = document.createElement("SELECT");
optionsProvince.onchange = function(){initCity(this.value);}
var provinceFirstObj = document.createElement("OPTION");
provinceFirstObj.text="==省==";
provinceFirstObj.value=0;
optionsProvince.options.add(provinceFirstObj);

// init city select object
var optionsCity = document.createElement("SELECT");
optionsCity.onchange = function(){initArea(this.value);}
var cityFirstOption = document.createElement("OPTION");
cityFirstOption.text="==市==";
cityFirstOption.value=0;
optionsCity.options.add(cityFirstOption);

// init city select object
var optionsArea = document.createElement("SELECT");
var areaFirstOption = document.createElement("OPTION");
areaFirstOption.text="==区、县==";
areaFirstOption.value=0;
optionsArea.options.add(areaFirstOption);


/*  Common  */
var myGlobalHandlers =
{
    onCreate: function()
    {
        Element.show('loadingObj');
    },
    onComplete: function()
    {
        if(Ajax.activeRequestCount == 0)
        {
            Element.hide('loadingObj');
        }
    }
};
Ajax.Responders.register(myGlobalHandlers);

	
/*  Province  */
var provinceItems = [];

var ProvinceItem = Class.create();
ProvinceItem.prototype = {
    initialize:function(id,enname,cnname)
    {
        this.Id = id;
        this.EnName = enname;
        this.CnName = cnname;
        this.Item = getProvinceItemObj(id,cnname);
        this.Cities = null;
    }
};

function getProvinceResponse()
{
    var url = window.location.href;// '/webservice.aspx';
    var pars = 'o=province';
	
    var myAjax = new Ajax.Request(
        url, 
        {
	        method: 'get', 
	        parameters: pars, 
	        onComplete: fillProvinceContainer
        });		
}

function fillProvinceContainer(originalRequest)
{
    if(provinceItems.length==0)
        provinceItems = fillProvinceItems(originalRequest.responseText) ;

    for(i=0;i<provinceItems.length;i++)
        optionsProvince.options.add(provinceItems[i].Item);
}
function fillProvinceItems(strResponse)
{
    var items = [];
    var stritems = strResponse.split(';');
    
    for(i=0;i<stritems.length;i++)
    {
        if(stritems[i]=='')
            continue ;
            
        var stritem = stritems[i].split(',');
        var id = stritem[0].toString();
        var enname = stritem[1].toString();
        var cnname = stritem[2].toString();
        var item = new ProvinceItem(id,enname,cnname);
        
        items.push( item );
    }
    return items;
}

function getProvinceItemObj(id,cnname)
{
    var oOption = document.createElement("OPTION");
    oOption.text = cnname;
    oOption.value = id;
    
    return oOption;
}

/*  City  */
var cityItems = [];

var CityItem = Class.create();
CityItem.prototype = 
{
    initialize:function(id,enname,cnname)
    {
        this.Id = id;
        this.EnName = enname;
        this.CnName = cnname;
        this.Item = getCityItemObj(id,cnname);
        this.Areas = null;
    }
};


function initCity(pId)
{
    // init City
    optionsCity.selectedIndex = 0;
    while(optionsCity.options.length!=1)
    {
        optionsCity.options[1].remove();
    }
    // init Area
    optionsArea.selectedIndex = 0;
    while(optionsArea.options.length!=1)
    {
        optionsArea.options[1].remove();
    }
    
    var url = window.location.href;// '/webservice.aspx';
    var pars = 'o=city&provinceId='+pId;
	
    var myAjax = new Ajax.Request(
        url, 
        {
	        method: 'get', 
	        parameters: pars, 
	        onComplete: fillCityContainer
        });
}

function fillCityContainer(originalRequest)
{
    var index = optionsProvince.selectedIndex-1;
    if(provinceItems[index].Cities==null)
    {
        cityItems = fillCityItems(originalRequest.responseText) ;
        provinceItems[index].Cities = cityItems;
    }
    else
        cityItems = provinceItems[index].Cities;	        
    
    for(i=0;i<cityItems.length;i++)
    {
        optionsCity.options.add(cityItems[i].Item);
    }
}

function fillCityItems(strResponse)
{
    var items = [];
    var stritems = strResponse.split(';');
    
    for(i=0;i<stritems.length;i++)
    {
        if(stritems[i]=='')
            continue;
            
        var stritem = stritems[i].split(',');
        var id = stritem[0].toString();
        var enname = stritem[1].toString();
        var cnname = stritem[2].toString();
        var item = new CityItem(id,enname,cnname);
        
        items.push( item );
    }
    return items;
}

function getCityItemObj(id,cnname)
{
    var oOption = document.createElement("OPTION");
    oOption.text = cnname;
    oOption.value = id;
    
    return oOption;
}

/*  Area  */	
var areaItems = [];

var AreaItem = Class.create();
AreaItem.prototype = 
{
    initialize:function(id,enname,cnname)
    {
        this.Id = id;
        this.EnName = enname;
        this.CnName = cnname;
        this.Item = getAreaItemObj(id,cnname);
    }
};


function initArea(cId)
{
    optionsArea.selectedIndex = 0;
    while(optionsArea.options.length>1)
    {
        optionsArea.options[1].remove();
    }
    
    var url = window.location.href;// '/webservice.aspx';
    var pars = 'o=area&cityId='+cId;
	
    var myAjax = new Ajax.Request(
        url, 
        {
	        method: 'get', 
	        parameters: pars, 
	        onComplete: fillAreaContainer
        });
}

function fillAreaContainer(originalRequest)
{
    var index = optionsCity.selectedIndex-1;
    if(cityItems[index].Areas==null)
    {
        areaItems = fillAreaItems(originalRequest.responseText) ;
        cityItems[index].Areas = areaItems;
    }
    else
        areaItems = cityItems[index].Areas;	        
    
    for(i=0;i<areaItems.length;i++)
    {
        optionsArea.options.add(areaItems[i].Item);
    }
}

function fillAreaItems(strResponse)
{
    var items = [];
    var stritems = strResponse.split(';');
    
    for(i=0;i<stritems.length;i++)
    {
        if(stritems[i]=='')
            continue;
            
        var stritem = stritems[i].split(',');
        var id = stritem[0].toString();
        var enname = stritem[1].toString();
        var cnname = stritem[2].toString();
        var item = new AreaItem(id,enname,cnname);
        
        items.push( item );
    }
    return items;
}

function getAreaItemObj(id,cnname)
{
    var oOption = document.createElement("OPTION");
    oOption.text = cnname;
    oOption.value = id;
    
    return oOption;
}

/* Location Name */
var localName = null;
window.setTimeout("getlocalNameObj()",100);
function getlocalNameObj()
{
    localName = $("localName");    
    if(localName==null)
        window.setTimeout("getlocalNameObj()",100);
    else
        fillLocalName();
}
function fillLocalName()
{
    if(globalVar.gAreaName!=null && globalVar.gAreaName!="")
        localName.innerHTML = globalVar.gCityName + "，" + globalVar.gAreaName;
    else
        localName.innerHTML = globalVar.gCityName;
        
    localName.innerHTML += "<br />[切换城区]";
}

/* Change Location */
function showlocation()
{
    $("localcontainer").style.display='inline';
    initChangeLocalObj();
}
function changelocal()
{    
    if(optionsProvince.value==0 || optionsCity.value==0)
    {
        alert("省、市为必选项！");
        return ;
    }
    
    var provinces = $A(optionsProvince.options);
    var protxt = "";
    var provalue = 0;
    provinces.each(function(node){if(node.selected){protxt=node.text;provalue=node.value;return;}});
    
    var cities = $A(optionsCity.options);
    var citytxt = "";
    var cityvalue = 0;
    cities.each(function(node){if(node.selected){citytxt=node.text;cityvalue=node.value;return;}});    
    
    var areatxt = "";
    var areavalue = 0;
    if(optionsArea.value!=0)
    {
        var areas = $A(optionsArea.options);
        areas.each(function(node){if(node.selected){areatxt=node.text;areavalue=node.value;return;}});
    }        
    
    //Fill Location Info To Cookie
    var expireCookieDate = new Date(2266,6,6);
    setCookie(cookiePId, provalue, expireCookieDate);
    setCookie(cookieCId, cityvalue, expireCookieDate);
    setCookie(cookieAId, areavalue, expireCookieDate);
    setCookie(cookiePName, protxt, expireCookieDate);
    setCookie(cookieCName, citytxt, expireCookieDate);
    setCookie(cookieAName, areatxt, expireCookieDate);
    
    //Change Global Variables
    globalVar.gProvinceId = getCookie(cookiePId);
    globalVar.gCityId = getCookie(cookieCId);
    globalVar.gAreaId = getCookie(cookieAId);
    globalVar.gProvinceName = getCookie(cookiePName);
    globalVar.gCityName = getCookie(cookieCName);
    globalVar.gAreaName = getCookie(cookieAName);
    
    //Refresh the page items.
    window.location.href = window.location.href;
        
    /*
    //Change Page Location Display Text
    initLocalImg();
    //Refresh page content    
    $("localcontainer").hide();
    $("addinfo").hide();
    */
}

function initChangeLocalObj()
{
    var container = $("localcontainer");
    while(container.childNodes.length>0)
        container.removeChild(container.childNodes[0]);
        
    if(optionsProvince.options.length==1)
        getProvinceResponse();
    // Location
    container.appendChild(optionsProvince);   
    container.appendChild(optionsCity); 
    container.appendChild(optionsArea);
    // confirm
    var confirm = document.createElement("SPAN");
    confirm.onclick = function(){changelocal();};
    confirm.className = "confirm";
    confirm.innerHTML = "确定";
    container.appendChild(confirm);
    
    // cancel
    var cancel = document.createElement("SPAN");
    cancel.onclick = function(){hideParent(this);};
    cancel.className = "cancel";
    cancel.innerHTML = "取消";
    container.appendChild(cancel);
    
    // comment
    var comment = document.createElement("SPAN");
    comment.className = "comm";
    comment.innerHTML = "说明：“省、市”为必选项，“区、县”为可选项！";
    container.appendChild(comment);
}

/* Add Information */
    var optionsGroup = document.createElement("SELECT");
    optionsGroup.onchange = function(){initCategoryOptions(this.value);}
    var firstGroupOption = document.createElement("OPTION");
    firstGroupOption.text="==选择栏目==";
    firstGroupOption.value=0;
    optionsGroup.options.add(firstGroupOption);
    
    var optionsCategory = document.createElement("SELECT");    
    var firstCOption = document.createElement("OPTION");
    firstCOption.text="==选择子栏目==";
    firstCOption.value=0;
    optionsCategory.options.add(firstCOption);
    
function showAddInfo()
{
    var container = $("addinfo");
    while(container.childNodes.length>0)
        container.removeChild(container.childNodes[0]);

    // Location
    if(optionsProvince.options.length==1)
        getProvinceResponse();
    var localContainer = document.createElement("DIV");
    localContainer.innerHTML = "城&nbsp;&nbsp;&nbsp;&nbsp;区：";
    localContainer.appendChild(optionsProvince);   
    localContainer.appendChild(optionsCity); 
    localContainer.appendChild(optionsArea);
    container.appendChild(localContainer);
    // Category
    if(optionsGroup.options.length==1)
        initGroupOptions();
    var cateContainer = document.createElement("DIV");
    cateContainer.innerHTML = "分&nbsp;&nbsp;&nbsp;&nbsp;类：";
    cateContainer.appendChild(optionsGroup);
    cateContainer.appendChild(optionsCategory);
    container.appendChild(cateContainer);
    // Title
    var tleContainer = document.createElement("DIV");
    tleContainer.innerHTML = "标&nbsp;&nbsp;&nbsp;&nbsp;题：";
    var txttitle = document.createElement("INPUT");
    txttitle.type = "text";
    txttitle.style.width = "320px";
    tleContainer.appendChild(txttitle);
    container.appendChild(tleContainer);
    // Keyword
    var kwContainer = document.createElement("DIV");
    kwContainer.innerHTML = "关键字：";
    var txtkeyword = document.createElement("INPUT");
    txtkeyword.type = "text";
    txtkeyword.style.width = "320px";
    kwContainer.appendChild(txtkeyword);
    container.appendChild(kwContainer);
    // Content
    var txtcontent = document.createElement("TEXTAREA");
    txtcontent.style.width = "400px";
    txtcontent.style.height = "300px";
    txtcontent.style.border = "1px dashed #000";
    container.appendChild(txtcontent);
    
    // confirm
    var confirm = document.createElement("SPAN");
    confirm.onclick = function(){submitContent(txttitle,txtkeyword,txtcontent);};
    confirm.className = "confirm";
    confirm.innerHTML = "提交";
    container.appendChild(confirm);
    
    // cancel
    var cancel = document.createElement("SPAN");
    cancel.onclick = function(){hideParent(this);};
    cancel.className = "cancel";
    cancel.innerHTML = "关闭";
    container.appendChild(cancel);
    
    container.style.display = "inline";
}
function initGroupOptions()
{    
    for(i=0;i<groupItems.length;i++)
        optionsGroup.options.add(groupItems[i].Option);
    
    return optionsGroup;
}
function getCategoryOptions(groupId)
{
    if(groupId==0)
        return optionsCategory;
    
    var groupItem = getGroupById(groupId);
    if(groupItem.Categories==null)
    {
        setTimeout(function(){getCategoryOptions(groupId);},400);
        return ;
    }
    
    while(optionsCategory.childNodes.length>1)
        optionsCategory.removeChild(optionsCategory.childNodes[1]);
    for(i=0;i<groupItem.Categories.length;i++)
        optionsCategory.options.add(groupItem.Categories[i].Option);
}
function initCategoryOptions(groupId)
{
    var groupItem = getGroupById(groupId);
    if(groupItem.Categories==null)
        attachCategoryData(groupId);
        
    getCategoryOptions(groupId);
}
function submitContent(txttitle,txtkeyword,txtcontent)
{
    var cityId = optionsCity.value;
    var areaId = optionsArea.value;
    if(areaId == 0)
    {
        alert("请选择信息所属城区");
        return ;
    }
    var cateId = optionsCategory.value;
    if(cateId == 0)
    {
        alert("请选择信息所属分类");
        return ;
    }
    var title = txttitle.value;
    var keyword = txtkeyword.value;
    var content = txtcontent.value;
    while(content.indexOf("\r\n")>-1)
        content = content.replace("\r\n","<br />");
    
    var url = window.location.href;// '/webservice.aspx';
    var pars = 'o=submit&cityid='+cityId+'&areaid='+areaId+'&cateid='+cateId+'&title='+title+"&keyword="+keyword+"&content="+content;
	
    var myAjax = new Ajax.Request(
        url, 
        {
	        method: 'get', 
	        parameters: pars,
	        onComplete: function(response)
	        {
	            alert(response.responseText);
	        }
        });
}

/* Common Functions */
function hideParent(obj)
{
    obj.parentNode.style.display='none';   
}