var RCOfficeHOST = document.getElementById("RCOfficepopupJS").src.match(/https?:\/\/[^\/]*\//i);
if (!RCOfficeHOST ) RCOfficeHOST = '/';

function checkClose() {

	document.getElementById("RCsignup").style.display = "none";

	document.getElementById("RCsignupFrame").src = RCOfficeHOST + 'affiliate/?srcURL=' + window.location.href;
	if ( !document.getElementById("RCsignupFrame").src )
		RCsignupFrame.location.href = RCOfficeHOST + '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>');

document.write('<script> if( window.name.indexOf( "RCuserNumber" ) != -1  && window.top != window.self ) { window.top.user_RC_Number = window.name.replace("RCuserNumber-", "");}</script>');

function cRCuserData() {
	this.firstName;
	this.lastName;
	this.contactPhone;
	this.eMail;
}

var user_RC_Number = "";
var linkText = document.getElementById("RCOfficepopupJS").getAttribute("linkText") ? document.getElementById("RCOfficepopupJS").getAttribute("linkText"): "Get Toll Free Number";
document.write('<link rel="stylesheet" href="' + RCOfficeHOST + '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";
		document.getElementById("RCuserNumberBuffer").contentWindow.location.href = RCOfficeHOST + "affiliate/readnumber.html";
	}
 }

 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 RCuserData = new cRCuserData();

	if( typeof setRCuserData == "function" )
	{
		RCuserData = setRCuserData();
	}

	RCuserData.firstName = (RCuserData.firstName) ? RCuserData.firstName : ( document.getElementById('RCuserFirstName') ) ? document.getElementById('RCuserFirstName').value : "";

	RCuserData.lastName = (RCuserData.lastName) ? RCuserData.lastName : ( document.getElementById('RCuserLastName') ) ? document.getElementById('RCuserLastName').value : "";

	RCuserData.contactPhone = (RCuserData.contactPhone) ? RCuserData.contactPhone : ( document.getElementById('RCuserContactPhone') ) ? document.getElementById('RCuserContactPhone').value : "";

	RCuserData.eMail =	(RCuserData.eMail) ? RCuserData.eMail : ( document.getElementById('RCuserEMail') ) ? document.getElementById('RCuserEMail').value : "";

	RCuserData.firstName = escape( RCuserData.firstName );
	RCuserData.lastName = escape( RCuserData.lastName );
	RCuserData.contactPhone = escape( RCuserData.contactPhone );
	RCuserData.eMail = escape( RCuserData.eMail );

	var RC_pid = document.getElementById("RCOfficepopupJS").getAttribute("RC_PID") ? document.getElementById("RCOfficepopupJS").getAttribute("RC_PID"): "";
	RC_pid = escape( RC_pid );

	var srcURL = window.location.href;

	//var RCurl = 'http://www.ringcentral.com/affiliate/?' + srcURL;
	var RCurl = RCOfficeHOST + 'affiliate/iframe.html?';
	if( RCuserData.firstName ) RCurl += 'firstname=' + RCuserData.firstName;
	if( RCuserData.lastName ) RCurl += '&lastname=' + RCuserData.lastName;
	if( RCuserData.contactPhone ) RCurl += '&contactphone=' + RCuserData.contactPhone;
	if( RCuserData.eMail ) RCurl += '&email=' + RCuserData.eMail;
	if( RC_pid )		   RCurl += '&RC_pid=' + RC_pid;
	RCurl += '&srcURL=' + srcURL;
	RCurl = RCurl.replace( "?&", "?" );
	//alert( RCurl );

	//delete RCuserData;

/*	if( srcURL.indexOf( '?' ) == -1 ) {
		RCurl = 'http%3A%2F%2Fwww.ringcentral.com%2Faffiliate%2F?srcURL=' + srcURL + "%3F";
	} else {
		RCurl = 'http%3A%2F%2Fwww.ringcentral.com%2Faffiliate%2F?srcURL=' + srcURL;
	}
*/
	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 RCwindowCloseImg = document.createElement("img");
    RCwindowCloseImg.setAttribute('id', 'RCpopupClose');
	RCwindowCloseImg.setAttribute('alt', 'Close');
    RCwindowCloseImg.setAttribute('src', RCOfficeHOST + 'images/affiliate/close.gif');
	RCwindowTopBarDiv.appendChild( RCwindowCloseImg );

	addEvent ( RCwindowCloseImg, "onclick", getNumber );

	var RCwindowFrame = document.createElement("iframe");
	RCwindowFrame.setAttribute('id', 'RCsignupFrame');
	RCwindowFrame.setAttribute('name', 'RCsignupFrame');
	RCwindowFrame.setAttribute('src', RCurl );
	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 );

	var RCuserBuffer = document.createElement("iframe");
	RCuserBuffer.setAttribute('id', 'RCuserNumberBuffer');
	RCuserBuffer.setAttribute('name', 'RCuserNumberBuffer');
	RCuserBuffer.setAttribute('src', RCOfficeHOST + "affiliate/readnumber.html" );
	RCuserBuffer.setAttribute('width', '0');
	RCuserBuffer.setAttribute('height', '0');
	RCuserBuffer.setAttribute('scrolling', 'no');
	//RCuserBuffer.setAttribute('border', '0');
	RCuserBuffer.setAttribute('frameBorder', '0');
	document.body.appendChild( RCuserBuffer );

	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 );

function getNumber() {

				document.getElementById("RCuserNumberBuffer").contentWindow.location.href = window.location.href;
				document.getElementById('RCsignup').style.display='none';
				}
