
function GetReportAjax(CBUrl1, NamingContainer, ReportMode, NamingContainer){
	this.CBUrl = CBUrl1;
	this.NamingContainer = NamingContainer;
	this.ReportMode = ReportMode;
	this.GoBtn = document.getElementById(NamingContainer + '_CalculatePageRank');
	this.URL = document.getElementById(NamingContainer + '_URLPageRank');
	this.Keywords = document.getElementById(NamingContainer + '_UserKeywords');
	this.Email = document.getElementById(NamingContainer + '_txtEmail');
	this.CompsUrls = document.getElementById(NamingContainer + '_CompsUrls');
	this.AddToDashboard = document.getElementById(NamingContainer + '_AddToDashboard');
	this.Output = document.getElementById('report-content');
	this.AbortProgress = false;
	this.AniGif = '<img src=\'/hsimages/hubspot_load5.gif\' border=\'0\'/>&nbsp;';
	this.ProgTimer;
	this.NamingContainer = NamingContainer;
	
	this.init  = function(){
		this.GoBtn.removeAttribute('disabled');
	}
		
	this.handleSuccess = function(o){ 
        var xml = o.responseXML;
       var Success = xml.getElementsByTagName('s')[0];
       if (Success.firstChild.nodeValue == 'true') {
			var Report = xml.getElementsByTagName('report')[0];
			var Progress = xml.getElementsByTagName('progress')[0];
			if (Report != null) {
				this.Output.innerHTML = Report.firstChild.nodeValue;
				window.location.href= "#ReportTop";
				if(this.GoBtn!=null)this.GoBtn.removeAttribute('disabled');
				//if(this.URL!=null)this.URL.removeAttribute('disabled');
				//if(this.Keywords!=null)this.Keywords.removeAttribute('disabled');
				//if(this.Email!=null)this.Email.removeAttribute('disabled');
				//if(this.CompsUrls!=null)this.CompsUrls.removeAttribute('disabled');
				//if(this.AddToDashboard!=null)this.AddToDashboard.removeAttribute('disabled');
				window.clearTimeout(this.ProgTimer);
				this.AbortProgress = true;
				//refresh Grid
				try
				{
				    RefreshDashboard();
				}
				catch(Exception)
				{
				   // alert('here');
				}
			} else {
				this.Output.innerHTML = this.AniGif + Progress.firstChild.nodeValue;
				this.ProgTimer = window.setTimeout("GetRep.startRequest(true)", 1500);
			}
		} else {
			var Error = xml.getElementsByTagName('error')[0];
			this.Output.innerHTML = Error.firstChild.nodeValue;
			if(this.GoBtn!=null)this.GoBtn.removeAttribute('disabled');
			//if(this.URL!=null)this.URL.removeAttribute('disabled');
			//if(this.Keywords!=null)this.Keywords.removeAttribute('disabled');
			//if(this.Email!=null)this.Email.removeAttribute('disabled');
			//if(this.CompsUrls!=null)this.CompsUrls.removeAttribute('disabled');
			//if(this.AddToDashboard!=null)this.AddToDashboard.removeAttribute('disabled');
			window.clearTimeout(this.ProgTimer);
			this.AbortProgress = true;
		}
	}

	this.handleFailure = function(o){
		this.Output.innerHTML = "Server contact failed, please try again.";
		if(this.GoBtn!=null)this.GoBtn.removeAttribute('disabled');
		//if(this.URL!=null)this.URL.removeAttribute('disabled');
	    //if(this.Keywords!=null)this.Keywords.removeAttribute('disabled');
	    //if(this.Email!=null)this.Email.removeAttribute('disabled');
	    //if(this.CompsUrls!=null)this.CompsUrls.removeAttribute('disabled');
	   // if(this.AddToDashboard!=null)this.AddToDashboard.removeAttribute('disabled');
		window.clearTimeout(this.ProgTimer);
		this.AbortProgress = true;
	}

	this.startRequest = function(ForProgress, ForProb) {  
		var callback =   
		{   
			success:this.handleSuccess,   
			failure:this.handleFailure,
			scope: this   
		};
		
		if (ForProgress) {
			if (this.Abort) {this.init(); return;}
			if (this.Asynch != null)
				if (YAHOO.util.Connect.isCallInProgress(this.Asynch)) return;
		} else {
			this.Output.style.display = 'inline';
			this.Output.innerHTML = this.AniGif + 'Initializing ...';
			if(this.GoBtn!=null)this.GoBtn.setAttribute('disabled', 'true');
			//if(this.URL!=null)this.URL.setAttribute('disabled', 'true');
			//if(this.Keywords!=null)this.Keywords.setAttribute('disabled', 'true');
			//if(this.Email!=null)this.Email.setAttribute('disabled', 'true');
			//if(this.CompsUrls!=null)this.CompsUrls.setAttribute('disabled', 'true');
			//if(this.AddToDashboard!=null)this.AddToDashboard.setAttribute('disabled', 'true');
		}
		
		//this.Output.innerHTML = '';
		//alert('here');
		var Url = this.CBUrl + '&Action=' + (ForProgress? 'GetProgress' : 'GenerateReport') + '&URL=' + encodeURIComponent(this.URL.value);
		if (this.Email != null && this.Email.value!="")
			Url += '&Email=' + encodeURIComponent(this.Email.value);
		if (this.Keywords != null && this.Keywords.value!="")
			Url += '&Keywords=' + encodeURIComponent(this.Keywords.value);
	    if(this.CompsUrls !=null && this.CompsUrls.value!="")
	        Url += '&CompsUrls=' + encodeURIComponent(this.CompsUrls.value);
	    if(this.AddToDashboard !=null)
	        Url += '&AddDashboard=' + encodeURIComponent(this.AddToDashboard.checked);
			 
		YAHOO.util.Connect.asyncRequest('GET', Url, callback);	
	}
	
	this.ProbProgress = function(){
		ProbSuccess = function (o){
			var xml = o.responseXML;
			var Success = xml.getElementsByTagName('s')[0];
			if (Success.firstChild.nodeValue == 'true') {
				var Report = xml.getElementsByTagName('report')[0];
				var Progress = xml.getElementsByTagName('progress')[0];
				if (Progress != null) {
					this.handleSuccess(o);
					if(this.GoBtn!=null)this.GoBtn.setAttribute('disabled', 'true');
					//if(this.URL!=null)this.URL.setAttribute('disabled', 'true');
			        //if(this.Keywords!=null)this.Keywords.setAttribute('disabled', 'true');
			        //if(this.Email!=null)this.Email.setAttribute('disabled', 'true');
			       // if(this.CompsUrls!=null)this.CompsUrls.setAttribute('disabled', 'true');
			        //if(this.AddToDashboard!=null)this.AddToDashboard.setAttribute('disabled', 'true');
				}
			}
		}
		
		ProbFailure = function(o){
		}
		
		var callback =   
		{   
			success:ProbSuccess,
			failure:ProbFailure,
			timeout: 5000, 
			scope: this
		};
		
		var Url = this.CBUrl + '&Action=' + 'GetProgress';		 
		YAHOO.util.Connect.asyncRequest('GET', Url, callback);	
	}
	
	this.WindowLoad = function() {
		this.ProbProgress();
		this.HookupEvents();
	}
	
	this.HookupEvents = function() {
		var EventDiv = document.getElementById('EventDiv');
		EventDiv.addKeyListener(13, this.KWGEnterHandler.createDelegate(this), this);
	}
	
	this.KWGEnterHandler = function(keycode, e) {
	
        var a = e.findTarget();
        
        try {
			if(a) {
				if (a.tagName.toLowerCase() == 'textarea') return;
				
				var EnterTargetID = a.getAttribute('EnterTargetID');
				if ( EnterTargetID != null) {
					var Buddy = document.getElementById((EnterTargetID.indexOf('_') > 0? EnterTargetID : this.NamingContainer + '_' + EnterTargetID));
					if (Buddy != null)
						Buddy.dom.click();
				} 

			}
		} catch(e){}
        
        e.preventDefault();
        e.stopEvent();
        e.stopPropagation();
	}
}

var WSGApp = function(){
	var KeywordsForm = null, KeywordsToggle = null;
	var CompetitorsForm = null, CompetitorsToggle = null;
	var WebSiteTypeForm = null, WebsiteTypeToggle = null;
	
    return {
        init : function(){
			var El;
			if ((El = document.getElementById('SiteTypeInfoCmd')) != null) {
				SiteTypeDlg = new YAHOO.ext.BasicDialog("SiteTypeInfo", { 
						modal:true,
						autoTabs:false,
						width:500,
						height:200,
						shadow:true,
						minWidth:500,
						minHeight:200,
						resizable:false,
						title:"Website Type"
				});
				SiteTypeDlg.addButton('Close', SiteTypeDlg.hide, SiteTypeDlg);
				El.mon('click', function(e){document.getElementById(document.body).addClass('ytheme-gray'); SiteTypeDlg.show(document.getElementById('SiteTypeInfoCmd')); }, true);
				SiteTypeDlg.on('hide', function(e){document.getElementById(document.body).removeClass('ytheme-gray'); }, true);
			}

			if ((El = document.getElementById('KeywordsInfoCmd')) != null) {
				KeywordsInfoDlg = new YAHOO.ext.BasicDialog("KeywordsInfo", { 
						modal:true,
						autoTabs:false,
						width:500,
						height:200,
						shadow:true,
						minWidth:500,
						minHeight:200,
						resizable:false,
						title:"Keywords"
				});
				KeywordsInfoDlg.addButton('Close', KeywordsInfoDlg.hide, KeywordsInfoDlg);
				El.mon('click', function(e){document.getElementById(document.body).addClass('ytheme-gray'); KeywordsInfoDlg.show(document.getElementById('KeywordsInfoCmd')); }, true);
				KeywordsInfoDlg.on('hide', function(e){document.getElementById(document.body).removeClass('ytheme-gray'); }, true);
			}
        }
	};
}();

//YAHOO.util.Event.on(window, 'load', WSGApp.init, WSGApp, true);
//********************************************************
function isURL(urlStr)
 {
   return true;  	
}
//********************************************************
var SendLinkByMail = function(URL,Mail, Link,CBHURL,ErrorDiv){
    this.ErrorDiv = document.getElementById(ErrorDiv);
	this.CBUrl = CBHURL;
	this.Asynch = null;
	this.Mail=Mail;
	this.Link=document.getElementById(Link);
	this.URL=document.getElementById(URL);

	this.init = function(){
		
		this.Abort = false;
	}
		
	this.abort = function(){
		this.Abort = true;
	}
		
	this.handleSuccess = function(o){ 
        var xml = o.responseXML;
        var Success = xml.getElementsByTagName('s')[0];
		if (Success.firstChild.nodeValue == 'true') {
		    this.ErrorDiv.innerHTML="Email Sent Successfully.";
		} else {
			var Error = xml.getElementsByTagName('error')[0];
			this.ErrorDiv.innerHTML = Error.firstChild.nodeValue;
		}
		this.Link.disabled=false;
	}

	this.handleFailure = function(o){
		this.ErrorDiv.innerHTML = "Server contact failed, please try again."; 
		this.Link.disabled=false;
	}

	this.startRequest = function() {   
		if (this.Abort) {this.init(); return;}
		if (this.Asynch != null)
			if (YAHOO.util.Connect.isCallInProgress(this.Asynch)) return;
		this.Link.disabled=true;
		var callback =   
		{   
			success:this.handleSuccess,   
			failure:this.handleFailure,
			timeout: 5000, 
			scope: this   
		};

		this.ErrorDiv.innerHTML="Sending Please Wait ...";

		// the url QS is commented because we get it from the session
		var Url = this.CBUrl + '&Action=SendReportLink'+'&Email='+encodeURIComponent(this.Mail)+'&ReportLink='+encodeURIComponent(this.Link.href)+'&URL='+encodeURIComponent(this.URL.value);
		this.Asynch = YAHOO.util.Connect.asyncRequest('GET', Url, callback);   
	}
}
//*****************************************************************
function ToggleFromCalendar(renderDay, e)
{
    var DateRangeDDL = GetDateRange();
	DateRangeDDL.value = "-100";
}
//*****************************************************************
function ToggleFromDateRange()
{
	var DateRangeDDL = GetDateRange();
	var FromDate =  GetFromDate();
	var ToDate =  GetToDate();
	var CustomDateTable = GetCustomDateTable();
	var DR = 0;
	if (DateRangeDDL.value == "-100")
	{
		DR = 6
		var Now = new Date();
		var From = new Date(Now - DR*24*60*60*1000);
		
		ToDate.SetDate(Now);
		FromDate.SetDate(From);	
		
		CustomDateTable.style.display = 'block';
		CustomDateTable.style.visibility = 'visible';
	}
	else
	{
		CustomDateTable.style.display = 'none';
		CustomDateTable.style.visibility = 'hidden';
		var DivObj=document.getElementById("dvDateError");
        if(DivObj!=null) {DivObj.innerHTML="";}
	}
}
//*****************************************************************
function GetDateParams()
{
	var DateRangeDDL = GetDateRange();
	if (DateRangeDDL == null) return 'dr=30';
    var FromDate =  GetFromDate();
	var ToDate =  GetToDate();
	var s='';
	if (DateRangeDDL.value == "-100")
	{	//return
		 s='sd=' + encodeURIComponent(FormateDate(FromDate.GetDate())) + '&ed=' + encodeURIComponent(FormateDate(ToDate.GetDate()))+'&';
	}
	//else
	return s+='dr=' + DateRangeDDL.value;
}
//*****************************************************************
function HideCloaps(Parent,Child)
{
    var P=document.getElementById(Parent);
    var C=document.getElementById(Child);
    if(C!=null)
        {
        if(C.style.display=='none')
        {
            C.style.display='block';
        }
        else
        {
           C.style.display='none' 
        }
    }
}

function ISLongText(Text)
{
	if(Text.Length > 50)
	{	
		return true;
	}
	else
	{
		 return false;
	}
}

function CreateItemCell(ItemText)
{
	if(ISLongText(ItemText))
	{
		var Display=ItemText.substr(0,47);
		document.write("<td title='" +ItemText+" '>"+Display+"...</td>");
	}
	else
	{
		document.write("<td title=' "+ItemText+" '>"+ItemText+"</td>");
	}
}

