﻿function Ajax(url)
{
	var m_xmlReq=false;
	var m_OnSucceed=function(){};
	var m_OnReading=function(){};
	var m_timer=null;
	
	this.Url=url;

	if(window.XMLHttpRequest)
	{
		m_xmlReq = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{ 
	   try {m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP'); }catch(e)
	   {
		  try { m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
		}
	}

	this.OnReading=function(reading)
	{
		m_OnReading=reading;
	}

	this.OnSucceed=function(succeed)
	{
		m_OnSucceed=succeed;
	}

	this.OnTimeout=function(timeout)
	{
		m_timer=window.setTimeout(timeout,10000);
	}

	this.invokeServer=function(send_data,method)
	{
		if(!m_xmlReq)  return;
		m_xmlReq.open(method,this.Url,true);
		if(method=='POST') 
		m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');  
		m_xmlReq.onreadystatechange=function()
		{
					if(m_xmlReq.readyState == 1)
					{
						m_OnReading();
					}
					else if(m_xmlReq.readyState == 4 && m_xmlReq.status==200)
					{
						var result=null;
						try{
						    eval("result="+m_xmlReq.responseText);
						    m_OnSucceed(result);
						}
						catch(e){}
						if(m_timer!=null) window.clearTimeout(m_timer);
					}
		}
		m_xmlReq.send(send_data.toString());
	}
}

function $(id)
{
	return document.getElementById(id);
}

function $F(id)
{
	return document.getElementById(id).value.trim();
}

var _UNDEFINED_="undefined";
function Hashtable() 
{ 
	this.count=0;
	this.content= new Object(); 
	this.defined=function(p)
	{
		return typeof(p)!=_UNDEFINED_;
	}
	this.add = function(key,value)
	{ 
		if(this.contains(key))	return false; 

		this.content[key]=value;
		this.count++;
		return true; 			
    } 
	this.remove= function(key)
	{
		if(!this.contains(key))	return;

		delete this.content[key];
		this.count--;
	} 
	this.items  = function(key)
	{
		if(this.contains(key))	return this.content[key];

		return null;
	} 
	this.contains= function(key)
	{ 
		return this.defined(this.content[key]);
	} 
	this.clear= function()
	{
		for(var k in this.content)	delete this.content[k];
		this.count=0;
	}
	
	this.toString=function()
	{
	    var sb=new StringBuilder();
	    var hasItem=false;
	    
	    for(var k in this.content)
	    {
	        if(hasItem)
	            sb.appendFormat("&{0}={1}",k,this.content[k]);
	        else
	        {
	            sb.appendFormat("{0}={1}",k,this.content[k]);
	            hasItem=true;
	        }
	    }
	    
	    return sb.toString();
	}
}

function StringBuilder(value)
{
	this.s = new Array();
	

	this.append=function(value)
	{
		if(value!=null)
			this.s.push(value);
		return this;
	}
	
	this.append(value);

	this.toString=function()
	{
		return this.s.join('');
	}
	
	this.clear=function()
	{
		this.s=new Array();
	}

	this.appendFormat=function(format,args)
	{
			for ( var i=0 ; i < format.length-1;)
			{
				if ( format.charAt(i) == '{' && format.charAt(i+1) != '{' )
				{
					var index = 0, indexStart = i+1;
					for ( var j=indexStart ; j <= format.length-2 ; ++j )
					{
						var ch = format.charAt(j);
						if ( ch < '0' || ch > '9' ) break;
					}
					if ( j > indexStart )
					{
						if ( format.charAt(j) == '}' && format.charAt(j+1) != '}' )
						{
							 for ( var k=j-1 ; k >= indexStart ; k-- )
							 {
								 index += (format.charCodeAt(k)-48)*Math.pow(10, j-1-k);
							 }  
							var swapArg = arguments[index+1];
							this.append(swapArg);
							i += j-indexStart+2;
							continue;
						}
					}
					this.append(format.charAt(i));
					i++;
				}
				else
				{
					if ( ( format.charAt(i) == '{' && format.charAt(i+1) == '{' ) || ( format.charAt(i) == '}' && format.charAt(i+1) == '}' ) )
					{
						i++
					}
					this.append(format.charAt(i));
					i++;
				}
			}
			this.append(format.substr(i));
			return this; 
	}
}

function DrawImage(img,width,height){
if(width==null) width=70;
if(height==null) height=110;
var image=new Image(); 
image.src=img.src;
	if(img.height>height)
	{
		rate=image.width/image.height;
		newWidth=rate*height;
		//img.height=height;
		//img.width=newWidth;
	}
	if(image.width>0 && image.height>0)
	{
	  	rate=image.height/image.width;
	  	newHeight=rate*width;
	  //	img.width=width;
	  //	img.height=newHeight;
	}
}

function setCookie (name, value, days) {
  var expires = new Date();
  expires.setTime (expires.getTime() + (86400 * 1000 * days));
  document.cookie = name + "=" + escape(value) +   "; expires=" + expires.toGMTString() +  "; path=/";
}

function getCookie(name) {
  var search;
  search = name + "="
  offset = document.cookie.indexOf(search) 
  if (offset != -1) {
    offset += search.length ;
    end = document.cookie.indexOf(";", offset) ;
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}

function deleteCookie(name) {
  var expdate = new Date();
  expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
  setCookie(name, "", expdate);
}

function getposOffset(what, offsettype)
{ 
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop; 
    var parentEl=what.offsetParent; 
    while (parentEl!=null)
    { 
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop; 
         parentEl=parentEl.offsetParent; 
    } 
    return totaloffset; 
} 


function AddPDNothing(ProdID)
{
	var url='http://misc.dangdang.com/lackproduct/pdnothing.aspx?pdname='+ProdID;   
    
    var width=448;
    var height=227;
    var w = 1024;
    var h = 768;
    if (document.all || document.layers)
    {
              w = screen.availWidth;
              h = screen.availHeight;
    }
    var leftPos = (w/2-width/2);
    var topPos = (h/2.3-height/2.3);
    var ew=window.open(url,"","width="+width+",height="+height+",top="+topPos+",left="+leftPos);
}
