var qwin;
var had_popup=0;

function SetCookie(sName, sValue)
{
  date = new Date((new Date()).getTime() + 31536000000);
  document.cookie = sName + '=' + escape(sValue) + '; expires=' + date.toGMTString() + '; domain=.sina.com; path=/';
}

function SetCookieWHour(sName, sValue, iHour)
{
  date = new Date((new Date()).getTime() + 3600000 * iHour);
  document.cookie = sName + '=' + escape(sValue) + '; expires=' + date.toGMTString() + '; domain=.sina.com; path=/';
}

function touchCookie( CookieName )
{
  var sValue = GetCookie(CookieName);
  SetCookie(CookieName, sValue);
}

function SetSubCookie(CookieName, sName, sValue)
{
  var alreadyset = 0;
  var sCookieValue = GetCookie(CookieName);
  if (sCookieValue != null) {
    var aSubCookie = sCookieValue.split('&');
    for (var i=0; i< aSubCookie.length; i++) {
      var aCrumb = aSubCookie[i].split('=');
	  if (sName == aCrumb[0]) {
	  	if (i==0) {
	  		sCValue = sName + '=' + sValue;
			alreadyset = 1;
		} else {
			sCValue = sCValue + '&' + sName + '=' + sValue;
			alreadyset = 1;
		}
	  } else {
	  	if (i==0)
	  		sCValue = aCrumb[0] + '=' + aCrumb[1];
		else
			sCValue = sCValue + '&' + aCrumb[0] + '=' + aCrumb[1];
	  }
    }
    if (alreadyset == 0) sCValue = sCValue + '&' + sName + '=' + sValue;
    SetCookie(CookieName, sCValue);
  } else {
  	SetCookie(CookieName, sName + '=' + sValue);
  }
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split('; ');
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split('=');
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function GetSubCookie(CookieName, sName)
{
  var sValue = GetCookie(CookieName);
  if ( sValue != null ) {
    var aSubCookie = sValue.split('&');
    for (var i=0; i< aSubCookie.length; i++) {
      var aCrumb = aSubCookie[i].split('=');
	  if (sName == aCrumb[0]) return(aCrumb[1]);
    }
  }
  return null;  
}

// below are sina's related cookies
function submitLocation()
{
	idx = qwin.document.fm.city.selectedIndex;
	SetSubCookie('sina_public_clr', 'area_code', qwin.document.fm.city.options[idx].value);
	SetSubCookie('sina_public_clr', 'area_name', qwin.document.fm.mapping_name.value);
	had_popup=1;
	return true;
}

function closeQWin()
{
//	if(had_popup == 1) window.location.reload();
}

function PopupQuestion()
{
	qwin = window.open('http://home.sina.com/question2.html?host='+document.location.hostname,'questionwin', 'toolbar=no,left=300,height=283,width=420,status=no,scrollbars=no,resizable=yes,menubar=no');
}

//added on 02/13/02 by Brandon. it checks the premium status
function checkPremium() {
	if (GetCookie('SinaPay') == null && GetCookie('spai') != 1) {
		return 1;
	} else {
		return 0;
	}
}

function popupMessage(ckName, earlyDate, hr, pwinURL, pwinName, pwinW, pwinH)
{
        var ldate = new Date();
        var sDate, ckYear = sDate = ldate.getYear() + '';
        sDate = sDate.substr(sDate.length - 2) + '/' + (ldate.getMonth()+1) + '/' + ldate.getDate();
        ckYear = ckYear.substr(ckYear.length - 2);
        var sValue = GetCookie(ckName);
        if (sValue == earlyDate) SetCookieWHour(ckName, sDate, hr);
        if (sValue == null) {
                SetCookieWHour(ckName, sDate, hr);
				if (checkPremium()) winOpen(pwinURL, pwinName, pwinW, pwinH, 'yes', 'no');
        }
}

function winOpen(pURL, pName, pW, pH, pBar, pFlex, pLeft, pTop) {
	if(pLeft == null) pLeft = 0;
	if(pTop == null) pTop = 0;
	window.open(pURL, pName, 'toolbar='+pBar+',width='+pW+',height='+pH+',status='+pBar+',scrollbars='+pFlex+',resizable='+pFlex+',menubar='+pBar+',left='+pLeft+',top='+pTop);
}

