var currentHost="fun.omy.sg";

function getOmyUsername()
{
    return document.cookie.match ( 'omyUsername' + '=(.*?)(;|$)' );
}

function getRegUsername()
{
    return readCookie('regName');
}

function isLoggedIn()
{
    var iplanetIsset = document.cookie.match ( 'iPlanetDirectoryPro' + '=(.*?)(;|$)' );
    
    if(getOmyUsername() && iplanetIsset) {
        return true;
    }
    else {
        return false;
    }
}

function getAuthURL(loginType)
{
    if( isLoggedIn() ) {
        var logout_path;
        
        logout_path = "http://sphauth.asiaone.com/amserver/UI/Logout?logoutdest=" + escape(window.location.href);
        
        return logout_path;
    }
    else {
        var login_path;

        if(loginType == 'refresh') {
            login_path = "http://" + currentHost + "/secure/relay.php?ref=refresh&origin=" + escape(window.location.hostname);
        }
        else {
            login_path = "http://" + currentHost + "/secure/relay.php?ref=" + escape(window.location.href);
        }

        return login_path;
    }
}

function setCookie(c_name, value, expiredays)
{
    var exdate = new Date();
    
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name+ "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    
    return null;
}

function keepTrack( sub_id )
{
	var hostnameaddress = window.location.hostname;
	var data = window.location.href.split("/");
	var blog_id = data[3];
	
	window.open('http://' + hostnameaddress + '/omy_post_http.php?blog_id=' + blog_id + '&subscribe=' + sub_id , 'omy' , 'scrollbars=no,width=200,height=200');
	
	return blog_id;
}

if( isLoggedIn() ) {
    document.write('<span class="user"><span class="en">Welcome ' + getRegUsername() + '</span></span></li>');
    document.write('<li><a href="http://sphreg.asiaone.com/RegAuth2/omyMainMenu.html"><span class="en">My Account</span></a></li>');
    document.write('<li><a href="' + getAuthURL(null) +'"><span class="en">Logout</span></a>');
    
}
else {
    document.write('<a onClick="login(\''+ getAuthURL('refresh') +'\');return false;"><span class="en">Login to omy</span></a></li>');

    if(currentHost == "fun.omydev.sg"){
          document.write('<li><a href="http://dsreg.asiaone.com/RegAuth2/omySignUp.html"><span class="en">Registration with omy</span></a>');
      }else{
          document.write('<li><a href="http://sphreg.asiaone.com/RegAuth2/omySignUp.html"><span class="en">Registration with omy</span></a>');
      }

}

var myimages = new Array();

function preloading(){
  for (x=0; x<preloading.arguments.length; x++){
    myimages[x] = new Image();
    myimages[x].src = preloading.arguments[x];
  }
}
