common_data = function() {

	this.cb = new ContentBuffer();
	
}

// PopUp Window
common_data.prototype.popwin = function(url,w,h,type) {
	if (!w) {w = 800;}
	if (!h) {h = 500;}
	if (url) {
		if (type == 'nonav') {
			this.popWindow = window.open(url, 'pop_window', 'width='+w+',height='+h+',scrollbars=yes,menubar=0,status=no,toolbar=0,resizable=yes');
		} else if (type == 'noscroll') {
			this.popWindow = window.open(url, 'pop_window', 'width='+w+',height='+h+',scrollbars=0,menubar=0,status=no,toolbar=0,resizable=yes');
		} else {
			this.popWindow = window.open(url, 'pop_window', 'width='+w+',height='+h+',scrollbars=yes,menubar=1,status=no,toolbar=1,resizable=yes');
		}
		this.popWindow.focus();
	}
}

function openInPopup(a) {
	var href = a.href;
	var newwindow = window.open(href,'SnP','height=465,width=650,scrollbars=yes');
	if (window.focus) {newwindow.focus();}	
}

common_data.prototype.getCard = function(el) {

	this.linkElement = el;

	this.cb.abortRequests();

	this.cb.load({
		url: '../../common/buffers/baseballCard.asp',
		context: self,
		onload: function(cp){
			var results = cp.getResult();
			common.showCard(results);
		},
		onerror: self.Error,
		method: "post"
	});

};

common_data.prototype.showCard = function(results) {

     var linkXY = Element.getXY(this.linkElement);

  	this.cardContainer = Element.create('div',{id:'card_container','class': 'shown',style:'position:absolute;top:'+(linkXY.y-405)+'px;left:'+(linkXY.x+100)+'px;width:350px'},results, document.body);

}

common_data.prototype.removeCard = function() {

	Element.remove('card_container');

}

common_data.prototype.showLoginInfo = function(el) {

	 var linkX = Element.getXY(el);
	 var results = "<img src='../../images/common/login_info.gif'>";

  	this.cardContainer = Element.create('div',{id:'login_info','class': 'shown',style:'position:absolute;top:47px;left:'+(linkX.x)+'px;width:350px;z-index:100;'},results, document.body);

}

common_data.prototype.hideLoginInfo = function() {

	window.setTimeout("Element.remove('login_info')",30);

}

common_data.prototype.showDesc = function(desc) {

	if (desc == 'long') {
          Element.setDisplay('businessDescShort','none');
          Element.setDisplay('businessDescLong','block');
	} else {
		Element.setDisplay('businessDescShort','block');
		Element.setDisplay('businessDescLong','none');
	}

}

common_data.prototype.showExecutive = function(activeDiv) {

	if (Element.hasClass('peopleName'+activeDiv,'open')) {
	     Element.setDisplay('peopleDetail'+activeDiv,'none');
		Element.replaceClass('peopleName'+activeDiv,'peoplePlus','peopleMinus');
		Element.removeClass('peopleName'+activeDiv,'open');
	} else {
		Element.setDisplay('peopleDetail'+activeDiv,'block');
		Element.replaceClass('peopleName'+activeDiv,'peopleMinus','peoplePlus');
		Element.addClass('peopleName'+activeDiv,'open');
	}

}

common_data.prototype.showSubsidiary = function(activeDiv) {


	if ((activeDiv % 2) == 0) {
		rowColor = "Blue";
	} else {
		rowColor = "White";	
	}

	if (Element.hasClass('subsidiaryName'+activeDiv,'open')) {
    	     Element.setDisplay('subsidiaryDetail'+activeDiv,'none');
		Element.replaceClass('subsidiaryName'+activeDiv,'subsidiaryPlus','subsidiaryMinus' + rowColor);
		Element.removeClass('subsidiaryName'+activeDiv,'open');
	} else {
		Element.setDisplay('subsidiaryDetail'+activeDiv,'block');
		Element.replaceClass('subsidiaryName'+activeDiv,'subsidiaryMinus' + rowColor,'subsidiaryPlus');
		Element.addClass('subsidiaryName'+activeDiv,'open');
	}

}

common_data.prototype.companySearch = function(input,section) {
    if(input) {
	    var symbol = Element.get(input).value;

		if (section == "admin") {
			var url = '../markets/company/company.asp?capIQId=' + symbol;
		} else {
			var url = '../company/company.asp?capIQId=' + symbol;
		}
		
        window.location.href = url;
    }
}

common_data.prototype.changeKeyIndex = function(exchange,numExchanges) {

	for (x=0;x<numExchanges;x++) {
	     if (x != exchange) {
	    	     Element.setDisplay('keyIndex_'+x,'none');
			Element.replaceClass('keyIndexTab_'+x,'exTabWhite','exTabBlue');
	     } else {
			Element.setDisplay('keyIndex_'+x,'block');
			Element.replaceClass('keyIndexTab_'+x,'exTabBlue','exTabWhite');
	     }
	}

	//
	//Element.addClass('peopleName'+activeDiv,'open');

}

var selectedEvent = null;

common_data.prototype.timelineExpandEvent = function(i) {

    this.oldHTML = Element.get("event" + i).innerHTML;    
    Element.get("event" + i).innerHTML = Element.get("event" + i + "_expand").innerHTML;

}

common_data.prototype.timelineShrinkEvent = function(i) {
	    
	Element.get("event" + i).innerHTML = this.oldHTML;

}

common_data.prototype.SendMeInfo = function() {

	var email = Element.get('SMI_Email').value;
	if(email) {
		this.cb.load({
			url: "../../common/sendMeInfo_buffer.asp",
			data: {email:email},
			contentType: "text/javascript",
			onerror: this.Error,
			onload: this.updateSendMeInfoResult,
			method: "post",
			context: this			
		});			
	}
}
	
common_data.prototype.updateSendMeInfoResult = function(buffer) {	

    results = buffer.getResult();
	
    if(results){
	    Element.get("sendMeInformationTxt").innerHTML = results;
    }	
}

// Displays Chart Rollover
common_data.prototype.displayInfo = function(who,what,left,top) 
{
	this.iContent = Element.get("content_"+who);
	if (what) {
		this.iContent.style.display = "block";
		this.iContent.style.left = left - 13 + 'px';
		this.iContent.style.top = top - 90 + 'px';
	} else {
		this.iContent.style.display = "none";
	}
}
// wrapper for onload
function rWrap(who,what,left,top) {
    try {
        common.displayInfo(who,what,left,top);
	} catch(e) {}
}

var common = new common_data();

function GetScrollTop(){
	var scrollTop = document.body.scrollTop;
	
	if(scrollTop == 0){
    if(window.pageYOffset){
    	scrollTop = window.pageYOffset;
    } else {
      scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
	}
		
	return scrollTop;
}

//Funtion used for Report Data Error link and panel
var reportError = function(){
	this.panel      = Element.get("reportErrorPanel");
	this.formDiv    = Element.get("formDiv");
	this.submitDiv  = Element.get("sentErrorDiv");
	this.reportLink = Element.get("reportErrorLink");
	this.closeBtn   = Element.get("reportErrorCloseBtn");
	this.cancelBtn  = Element.get("reportErrorCancelBtn");
	this.submitBtn  = Element.get("reportErrorSubmitBtn");
	
	//after submit
	this.anotherLink = Element.get("submitAnotherLink");
	this.doneLink   = Element.get("doneLink");
	
	// input fields elements
	this.inSection   = Element.get("sectionHeader");
	this.inDataPoint = Element.get("dataPoint");
	this.inYourName  = Element.get("yourName");
	this.inYourEmail = Element.get("yourEmail");
	this.inDescribe  = Element.get("describeError");
	
	//labels elements
	this.lbSection   = Element.get("sectionHeaderLabel");
	this.lbDataPoint = Element.get("dataPointLabel");
	this.lbYourName  = Element.get("yourNameLabel");
	this.lbYourEmail = Element.get("yourEmailLabel");
	this.lbDescribe  = Element.get("describeErrorLabel");
	
	this.fields = [];
	this.fields['sectionHeader'] = {inputEl: this.inSection,   labelEl: this.lbSection,   label:'Section Header'};
	this.fields['dataPoint']     = {inputEl: this.inDataPoint, labelEl: this.lbDataPoint, label:'Data Point'};
	this.fields['yourName']      = {inputEl: this.inYourName,  labelEl: this.lbYourName,  label:'Your Name'};
	this.fields['yourEmail']     = {inputEl: this.inYourEmail, labelEl: this.lbYourEmail, label:'Your E-Mail Address'};
	this.fields['describeError'] = {inputEl: this.inDescribe,  labelEl: this.lbDescribe,  label:'Describe Data Error'};
	
	this.formData = [];
	this.errors = [];
	this.errorAnnouncePanel = Element.get("errorAnnouncePanel");
	this.errorList = Element.get("errorList");
	
	
	this.shouldDrag = false;
	
	this.cBuffer = new ContentBuffer();
}

reportError.prototype.AttachEvents = function(){

	Events.add({
		element: this.panel,
		type: "mousedown",
		handler: this.DragPanel,
		context: this
	});
	Events.add({
		element: document,
		type: "mouseup",
		handler: this.DropPanel,
		context: this
	});
	Events.add({
		element: document,
		type: "mousemove",
		handler: this.MovePanel,
		context: this
	});
	
	Events.add({
		element: this.reportLink,
		type: "click",
		handler: this.ShowReportErrorPanel,
		context: this
	});
	
	Events.add({
		element: this.closeBtn,
		type: "click",
		handler: this.HideReportErrorPanel,
		context: this
	});
	Events.add({
		element: this.cancelBtn,
		type: "click",
		handler: this.HideReportErrorPanel,
		context: this
	});
	
	Events.add({
		element: this.submitBtn,
		type: "click",
		handler: this.CheckFormAndSubmit,
		context: this
	});
	
	Events.add({
		element: this.anotherLink,
		type: "click",
		handler: this.ResetForm,
		context: this
	});
	
	Events.add({
		element: this.doneLink,
		type: "click",
		handler: this.HideReportErrorPanel,
		context: this
	});
	
	
	Events.add({
		element: this.inSection,
		type: "mousemove",
		handler: this.DropPanel,
		context: this
	});
	Events.add({
		element: this.inDataPoint,
		type: "mousemove",
		handler: this.DropPanel,
		context: this
	});
	Events.add({
		element: this.inYourName,
		type: "mousemove",
		handler: this.DropPanel,
		context: this
	});
	Events.add({
		element: this.inYourEmail,
		type: "mousemove",
		handler: this.DropPanel,
		context: this
	});
	Events.add({
		element: this.inDescribe,
		type: "mousemove",
		handler: this.DropPanel,
		context: this
	});
}

reportError.prototype.ShowReportErrorPanel = function(){
	if(this.panel.style.display != "block"){
		var scrollTop = GetScrollTop();
		var top = scrollTop + 100;
		var left = 300; //window.scrollX
		this.panel.style.top = top + "px";
		this.panel.style.left = left + "px";
		this.panel.style.display = "block";
	}
}

reportError.prototype.ResetForm = function(){
	for(var key in this.fields){
		this.fields[key].inputEl.value = "";
		this.fields[key].labelEl.className = "";
	}
	
	this.ErrorReset();
	
	if(this.submitDiv.style.display == "block"){
		this.submitDiv.style.display = "none";
		this.formDiv.style.display = "block";
	}
}

reportError.prototype.ErrorReset = function(){
	this.errors = [];
	Element.removeChildNodes(this.errorList);
	this.errorAnnouncePanel.style.display = "none";
}

reportError.prototype.HideReportErrorPanel = function(){
	if(this.panel.style.display != "none"){
		this.panel.style.display = "none";
		this.ResetForm();
	}
}

reportError.prototype.CheckFormAndSubmit = function(){
	this.ErrorReset();
	var ct = 0;
	
	for(var key in this.fields){
		var asterick = Element.create("span", {className:"formAsterick"}, '*');
		
		if(this.fields[key] && this.fields[key].inputEl && !this.fields[key].inputEl.value){	
			if(ct == 0){
				Element.addChild(this.errorList, this.fields[key].label);
			} else {
				Element.addChild(this.errorList, ", " + this.fields[key].label);
			}
			this.errorList.appendChild(asterick);
			
			this.fields[key].labelEl.className = "errorLabel";
			
			ct++;
		}
		else if(key == 'yourEmail'&& !this.ValidateEmail(this.fields[key].inputEl.value)){
			if(ct == 0){
				Element.addChild(this.errorList, this.fields[key].label);
			} else {
				Element.addChild(this.errorList, ", " + this.fields[key].label);
			}
			this.errorList.appendChild(asterick);
			
			this.fields[key].labelEl.className = "errorLabel";
			
			ct++;
		}
		else{
		    try {
			    this.formData[key] = {label: this.fields[key].label, value: this.fields[key].inputEl.value};
			    if(this.fields[key].labelEl.className == "errorLabel"){
				    this.fields[key].labelEl.className = "";
    			}
			} catch(e) {}
		}
	}
	
	if(ct > 0){
		this.errorAnnouncePanel.style.display = "block";
	} else {
		var dataStr = '';
		var ct = 0;
		
		for(var key in this.formData){
			if(ct == 0){
				dataStr = dataStr + this.formData[key].label + '::' + this.formData[key].value;
			} else {
				dataStr = dataStr + '|' + this.formData[key].label + '::' + this.formData[key].value;
			}
			ct++;
		}
		
		this.cBuffer.load({
			url: "../../common/reportError_buffer.asp",
			data: {formdata: dataStr, fromurl: escape(String(window.location))},
			contentType: "text/javascript",
			onload: this.SubmitSuccess,
			onerror: this.SubmitError,
			method: "post",
			context: this
		});
	}
}

reportError.prototype.ValidateEmail = function(email){
	var isValid = false;
	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)){
		isValid = true;
	}
	
	return isValid;
}

reportError.prototype.SubmitSuccess = function(buffer){
	var result = buffer.getResult();
	if(result == "true"){
		this.formDiv.style.display = "none";
		this.submitDiv.style.display = "block";
	}
}

reportError.prototype.SubmitError = function(buffer){
	
}

reportError.prototype.DragPanel = function(ev, el){
	var scrollTop = GetScrollTop();
	
	this.panelX = parseInt(el.style.left);
	this.panelY = parseInt(el.style.top);
	
	this.clickX = ev.clientX;
	this.clickY = ev.clientY + scrollTop;
	
	this.inFromLeftDis = this.clickX - this.panelX;
	this.inFromTopDis = this.clickY - this.panelY;
	
	this.shouldDrag = true;
}
reportError.prototype.DropPanel = function(ev, el){
	this.shouldDrag = false;
}

reportError.prototype.MovePanel = function(ev, el){
	if(this.shouldDrag){
		var scrollTop = GetScrollTop();
		
		var x = ev.clientX - this.inFromLeftDis;
		var y = (ev.clientY + scrollTop) - this.inFromTopDis;
		if(this.clickX < this.panelX){
			x = this.panelX + 20;
		}
		if(this.clickY < this.panelY){
			y = this.panelY + 20;
		}
		
		this.panel.style.left = x + "px";
		this.panel.style.top = y + "px";
	}
}

function InitReportError(){
	try{
		var error = new reportError();
		error.AttachEvents();
	} catch(e){
		
	}
}

Events.add({
	element: window,
	type: 'load',
	handler: InitReportError
});