/**
 * @author weizhang
 */

String.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};

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

function isDigit (c)
{
   return ((c >= "0") && (c <= "9"))
}

function goBidongxi(component)
{
	var f = document.createElement('form'); 
	f.style.display = 'none'; 
	component.appendChild(f); 
	f.method = 'POST'; 
	f.action = 'http://ror.bidongxi.com';
	var s = document.createElement('input'); 
	s.setAttribute('type', 'hidden'); 
	s.setAttribute('name', 'user_cookie'); 
	s.setAttribute('value', getCookie('user')); 
	f.appendChild(s);f.submit(); 
	return false;
	//document.write("<form name=\"tobidongxi\" action=\"http://ror.bidongxi.com\" method=\"POST\"></form>");
	//document.forms["tobidongxi"].submit();
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}

function setCookie(theDocument,c_name,value,exdays,host)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + ((host==null) ? "" : "; host="+escape(host));	
	theDocument.cookie=c_name + "=" + c_value;
}

function loadimage(image) {
	ajaxGet ("/images/"+image+"?"+new Date().getTime());
}

function AJAXInteraction(url, callback, div_id) {

    var req = init();
    req.onreadystatechange = processRequest;
        
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          if (callback) callback(req.responseText, div_id);
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }
    
    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function ajaxGet (url) {
        // Obtain XMLHttpRequest object
        var xmlhttp = null;
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	    }
        if (!xmlhttp || xmlhttp == null) {
            alert('No browser XMLHttpRequest (AJAX) support');
            return;
        }
        // Setup optional async response handling via callback
        var async = false;
        xmlhttp.open('GET', url, async);

        xmlhttp.send(null);
        var resp = '';
        if (xmlhttp.status == 200) {
            resp = xmlhttp.responseText;
        }
        
        return resp;
}

function updateDiv(html, div_id) {
	var div_el = document.getElementById(div_id);
	
	div_el.innerHTML = html;
}

function getBrandPostList(category) {
	
	//var brands = ajaxGet ("/brand_hosts/ajax_get_post/all.json?category="+category);
	//var brand_top_div = document.getElementById("brand_top_list");
	var ai = new AJAXInteraction("/brand_hosts/ajax_get_post/all.json?category="+category, updateDiv, "brand_top_list");
	ai.doGet();
	
	//brand_top_div.innerHTML = brands;
}

function getBrandProductList(category) {
	
	//var brands = ajaxGet ("/brand_hosts/ajax_get_product/all.json?category="+category);
	//var brand_top_div = document.getElementById("brand_top_list");
	var ai = new AJAXInteraction("/brand_hosts/ajax_get_product/all.json?category="+category, updateDiv, "brand_top_list");
	ai.doGet();
	
	//brand_top_div.innerHTML = brands;
}

function getProductList(brand_id, count, width) {
	
	//var products = ajaxGet ("/products/ajax_get/all.json?brand_id="+brand_id+"&count="+count+"&width="+width);
	//var products_div = document.getElementById("products");
	var ai = new AJAXInteraction("/products/ajax_get/"+brand_id+"/"+count+"/"+width+"/0/all.json", updateDiv, "products");
	ai.doGet();
	
	//products_div.innerHTML = products;
}

function getProductSearchList(product_id, count, width) {
	
	//var products = ajaxGet ("/products/ajax_search/all.json?product_id="+product_id+"&count="+count+"&width="+width);
	//var products_div = document.getElementById("search_products");
	var ai = new AJAXInteraction("/products/ajax_search/all.json?product_id="+product_id+"&count="+count+"&width="+width, updateDiv, "search_products");
	ai.doGet();
	
	//products_div.innerHTML = products;
}

function getChangedProductList(count, width) {
	
	//var products = ajaxGet ("/products/ajax_price_changed/all.json?count="+count+"&width="+width);
	//var products_div = document.getElementById("products");
	var ai = new AJAXInteraction("/products/ajax_price_changed/all.json?count="+count+"&width="+width, updateDiv, "products");
	ai.doGet();
	
	//products_div.innerHTML = products;
}

function getPostList(brand_host_id, count) {
	
	//var posts = ajaxGet ("/posts/ajax_get/all.json?brand_host_id="+brand_host_id+"&count="+count);
	//var posts_div = document.getElementById("posts");
	var ai = new AJAXInteraction("/posts/ajax_get/"+brand_host_id+"/"+count+"/0/all.json", updateDiv, "posts");
	ai.doGet();
	
	//posts_div.innerHTML = posts;
}

function getBlogPostList(brand_id, count) {
	
	//var posts = ajaxGet ("/posts/ajax_get_blog/all.json?brand_id="+brand_id+"&count="+count);
	//var posts_div = document.getElementById("posts");
	var ai = new AJAXInteraction("/posts/ajax_get_blog/"+brand_id+"/"+count+"/0/all.json", updateDiv, "posts");
	ai.doGet();
	
	//posts_div.innerHTML = posts;
}

function getMostPostList(brand_id, count) {
	
	//var posts = ajaxGet ("/posts/ajax_get_blog/all.json?count="+count+"&width="+width);
	//var posts_div = document.getElementById("posts");
	var ai = new AJAXInteraction("/posts/ajax_get_blog/all.json?count="+count+"&width="+width, updateDiv, "posts");
	ai.doGet();
	
	//posts_div.innerHTML = posts;
}

function getUserBrandWatch(brand_id) {
	
	//var html = ajaxGet ("/products/ajax_get_user_brand_watch/all.json?brand_id="+brand_id);
	//var brand_watch = document.getElementById("brand_watch");
	var ai = new AJAXInteraction("/products/ajax_get_user_brand_watch/all.json?brand_id="+brand_id, updateDiv, "brand_watch");
	ai.doGet();
	
	//brand_watch.innerHTML = html;
}

function getUserBar() {
	
	//var html = ajaxGet ("/users/ajax_get_user_bar/all.json");
	var ai = new AJAXInteraction("/users/ajax_get_user_bar/all.json", updateDiv, "user_bar");
	ai.doGet();
}

function getNewsList(brand_id, count, width) {
	
	//var posts = ajaxGet ("/posts/ajax_get/all.json?brand_host_id="+brand_host_id+"&count="+count);
	//var posts_div = document.getElementById("posts");
	var ai = new AJAXInteraction("/news/ajax_get/"+brand_id+"/"+count+"/"+width+"/0/all.json", updateDiv, "news");
	ai.doGet();
	
	//posts_div.innerHTML = posts;
}

