function req(variable) {
  var query = window.location.search.substring(1);
	var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

function popwin(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{
  		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 ArrayLength(arr){
	var ct = 0;
	for(var idx in arr){
		ct++;
	}
	return ct;
}

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;
}

//	clearInput checks an input box to see if the current value is 
//	equal to the default value.  you can supply a default value if you choose.
//	otherwise, the default value is set to targetID.defaultValue.  this is useful
//	if you are dynamically setting a default value (such as the news pages)
function clearInput(targetID, defVal) {
	var defaultVal = "";
	defVal ? defaultVal = defVal : defaultVal = targetID.defaultValue;

	if (targetID.value == defaultVal) {
		targetID.value = "";
	}
}

//fillInput checks the value in an element and, if it matches valueToReplace, replaces that value with replacementVal
/*example:
 	<input onfocus=\"javascript:fillInput(this, 'Keyword(s)', '');\" onblur=\"javascript:fillInput(this, '', 'Keyword(s)');\" />
*/ 
function fillInput(targetEl, valueToReplace, replacementVal) {
	if (targetEl.value == valueToReplace) {
		targetEl.value = replacementVal;
	}
} 

//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].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{
			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 = "";
			}
		}
	}
	
	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: "/businessweek/research/common/buffer/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
});