
function checkClose( ) {

	document.getElementById("RCsignup").style.display = "none";

	document.getElementById("RCsignupFrame").src = 'http://www.ringcentral.com/affiliate/?srcURL=' + window.location.href;
	if ( !document.getElementById("RCsignupFrame").src )
		RCsignupFrame.location.href = 'http://www.ringcentral.com/affiliate/?srcURL=' + window.location.href;

}

document.write('<script> if( window.name.indexOf( "closeRCpopup" ) != -1  && window.top != window.self ) { window.top.checkClose( ); window.top.user_RC_Number = window.name.replace("closeRCpopup#", "");}</script>');

var user_RC_Number = "";
var linkText = document.getElementById("RCOfficepopupJS").getAttribute("linkText");
document.write('<link rel="stylesheet" href="http://www.ringcentral.com/css/affiliate/popup.css" type="text/css">');
document.write('<a href="javascript: void();" onclick="showRCWindow( ); return false;" >' +linkText+ '</a>');
 
 function showRCWindow( ) {
	
	if (document.getElementById("RCsignup") == null ){
		createRCWindow();
		document.getElementById("RCsignup").style.display="block";
	}else
		document.getElementById("RCsignup").style.display="block";
 }
 
 function removeEvent(obj,ev,func){

	if(window.addEventListener)
		{obj.removeEventListener(ev.replace(/^on/i,''),func,false);}

	if(window.detachEvent)
		{obj.detachEvent(ev,func);}
}


function addEvent(obj,ev,func)
{
	if(ev!='onunload'){
		window.addEvent( window,'onunload',function(){window.removeEvent(obj,ev,func);func=null;})
	}

	if(window.addEventListener){obj.addEventListener(ev.replace(/^on/i,""),func,false);}

	if(window.attachEvent){obj.attachEvent(ev,func);}
}

function createRCWindow( ) {

try {

	var aid = document.getElementById("RCOfficepopupJS").getAttribute("AID");
	var pid = document.getElementById("RCOfficepopupJS").getAttribute("PID");
	var sid = document.getElementById("RCOfficepopupJS").getAttribute("SID");
	var clickDomain = document.getElementById("RCOfficepopupJS").getAttribute("CLICKDOMAIN");

	var srcURL = window.location.href;
	srcURL = srcURL.replace( /\//g, "%2F" );
	srcURL = srcURL.replace( /\:/g, "%3A" );
	//srcURL = srcURL.replace( "?", "%3F" );
	srcURL = srcURL.replace( /\&/g, "%26" );
	//var url = 'http%3A%2F%2Fwww.ringcentral.com%2Faffiliate%2F?srcURL=' + srcURL;
	
	var url = '';
	url = 'http%3A%2F%2Fwww.ringcentral.com%2Faffiliate%2F';
	/*if( srcURL.indexOf( '?' ) == -1 ) {
		url = 'http%3A%2F%2Fwww.ringcentral.com%2Faffiliate%2F?srcURL=' + srcURL + "?";
	} else {
		url = 'http%3A%2F%2Fwww.ringcentral.com%2Faffiliate%2F?srcURL=' + srcURL;
	}
	*/
	var CJ_Tracking = 'http://' +clickDomain+ '/click-' +pid+ '-' +aid+ '?sid=' +sid+ '&url=' +url;
	
	var RCwindowDiv = document.createElement("div");
    RCwindowDiv.setAttribute('id', 'RCsignup');
	document.body.appendChild( RCwindowDiv );
	
	var RCwindowTopBarDiv = document.createElement("div");
    RCwindowTopBarDiv.setAttribute('id', 'RCwindowTopBar');
	RCwindowDiv.appendChild( RCwindowTopBarDiv );
	
	var RCwindowCloaseImg = document.createElement("img");
    RCwindowCloaseImg.setAttribute('id', 'RCpopupClose');
	RCwindowCloaseImg.setAttribute('alt', 'Close');
    RCwindowCloaseImg.setAttribute('src', 'http://www.ringcentral.com/images/affiliate/close.gif');
	RCwindowTopBarDiv.appendChild( RCwindowCloaseImg );
	
	addEvent ( RCwindowCloaseImg, "onclick", function() { document.getElementById('RCsignup').style.display='none';} );
	
	var RCwindowFrame = document.createElement("iframe");
	RCwindowFrame.setAttribute('id', 'RCsignupFrame');
	RCwindowFrame.setAttribute('name', 'RCsignupFrame');
	RCwindowFrame.setAttribute('src', CJ_Tracking );
	RCwindowFrame.setAttribute('width', '402');
	RCwindowFrame.setAttribute('height', '360');
	RCwindowFrame.setAttribute('scrolling', 'no');
	//RCwindowFrame.setAttribute('border', '0');
	RCwindowFrame.setAttribute('frameBorder', '0');
	RCwindowDiv.appendChild( RCwindowFrame );
	
	var RCwindowButtomBarDiv = document.createElement("div");
    RCwindowButtomBarDiv.setAttribute('id', 'RCwindowBottom');
	RCwindowDiv.appendChild( RCwindowButtomBarDiv );
	
	posPopup();
	
	
} catch(e) {
	alert("init-error");
}
}

function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function posPopup() {

	if (document.getElementById("RCsignup") != null ){
	
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) { 
      xScroll = window.innerWidth + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) { 
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    }
    else { 
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) { 
      if (document.documentElement.clientWidth) {
        windowWidth = document.documentElement.clientWidth; 
      }
      else {
        windowWidth = self.innerWidth;
      }
      windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) { 
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }

    if (yScroll < windowHeight) {
      pageHeight = windowHeight;
    }
    else { 
      pageHeight = yScroll;
    }

    if (xScroll < windowWidth) { 
      pageWidth = xScroll;  
    }
    else {
      pageWidth = windowWidth;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) 
	
	var offsetLeft = parseInt(((windowWidth - 400 )>> 1),10);
	offsetLeft = ( offsetLeft < 0 )? 0 : offsetLeft;
	
	var offsetTop = parseInt( ((windowHeight - 408 ) >> 1),10);
	offsetTop = ( offsetTop < 0 )? 0 : offsetTop;
	
	document.getElementById('RCsignup').style.left = getBodyScrollLeft() + offsetLeft + "px";
	document.getElementById('RCsignup').style.top = getBodyScrollTop() + offsetTop + "px";
	}
}

addEvent( window, "onresize", posPopup );
addEvent( window, "onscroll", posPopup );

