// popup feedback form
function feedbackform() {

    dest= "http://prog.real.com.sg/feedback_taiwan.html";
    if (popupsz()) { w=450; h=500; }
    else { w=380; h=390; }
    window.open(dest,"feedback","top=150,left=150,height="+h+",width="+w+",status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

//-----------------------------------------------------------------------
// downloadrp login LOGO
function downloadrp(){
        
    url = "http://guide.taiwan.real.com/login/v3_trad/downloadplayer.htm";
    if (popupsz()) { w=404; h=480; }
    else { w=380; h=400; }

    window.open(url,"downloadrp","top=150,left=150,height="+h+",width="+w+",status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
}

function loginOpen(mvid){

        //url = "/login/logincheck.html"; 
	//window.open(url,null,"top=150,left=150,height=360,width=480,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");

// get domain
url = document.location;
url = url.toString();
dnm1 = url.indexOf("http://") + 7;
dnm2 = url.indexOf("real.com") + 8;
dnm = url.substring(dnm1,dnm2);
// pass movie id
mv1 = url.indexOf('?id=') + 4;
mv2 = url.indexOf('&pcode');
m = url.substring(mv1,mv2);
var valid = "0123456789";
for (var i=0; i<m.length; i++) {
    temp = "" + m.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") m=0;
}
if (m == 0 && mvid ) m = mvid;

url = "http://login.sg.real.com/taiwan/login/index.html?ref="+dnm+"&m="+m;
if (popupsz()) { w=404; h=480; }
else { w=380; h=400; }
window.open(url,"login","top=150,left=150,height="+h+",width="+w+",status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");

}

// launch RP
function rpOpen(S){

    var agt=navigator.userAgent.toLowerCase();
    var ie  = (agt.indexOf("msie") != -1);
    var rp  = (agt.indexOf("r1") != -1);
    if (ie || rp)
	document.RealOneActiveXObject.PlayClip(S);
    else
	location.href=S;
}

//--------------------------
// clear cookies for logout
function logout()
{
    if (confirm('您确定要退出登陆？ \n退出登陆将会使您无法使用高级订购内容，功能以及个性化服务。 \n要使用高级订购内容及功能，您需要重新登陆。')){

    var uds=0;
    var cookies = document.cookie.split('; ');
    for (var i=0; i<cookies.length; i++) {
        cookies[i] = cookies[i].split('=');
        var cookieName = cookies[i][0];
        var cookieValue = cookies[i][1];
        if (cookieName.indexOf('R1EuropeAuth_UDS') == 0 || cookieName.indexOf('RNPlayer') == 0) uds =1;

        if((cookieName.indexOf('R1EuropeAuth_') == 0 && cookieName != 'R1EuropeAuth_langloc')|| (cookieName.indexOf('RN') == 0 && cookieName != 'RNGuide')){
            document.cookie= cookieName+'=; path=/; domain=.real.com; expires=;';
        }
    }

    issafari=false;
    if (((navigator.userAgent).indexOf("Macintosh")!=-1)&&((navigator.userAgent.match(/Safari/ig))||(navigator.userAgent.match(/Mozilla/ig)))){
        issafari=true
    }

    if (issafari){
        clearPlayerAuthUrl(1);

    }else if (uds){
        clearPlayerAuthUrl(2);    
    }

    alert('您已经退出登陆。');
    setTimeout('window.location.reload();',2000);
}
}

//-----------------
// call by logout(), Clear RealPlayer cookies
function clearPlayerAuthUrl(id){

    var playerAuthUrl = ['a=removeallcookies'];
    if (id ==1)
        playerAuthUrl = 'http://login.sg.real.com/inc/logout_hk.ram?'+playerAuthUrl.join('&');
    else if (id==2)
        playerAuthUrl = 'http://login.sg.real.com/inc/logout_hk.ram?'+playerAuthUrl.join('&');

    var playerAuthDIV = document.getElementById('clearplayerAuthDIV_ID');
    if (playerAuthDIV) Element.remove(playerAuthDIV);
    playerAuthDIV = document.createElement('div');
    playerAuthDIV.id = 'clearplayerAuthDIV_ID';
    playerAuthDIV.setAttribute('style','position:absolute;right:0;top:0;overflow:hidden;height:0;width:0;margin:0;padding:0;');
    playerAuthDIV.innerHTML = '<OBJECT CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" WIDTH=0 HEIGHT=0><PARAM NAME="SRC" VALUE="'+playerAuthUrl+'"><PARAM NAME="AUTOSTART" VALUE="true"></OBJECT><embed src="'+playerAuthUrl+'" type="audio/x-pn-realaudio-plugin" width="0" height="0" autostart="true" />';

    if (document.body) document.body.appendChild(playerAuthDIV);

}

//-----------------------------------------------------------------------
// if user does not install Player
function download_warn(){
    issafari=false;
    if (((navigator.userAgent).indexOf("Macintosh")!=-1)&&((navigator.userAgent.match(/Safari/ig))||(navigator.userAgent.match(/Mozilla/ig)))){
        issafari=true
    }

    if (issafari){
	url = "https://globalstore.real.com/rn/1014/zh/tw/?cpath=CNT&pcode=CNT&src=downloadrp_warning&rsrc=downloadrp_warning";
//	url = "https://order.real.com/pt/order.html?ppath=cpsptw101607a&language=TW&country=TW&src=downloadrp_warning&rsrc=downloadrp_warning&cpath=CNT";
    }else{
	url = "http://tw.real.com/?rsrc=downloadrp_warning&src=downloadrp_warning&cpath=CNT";
    }
    window.open(url,null,"top=150,left=150,height=550,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");

}

//------------------------------------------------------------------------------------------------



function getCookieVal (offset)
   {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
   }

function GetCookie (name)
   {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen)
      {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
      }
   return null;
   }

function getValue(c,v){
        if(c.indexOf(v) < 0) return "";
        var cV = new RegExp(".*" + v +"=([^&]*)(&|$).*");
        return c.replace(cV,"$1");
}

function getCookieValue( cookieName ){
        var cookieArray = new Array();
        var cRE = new RegExp("(\;|^)[^;]*("+cookieName+")\=([^;]*)(;|$)");
        cookieArray = cRE.exec(document.cookie);
        if( cookieArray != null ){
                return cookieArray[3];
        }
        return "";
}

function SetCookie (name, value)
{
    var argv    = SetCookie.arguments;
    var argc    = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path    = (argc > 3) ? argv[3] : null;
    var domain  = (argc > 4) ? argv[4] : null;
    var secure  = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
        ((expires == null)  ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null)     ? "" : ("; path=" + path)) +
        ((domain == null)   ? "" : ("; domain=" + domain)) +
        ((secure == true)   ? "; secure" : "");
   }

function deleteCookie(name, path, domain) {
    SetCookie(name,"",-1,path,domain);
}


if(GetCookie('RNGuide') == null) {
    var now     = new Date();
    var random  = Math.round(1000+8999*Math.random());
    var value   = Math.round(now.getTime()) + '' + random;
    var expdate = new Date ();
    expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365 * 10));
    var path    = "/";

    var name    = 'RNGuide';
    var domain  = ".taiwan.real.com";
    SetCookie(name,value,expdate,path,domain,false);

    var name    = 'RNGuide2';
    var domain  = ".real.com";
    SetCookie(name,value,expdate,path,domain,false);

} else if (GetCookie('RNGuide2') == null) {
    var value   =  GetCookie('RNGuide');
    var expdate = new Date ();
    expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365 * 10));
    var path    = "/";

    var name    = 'RNGuide2';
    var domain  = ".real.com";
    SetCookie(name,value,expdate,path,domain, false);
}



if (cookie = getCookieValue("RNPlayer")){
    var en  = getValue(cookie,"SN");
    var cn  = getValue(cookie,"CN");
    var uid = getValue(cookie,"CI");
    //alert("line 236 - en="+en+",cn="+cn+",uid="+uid);

}else if (GetCookie("R1EuropeAuth_UDS")){
    cont = GetCookie("R1EuropeAuth_UDS");
    enconts = cont.split(':');
    en="";
    for (var i=0; i<enconts.length; i++) {
        var temp = new Array();
        if (enconts[i].indexOf('@') != -1) {
            temp = enconts[i].split('@'); ens = temp[1].split('/'); }
        else {
            temp = enconts[i].split('%40'); ens = temp[1].split('%2F'); }

        uid = temp[0];
        en += ens[0]+"|";
    }
    en = en.substr(0,en.length-1);
    //alert("line 254 - en="+en+",cn="+cn+",uid="+uid);

} else {
    var uid = '';
    var en  = '';
    var cn  = '';
}

//alert(document.cookie);
rid = GetCookie('RNGuide') ? GetCookie('RNGuide') : GetCookie('RNGuide2');


function update_warning() {

// detect RealPlayer
var agt=navigator.userAgent.toLowerCase();
var ie  = (agt.indexOf("msie") != -1);
var ns  = (navigator.appName.indexOf("Netscape") != -1);
var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
var mac = (agt.indexOf("mac")!=-1);

    if (ie && win) {
        pluginlist = detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer");
    }
    if (ns || !win) {
        nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
        pluginlist = detectNS("audio/x-pn-realaudio-plugin","RealPlayer");
    }

    pluginlist += navigator.javaEnabled() ? "Java," : "";
    if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);

    if (pluginlist.indexOf("RealPlayer")!=-1){
        //player exists
        document.getElementById("warning_container").style.display = "none";
    }
}

function detectIE(ClassID,name) {
    result = false;
    document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n');
    if (result) return name+','; else return '';
}

function detectNS(ClassID,name) {
    n = ""; if (nse.indexOf(ClassID) != -1) if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; return n;
}

// use in index
function update_guide(){

    var cookies = document.cookie.split('; ');
    var uds = 0; var userlogin = 0;
    for (var i=0; i<cookies.length; i++) {
        cookies[i] = cookies[i].split('=');
        var cookieName = cookies[i][0];
        var cookieValue = cookies[i][1];

        if ((cookieName.indexOf('R1EuropeAuth_') == 0 && cookies[i][1]!='') || (cookieName.indexOf('RNPlayer') ==0 && cookies[i][1])){
             userlogin = 1;
        }

        if ((cookieName.indexOf('R1EuropeAuth_UDS') == 0 && cookies[i][1] != '')) { uds = 1; }
        else if ((cookieName.indexOf('RNPlayer') == 0 && cookies[i][1]) != ''){
            useren = '';
            if (cookie = getCookieValue("RNPlayer")){ useren  = getValue(cookie,"SN"); }
            if (useren) uds = 1;
        }

    }
    if(userlogin){
        document.getElementById("login_module").style.display = "none";
        document.getElementById("logout_module").style.display = "block";
    }
    else{
        document.getElementById("login_module").style.display = "block";
        document.getElementById("logout_module").style.display = "none";
    }

    if (uds) {
        document.getElementById("highlight_container").style.display = "none";
        document.getElementById("highlight_left").style.display = "none";
        document.getElementById("highlight_right").style.display = "none";

        document.getElementById("highlight_container_psub").style.display = "block";
    }
    else
    {
        document.getElementById("highlight_container").style.display = "block";
        document.getElementById("highlight_left").style.display = "block";
        document.getElementById("highlight_right").style.display = "block";


        document.getElementById("highlight_container_psub").style.display = "none";
    }
}

// use in genre and synopsis
function update_login(){
    var cookies = document.cookie.split('; ');
    var uds = 0;
    for (var i=0; i<cookies.length; i++) {
        cookies[i] = cookies[i].split('=');
        var cookieName = cookies[i][0];
        var cookieValue = cookies[i][1];
        if ((cookieName.indexOf('R1EuropeAuth_') ==0 && cookies[i][1]!='') || (cookieName.indexOf('RNPlayer') ==0 && cookies[i][1])){
             uds =1;
        }
    }

    if(uds){
        document.getElementById("login_module").style.display = "none";
        document.getElementById("logout_module").style.display = "block";

    }else
    {
        document.getElementById("login_module").style.display = "block";
        document.getElementById("logout_module").style.display = "none";

    }
}

function chksearch()
{
    if (search.query.value == "" || search.query.value == "Search for Content") search.query.value = "all";
}

//-----------------------------------------------------------------------
// call by Main page - new arrival, coming soon and others div
function moviepage(url,div){

    if (!div){
        div="unknown";
    }
    // .htm page
    if (url.indexOf('.htm') != -1){
            window.location=url+"?"+div;
    // .php page
    }else{
        window.location=url;
    }
}

function popupsz()
{
    var platform = '';
    if (typeof(window.navigator.platform) != 'undefined')
    {
        platform = window.navigator.platform.toLowerCase();
        if (platform.indexOf('mac') != -1)
        {
            var agt=navigator.userAgent.toLowerCase();
            var macrp = (agt.indexOf("realplayer") != -1);
            var rpver11 = (agt.indexOf("11.") != -1);
            var rpver10 = (agt.indexOf("10.") != -1);

            if (macrp && (rpver11||rpver10))
            {
                //alert("macrp");
                return 0;
            }
        }
    }
    // win & mac safari
    return 1;
}

//-----------------------------------------------------------------------
// traffic driver - set cookie RNMKT
function chk_ipm()
{
    url = document.location;
    url = url.toString();
//    url = url.toLowerCase();

// main page and video page need to drop mkt cookie
//    if (url.indexOf("synopsis") != -1)
//    {
        pos1 = url.indexOf("pcode=");
        pos2 = url.indexOf("cpath=");
        pos3 = url.indexOf("rsrc=");
        pos4 = url.indexOf("&", pos3);
        if ( pos1 != -1 && pos2 != -1 && pos3 != -1 )
        {
            pcode = url.substring(pos1+6 , pos2-1);
            cpath = url.substring(pos2+6, pos3-1);
            rsrc = url.substring(pos3+5, pos4);
            if ( pcode != '' && cpath != '' && rsrc != '' ) { setIPMCookie(pcode, cpath, rsrc); }
        }
//    }
}

// call by chk_ipm()
function setIPMCookie(pcode, cpath, rsrc)
{
    req = false;
    dataResult = '';

    if ( window.XMLHttpRequest ) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
    } else if ( window.ActiveXObject ) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                req = false;
            }
        }
    }
    if ( req ) {
        req.open("GET", "http://"+location.host+"/mrkt.php?pcode="+pcode+"&cpath="+cpath+"&rsrc="+rsrc, false);
        req.send("");
        dataResult = req.responseText;
    }
    return dataResult;
}

