function submitForm(formname){
	document.getElementById(formname).submit();
}

function resizeWindow(x,y){
	window.resizeBy(x, y);
}

function testEmails()
{
	var img = new Image();
	img.src = "admin.cgi?action=testemails&r=" + Math.random();
	alert("Test emails have been sent.");
}

function resetEntry(id)
{
	var entry_str = "";
	
	if(id == -1)
		entry_str = "No Referrers";
	else if(id == -2)
		entry_str = "Search Engines";
	else if(id == -3)
		entry_str = "Extra Referrers";
	else if(id == -4)
		entry_str = "No Cookies";
	else if(id == -5)
		entry_str = "Exit URL";
	else if(id == -6)
		entry_str = "Ignored";
	else
		entry_str = "ID #" + id;
	
	if(confirm("Are you sure you want to reset the last 24 hours for " + entry_str + "?")){
		var img = new Image();
		img.src = "admin.cgi?action=resetentry&id=" + id;
		alert(entry_str + " has been reset.");
	}
}

function deleteBroker(id)
{
	if(confirm("Are you sure you want to delete Broker ID #" + id + "?")){
		var img = new Image();
		img.src = "admin.cgi?action=delbroker&id=" + id;
		alert("Broker ID #" + id + "has been deleted.");
	}
}

function selectTrades(formname)
{
	var selobj = document.getElementById(formname).selectedentries;
	var unselobj = document.getElementById(formname).unselectedentries;
	var n = selobj.options.length;
	var z = 0, i;
	for (i=0; i<unselobj.options.length; i++){
		if(unselobj.options[i].selected){
			var doAdd = 1;
			
			for(z=0;z<selobj.options.length; z++)
				if(unselobj.options[i].value == selobj.options[z].value){
					doAdd = 0;
					break;
				}
			
			if(doAdd){
				selobj.options[n] = new Option(unselobj.options[i].text, unselobj.options[i].value);
				n++;
			}
		}
	}
}

function unselectTrades(formname)
{
	var selobj = document.getElementById(formname).selectedentries;
	for (var i=selobj.options.length-1; i>=0; i--)
		if(selobj.options[i].selected){selobj.options[i] = null;}
}

function prepareForm(formname)
{
	var selobj = document.getElementById(formname).selectedentries;
	var unselobj = document.getElementById(formname).unselectedentries;
	for (var i=0; i<unselobj.options.length; i++)
		unselobj.options[i].selected = false;
	for (var i=0; i<selobj.options.length; i++)
		selobj.options[i].selected = true;
	document.getElementById(formname).submit();
}

function toggleCheckBoxes(value)
{
	var i = 0;
	var checkImage_master_edit = document.getElementById('checkImage_master_edit');
	if(value){
		checkImage_master_edit.src = checkImage_master_edit.src.replace('/unchecked', '/checked');
		checkImage_master_edit.onclick = function(){toggleCheckBoxes(0);};
		document.getElementById('master_edit').checked = 'checked';
		for(i=0; i<document.massedit.edittrade.length; i++){
			checkImage_edittrade = document.getElementById('checkImage_edittrade_'+i);
			checkImage_edittrade.src = checkImage_edittrade.src.replace('/unchecked', '/checked');
			document.getElementById('edittrade_'+i).checked = 'checked';
		}
	}
	else{
		checkImage_master_edit.src = checkImage_master_edit.src.replace('/checked', '/unchecked');
		checkImage_master_edit.onclick = function(){toggleCheckBoxes(1);};
		document.getElementById('master_edit').checked = '';
		for(i=0; i<document.massedit.edittrade.length; i++){
			checkImage_edittrade = document.getElementById('checkImage_edittrade_'+i);
			checkImage_edittrade.src = checkImage_edittrade.src.replace('/checked', '/unchecked');
			document.getElementById('edittrade_'+i).checked = '';
		}
	}
}

function massEditSelected()
{
	var i = 0;
	for(i=0; i<document.massedit.edittrade.length; i++)
		if(document.getElementById('edittrade_'+i).checked)
			return true;
	
	alert("You must select at least one entry to proceed.");
	return false;
}

function CopyToClipboard(name,date1,date2,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u)
{
	var loop;
	var title1 = "* " + name + " Statistics For " + date1 + " - " + date2 + " *\n";
	var title2 = "";
	
	for(loop=0;loop<title1.length-1;loop++)
		title2 += "*";
	title2 += "\n";
	
	PortableCopyToClipBoard(title2 + title1 + title2 +
							"\nIncoming:\n" +
							"  Uni In:                " + a + "\n" +
							"  Raw In:                " + b + "\n" +
							"\nProductivity:\n" +
							"  Total Productivity:    " + c + "\n" +
							"  Total Clicks:          " + d + "\n" +
							"\nProductivity Breakdown & Clicking Ratios:\n" +
							"  Content Productivity:  " + e + "\n" +
							"  Content Ratio:         " + f + "\n" +
							"  Trade Productivity:    " + g + "\n" +
							"  Trade Ratio:           " + h + "\n" +
							"  Sold Productivity:     " + i + "\n" +
							"  Sold Ratio:            " + j + "\n" +
							"  Other Productivity:    " + k + "\n" +
							"  Other Ratio:           " + l + "\n" +
							"\nOutgoing:\n" +
							"  Uni Out:               " + m + "\n" +
							"  Raw Out:               " + n + "\n" +
							"\nOutgoing Breakdown & Ratios:\n" +
							"  From blind:            " + o + "\n" +
							"  From permanent:        " + p + "\n" +
							"  Return %:              " + q + "\n" +
							"\nQuality Markers:\n" +
							"  Loyal %:               " + r + "\n" +
							"  Gain %:                " + s + "\n" +
							"\nCurrent Settings:\n" +
							"  Force Goal:            " + t + "\n" +
							"  Ladder Rank:           " + u + "\n");
}

function CopyLinkToClipboard(id,url)
{
	PortableCopyToClipBoard("out.cgi?p=" + id + "&u=" + url);
}

function PortableCopyToClipBoard(data)
{
	if(window.clipboardData)
		window.clipboardData.setData("Text", data);
	else if(window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext = data;
		str.data = copytext;
		trans.setTransferData("text/unicode", str, copytext.length*2);
		
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}

function collapseTable(myRows,colorSwap)
{                                                                    //VARIABLES:
	var myHide    = myRows + '_hide';                                //generate id of "collapse [-]" div
	var myExpand  = myRows + '_expand';                              //generate id of "expand [+]" div
	var myTotal   = myRows + '_total';                               //generate id of "total" row

    if (document.getElementById(myRows).style.display != "none"){    //COLLAPSE HAS BEEN PRESSED:
		document.getElementById(myRows).style.display = "none";      //hide specified row group
		document.getElementById(myHide).style.display = "none";      //hide the "collapse [-]" div
		document.getElementById(myExpand).style.display = "";        //show the "expand [+]" div
		
		if (colorSwap){                                              //if color swapping is required
			document.getElementById(myTotal).className = "darker";   //convert "total" row to darker on collapse
			document.getElementById(myTotal).onmouseout = function(){this.className="darker";};
			document.getElementById(myTotal).ondblclick = function(){handleDoubleClick(this, "darker");};
		}
	}
	else{                                                            //EXPAND HAS BEEN PRESSED:
		document.getElementById(myRows).style.display = "";          //show specified row group
		document.getElementById(myHide).style.display = "";          //show the "collapse [-]" div
		document.getElementById(myExpand).style.display = "none";    //hide the "expand [+]" div
		
		if (colorSwap){                                              //if color swapping is required
			document.getElementById(myTotal).className = "lighter";  //convert "total" row back to lighter on expand
			document.getElementById(myTotal).onmouseout = function(){this.className="lighter";};
			document.getElementById(myTotal).ondblclick = function(){handleDoubleClick(this, "lighter");};
		}
    }
}

function handleDoubleClick(obj, origClass)
{
    if(obj.onmouseover){
        obj.onmouseover = "";
        obj.onmouseout  = "";
        obj.className   = "double_clicked";
    }
    else{
        obj.onmouseover = function(){this.className='highlighted';};
        obj.onmouseout  = function(){this.className=origClass;};
        obj.className   = origClass;
    }
}

function resizeWindow(width,height){
	var par = window;
	var myWidth = 0, myHeight = 0;
	var LeftPosition = 0, TopPosition = 0;
	
	if(par.opener){par = window.opener;}
	
	if( typeof( window.innerWidth ) == 'number' ){
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		LeftPosition = (par.innerWidth-width)/2;
		TopPosition = (par.innerHeight-height)/2;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		LeftPosition = (par.document.documentElement.clientWidth-width)/2;
		TopPosition = (par.document.documentElement.clientHeight-height)/2;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		LeftPosition = (par.document.body.clientWidth-width)/2;
		TopPosition = (par.document.body.clientHeight-height)/2;
	}
	
	window.moveTo(LeftPosition, TopPosition);
	window.resizeBy(width-myWidth, height-myHeight);
}

function popUp(URL,width,height,i){
	day = new Date();
	id = day.getTime();
	
	if( typeof( window.innerWidth ) == 'number' ){
		//Non-IE
		LeftPosition = (window.innerWidth-width)/2;
		TopPosition = (window.innerHeight-height)/2;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
		//IE 6+ in 'standards compliant mode'
		LeftPosition = (document.documentElement.clientWidth-width)/2;
		TopPosition = (document.documentElement.clientHeight-height)/2;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		//IE 4 compatible
		LeftPosition = (document.body.clientWidth-width)/2;
		TopPosition = (document.body.clientHeight-height)/2;
	}
	if(i){attr = 'resizable=yes,scrollbars=yes,';}
	else{attr = 'resizable=no,scrollbars=no,';}
	attr += 'width=' + width + ',height=' + height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' + TopPosition + '';
	window.open(URL, id, attr);
}

function popUpWithPost(URL,width,height,i,keys,values)
{
	day = new Date();
	id = day.getTime();
	
	if( typeof( window.innerWidth ) == 'number' ){
		//Non-IE
		LeftPosition = (window.innerWidth-width)/2;
		TopPosition = (window.innerHeight-height)/2;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
		//IE 6+ in 'standards compliant mode'
		LeftPosition = (document.documentElement.clientWidth-width)/2;
		TopPosition = (document.documentElement.clientHeight-height)/2;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		//IE 4 compatible
		LeftPosition = (document.body.clientWidth-width)/2;
		TopPosition = (document.body.clientHeight-height)/2;
	}
	if(i){attr = 'resizable=yes,scrollbars=yes,';}
	else{attr = 'resizable=no,scrollbars=no,';}
	attr += 'width=' + width + ',height=' + height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' + TopPosition + '';
	var PopUp = window.open(URL, id, attr);
	var html = "";
	html += "<html><head></head><body><form id=\"tmpform\" method=\"POST\" action=\"" + URL + "\">";
	if (keys && values && (keys.length == values.length))
		for (var i=0; i < keys.length; i++)
			html += "<input type=\"hidden\" name=\"" + keys[i] + "\" value=\"" + values[i] + "\">\n";
	html += "</form><script type=\"text/javascript\">document.getElementById('tmpform').submit();</script></body></html>";
	PopUp.document.write(html);
	return PopUp;
}

function checkChange(boxid){
	var checkImage = document.getElementById('checkImage_'+boxid);
	var checkBox   = document.getElementById(boxid);
	if(checkBox.checked){
		checkBox.checked = '';
		checkImage.src = checkImage.src.replace('/checked', '/unchecked');
	}
	else{
		checkBox.checked = 'checked';
		checkImage.src = checkImage.src.replace('/unchecked', '/checked');
	}
}

function resetDisplayDate(id){
	if(confirm("Are you sure you wish to reset gallery "+id+"'s Display Date?")){
		img = new Image();
		img.src = "index.cgi?action=resetdisplaydate&id="+id;
		alert("Gallery "+id+"'s Display Date has been reset.");
	}
}

function AddRow(content, padding, icon)
{
	var newDiv = document.createElement("div");
	if(icon == 1){newDiv.innerHTML = "<font style=\"color: 00FF00; letter-spacing: -3px; padding-right: 8px;\">v/</font>";}
	else if(icon == 2){newDiv.innerHTML = "<font style=\"color: FF0000; padding-right: 6px;\">X</font>";}
	else if(icon == 3){newDiv.innerHTML = "<font style=\"color: 00CCFF; padding-left: 2px; padding-right: 7px;\"><strong>&rsaquo;</strong></font>";}
	newDiv.innerHTML += "<font style=\"padding-left: " + padding + "px;\">" + content + "</font>";

	var objDiv = document.getElementById("progress_report");
	objDiv.appendChild(newDiv);
	objDiv.scrollTop = objDiv.scrollHeight;
}

function handleResize(){
	var oTable = document.getElementById('imagetable');
	if(oTable){
		if( typeof( window.innerWidth ) == 'number' ){
			//Non-IE
			oTable.style.width = window.innerWidth + 'px';
			oTable.style.height = window.innerHeight + 'px';
		}
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
			//IE 6+ in 'standards compliant mode'
			oTable.style.width = document.documentElement.clientWidth + 'px';
			oTable.style.height = document.documentElement.clientHeight + 'px';
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
			//IE 4 compatible
			oTable.style.width = document.body.clientWidth + 'px';
			oTable.style.height = document.body.clientHeight + 'px';
		}
	}
}

function popChart(query_string, forceRefresh){
	//Elements
	var ImgUrl="charts.cgi";
	var oBody=document.getElementsByTagName("body")[0];
	var oTable=document.createElement("table");
	var oTbody=document.createElement("tbody");
	var oTR=document.createElement("tr");
	var oTD=document.createElement("td");
	var oImg=document.createElement("img");
	var oSpan=document.createElement("span");
	
	if(query_string.indexOf('&') == 0)
		query_string = query_string.replace(/&/, "");
	
	if(query_string != '')
		ImgUrl += '?' + query_string;
	
	//Properties
	oTable.id = "imagetable";
	oTable.border = "0";
	oTable.cellPadding = "0";
	oTable.cellSpacing = "0";
	oTable.style.position = 'absolute';
	oTable.style.left = '0px';
	oTable.style.zIndex = 100;
	oTD.style.backgroundImage = "url(/swiftclicks/images/bg_chart.png)";
	oTD.align = 'center';
	oTD.valign = 'middle';
	oSpan.align = 'center';
	oSpan.innerHTML = '<br><br><select style="width: 150px;" onChange="switchChart(this.options[this.selectedIndex])"><option value="60" selected>60 Minute Totals</option><option value="720">12 Hour Totals</option><option value="1440">24 Hour Totals</option></select>';
	
	if(typeof(window.pageYOffset)=='number'){
		var pageYOffset = window.pageYOffset;
		oTable.style.top = window.pageYOffset + 'px';
		window.onscroll = function(){window.scrollTo(0, pageYOffset);};
		oImg.onclick = function(){
			document.getElementsByTagName("body")[0].removeChild(document.getElementById('imagetable'));
			window.onscroll = null;
			};
	}
	else{
		var pageYOffset = document.body.scrollTop;
		oTable.style.top = document.body.scrollTop + 'px';
		window.onscroll = function(){window.scrollTo(0, pageYOffset);};
		oImg.onclick = function(){
			document.getElementsByTagName("body")[0].removeChild(document.getElementById('imagetable'));
			window.onscroll = null;
			};
	}
	
	if( typeof( window.innerWidth ) == 'number' ){
		//Non-IE
		oTable.style.width = window.innerWidth + 'px';
		oTable.style.height = window.innerHeight + 'px';
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
		//IE 6+ in 'standards compliant mode'
		oTable.style.width = document.documentElement.clientWidth + 'px';
		oTable.style.height = document.documentElement.clientHeight + 'px';
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		//IE 4 compatible
		oTable.style.width = document.body.clientWidth + 'px';
		oTable.style.height = document.body.clientHeight + 'px';
	}
	
	// image stuff
	if(forceRefresh){
		ImgUrl += query_string == '' ? '?' : '&';
		ImgUrl += 'refresh=' + Math.random();
	}
	oImg.id = 'chartimage';
	oImg.setAttribute('src', ImgUrl);
	//oImg.setAttribute('alt', 'alternate text');
	//oImg.setAttribute('height', '50px');
	//oImg.setAttribute('width', '100px');
	oImg.setAttribute('border', '0px');
	
	// append
	oTD.appendChild(oImg);
	oTD.appendChild(oSpan);
	oTR.appendChild(oTD);
	oTbody.appendChild(oTR);
	oTable.appendChild(oTbody);
	oBody.appendChild(oTable);
}

function switchChart(selOption)
{
	var start_qs = 0;
	var chartimage = document.getElementById('chartimage');
	
	start_qs = chartimage.src.indexOf("?");
	
	if(start_qs == -1)
		chartimage.src += "?";
	
	if(chartimage.src.indexOf("totals=") != -1)
		chartimage.src = chartimage.src.replace(/totals=[0-9]*/gi, "totals=" + selOption.value);
	else{
		if(start_qs != -1)
			chartimage.src += "&";
		
		chartimage.src += "totals=" + selOption.value;
	}
}

window.onresize = handleResize;
