// JavaScript Document
popper="";
window.addEvent('domready', function(){
	SqueezeBox.assign($$('a.boxed'), {
		parse: 'rel'
	});
});
function refreshSB(){
	SqueezeBox.assign($$('a.boxed'), {
		parse: 'rel'
	});
}
function expander(div){
	el=$(div+'Content').getSize();
	thisHeight=el.y+"px";
	$(div+'Box').tween('height',thisHeight);
}
function activateSection(div){
	expander(div);
	$(div).value="1";
	savePromotion(div);
}
function deactivateSection(div){
	$(div+'Box').tween('height',"0px");
	$(div).value="0";
	savePromotion(div);
}
function updateRegistrationField(field,s){
	$(field).value=s;
	savePromotion(field);
}
function uploadDone(i){
		var imager = new Request({url:'get_image_path.php'});
		imager.onSuccess=function(reply){
			if(i=="background"){
				window.document["previewSWF"].updateBG(reply);
			}else{
				$('imageBox').innerHTML=reply;
			}
			checkStatus();
		}
		imager.send("i="+i);
}
function getChecks(div,name){
	radios=$(div).getElements('input[name^='+name+']');
	checker=new Array();
	ids=new Array();
	for(i=0;i<radios.length;i++){
		checker[i]=radios[i].checked;
		ids[i]=radios[i].id;
	}
}
function setChecks(){
	for(i=0;i<radios.length;i++){
		$(ids[i]).checked=checker[i];
	}
}
function checkStatus(){
	$('mnav').checkStatus();
}
function preview(dir){
	disableGravy();
	previewer(dir);
}
function previewer(dir){
	popper=window.open("promo/preview.php?p="+dir,"GRAVY")
}
function launch(){
	SqueezeBox.open('pop_launch.php',{handler:'iframe',size:{x:560,y:500}});
}
function lockPromo(id){
	var lock = new Request({url:'lock_promo.php'});
	lock.onSuccess=function(response){
		window.parent.location="manage_promotions.php";
	}
	lock.send("id="+id);
}
//manage
function copyPromotion(id){
	alert("copy promo: "+id);
}
function reportPromotion(id){
	alert("report promo: "+id);
}
///////
function saveLimitation(id,field,value){
	var save = new Request({url:'limitation_save.php'});
	save.onSuccess=function(reply){
		window.parent.location.reload(true);
	}
	save.send("id="+id+"&field="+field+"&value="+value);
}
//promotion
function savePromotion(field){
	data=$(field).value;
	var save = new Request({url:'promotion_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	save.send(field+"="+data);
}
function saveColor(field,val){	
	var save = new Request({url:'promotion_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
		window.document["previewSWF"].updateColor(field,val);
	}
	save.send(field+"="+val);
}
function savePromotionFriend(val){
	var save = new Request({url:'promotion_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	save.send("sendActive="+val);
}
function saveMCE(o) {
	var save = new Request({url:'promotion_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	data=escape(o.getBody().innerHTML);
	save.send(o.id+"="+data);
}
//
function deletePromotion(pID){
	if(confirm("Are you sure you want to delete this promotion?")){
		var deleter = new Request({url:'promotion_delete.php'});
		deleter.onSuccess=function(reply){
			location.reload(true);
		}
		deleter.send("id="+pID);
	}
}
//survey
function saveSurvey(id){
	var save = new Request({url:'survey_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	data=$("question"+id).value;
	save.send("id="+id+"&question="+data);
}
function saveSurveyType(type,id){
	var save = new Request({url:'survey_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	save.send("id="+id+"&type="+type);
}
function saveSurveyOther(val,id){
	var save = new Request({url:'survey_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	save.send("id="+id+"&other="+val);
}
function deleteSurvey(id){
	if(confirm("Are you sure you want to delete this Survey Question and all the Responses it contains?")){
		var deleter = new Request({url:'survey_delete.php'});
		deleter.onSuccess=function(reply){
			//location.reload(true);
			$('survey'+id).destroy();
			expander("surveyActive");
			checkStatus();
			saved();
		}
		deleter.send("id="+id);
	}
}
function addSurveyQuestion(type){
	switch(type){
		case "multiple":
			surl="survey_new_multiple.php";
			break;
		case "text":
			surl="survey_new_text.php";
			break;
	}
	getChecks('surveyBody','type');
	var question=new Request({url:surl});
	question.onSuccess=function(reply){
		oc=$('surveyBody').innerHTML;
		alltext=oc+reply;
		$('surveyBody').innerHTML=alltext;
		surveySorter();
		//hack for calling existing JS
		function searchForSurveyIDs(offset){
			startPos=alltext.indexOf("startID:",offset)+8;
			if(startPos!=-1){
				endPos=alltext.indexOf(":endID",offset);
				diff=endPos-startPos;
				id=alltext.substr(startPos,diff);
				if(id!=""){
					responseSorter(id);
				}
				if(endPos!=-1){
					searchForSurveyIDs(endPos+50);
				}
			}
		}
		searchForSurveyIDs(0);
		expander("surveyActive");
		setChecks();
		checkStatus();
		saved();
		refreshSB();
	}
	question.send();
}
function surveySorter(){
	//alert("surveySorter");
	var surveySort = new Sortables('surveyBody', {
		constrain:false,
		handle:"div.surveyBoxHead",
		clone:true,
		opacity:.3,
		revert: { duration: 500, transition: 'expo:out' },
		onComplete:function(el){
			var surveyZ=new Request({url:'survey_z.php'});
			surveyZ.send("z="+surveySort.serialize());
			saved();
		}
	});
}
//response
function saveResponse(id){
	var save = new Request({url:'response_save.php'});
	save.onSuccess=function(reply){
		checkStatus();
		saved();
	}
	data=$("response"+id).value;
	save.send("id="+id+"&response="+data);
}
function deleteResponse(rID){
	if(confirm("Are you sure you want to delete this Responses?")){
		var deleter = new Request({url:'response_delete.php'});
		deleter.onSuccess=function(reply){
			//location.reload(true);
			$('responses'+rID).destroy();
			expander("surveyActive");
			checkStatus();
			saved();
		}
		deleter.send("id="+rID);
	}
}
function addSurveyResponse(id){
	var response=new Request({url:'response_new.php'});
	response.onSuccess=function(reply){
		$('responseBody'+id).set('html',$('responseBody'+id).get('html')+reply);
		responseSorter(id);
		expander("surveyActive");
		checkStatus();
		saved();
	}
	response.send("id="+id);
}
function responseSorter(id){
	//alert("responseSorter("+qID+")");
	var responseSort = new Sortables('responseBody'+id, {
		constrain:true,
		handle:"div.responseMove",
		clone:true,
		opacity:.3,
		revert: { duration: 500, transition: 'expo:out' },
		onComplete:function(el){
			var responseZ=new Request({url:'response_z.php'});
			responseZ.send("z="+responseSort.serialize());
			saved();
		}
	});
}
//webpage
function saveWebpage(id){
	var save = new Request({url:'webpage_save.php'});
	save.onSuccess=function(reply){
		//checkStatus();
		//webpageCheck(id);
		saved();
	}
	data=$("clue"+id).value;
	save.send("id="+id+"&clue="+data);
}
function saveWebpageHint(type,id){
	var save = new Request({url:'webpage_save.php'});
	save.onSuccess=function(reply){
		//checkStatus();
		//webpageCheck(id);
		saved();
	}
	save.send("id="+id+"&hint="+type);
}
function saveCoords(x1,y1,x2,y2,id){
	var save=new Request({url:'webpage_save.php'});
	save.onSuccess=function(reply){
		//checkStatus();
		//webpageCheck(id);
		saved();
	}
	save.send("id="+id+"&x1="+x1+"&y1="+y1+"&x2="+x2+"&y2="+y2);
}
function deleteWebpage(id){
	if(confirm("Are you sure you want to delete this Interactive Web Page?")){
		var deleter = new Request({url:'webpage_delete.php'});
		deleter.onSuccess=function(reply){
			//location.reload(true);
			$('webpage'+id).destroy();
			expander("webpageActive");
			checkStatus();
			saved();
		}
		deleter.send("id="+id);
	}
}
function addWebpage(){
	getChecks('webpageBody','hint');
	var web=new Request({url:'webpage_new.php'});
	web.onSuccess=function(reply){
		alltext=$('webpageBody').innerHTML+reply;
		$('webpageBody').innerHTML=alltext;
		function searchForWebpageIDs(offset){
			startPos=alltext.indexOf("startID:",offset)+8;
			if(startPos!=-1){
				endPos=alltext.indexOf(":endID",offset);
				diff=endPos-startPos;
				id=alltext.substr(startPos,diff);
				if(id!=""){
					webpageSWFs(id);
				}
				if(endPos!=-1){
					searchForWebpageIDs(endPos+50);
				}
			}
		}
		searchForWebpageIDs(0);
		webpageSorter();
		expander("webpageActive");
		checkStatus();
		saved();
		refreshSB();
	}
	web.send();
}
function webpageSorter(){
	var webpageSort = new Sortables('webpageBody', {
		constrain:true,
		handle:"div.webpageBoxHead",
		clone:true,
		opacity:.3,
		revert: { duration: 500, transition: 'expo:out' },
		onComplete:function(el){
			var webpageZ=new Request({url:'webpage_z.php'});
			webpageZ.send("z="+webpageSort.serialize());
			saved();
		}
	});
}
function webpageSWFs(id){
	new Swiff('assets/swf/webpage.swf',{
		id:'webpage'+id+'SWF', 
		width:'500px', 
		height:'95px', 
		container:'webpageController'+id, 
		vars:{wID:id}
	});
}
function screengrab(id){
	SqueezeBox.open('pop_webpage_grabber.php?id='+id,{handler:'iframe',size:{x:650,y:360}});
	//at end of success call "webpageCheck" in SWF
}
function define(id){
	SqueezeBox.open('pop_webpage_attributes.php?id='+id,{handler:'iframe',size:{x:1000,y:600},onClose:function(){setTimeout("webpageUpdate("+id+")",251);}});
}
//from swf
function webpageUpdate(id){
	webpageSWFs(id);
	checkStatus();
}
//
function pieChart(id){
	new Swiff('assets/swf/Pie2D.swf',{
		id:'pieChart'+id+'SWF', 
		width:'300px', 
		height:'170px', 
		container:'pieChart'+id, 
		vars:{dataURL:"xml_survey.php?qID="+id,chartWidth:250,chartHeight:170}
	});
}
function openReport(id){
	disableGravy();
	popper=window.open("reports.php?id="+id,"GRAVY");
}
function disableGravy(){
	SqueezeBox.open('pop_disable.php',{handler:'iframe',size:{x:400,y:250},onClose:function(){popper.close();}})
}
function reportExport(){}
function reportPrint(){
}
function reportChange(obj){
	id=obj.value;
	window.location="reports.php?id="+id;
}
function convertDate(s){
	var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	y=s.substring(0,4);
	//alert(s.substring(4,6)-1);
	m=s.substring(4,6)-1;
	d=s.substring(6,8)-1+1;
	if (d == 1 || d == 21 || d ==31){
		sf = "st";
	}else if (d == 2 || d == 22){
		sf = "nd";
	}else if (d == 3 || d == 23){
		sf = "rd";
	}else{
		sf = "th";
	}
	f=months[m]+" "+d+"<sup>"+sf+"</sup>, "+y;
	return f;
}
function imageFull(id){
	SqueezeBox.open('pop_webpage_view.php?id='+id,{handler:'iframe',size:{x:1000,y:600}});
}
function dateUpdate(type,d){
	var save = new Request({url:'report_date_update.php'});
	save.onSuccess=function(reply){
		if(reply=="updated"){
			window.location.reload(true);
		}
	}
	save.send("t="+type+"&d="+d);
}
function gotoCredit(){
	window.opener.location="credit.php";
	popper.close();
}
function expirePay(){
	var save = new Request({url:'report_expire_pay.php'});
	save.onSuccess=function(reply){
		if(reply=="paid"){
			updateCredit();
		}
	}
	save.send();
}
function updateCredit(w){
	var save = new Request({url:'credit_available.php'});
	save.onSuccess=function(reply){
		window.opener.$('availableCredit').innerHTML=reply;
		window.location.reload(true);
	}
	save.send();
}
function pageNext(p){
	p++;
	window.location="?p="+p;
}
function pagePrev(p){
	p--;
	window.location="?p="+p;
}
function pageNav(n){
	window.location="?p="+n;
}
function closeReports(){
	window.opener.focus();
	window.opener.SqueezeBox.close();
	window.close();
}
function exporter(){
	SqueezeBox.open('pop_export.php',{handler:'iframe',size:{x:600,y:400}});
}
function exportData(){
	type=getCheckedValue(document.forms['exportForm'].elements['fileType']);
	url="export"+type+".php";
	window.open(url);
}
function exportData(q){
	window.open("exportCSV.php?q="+q);
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function saved(){
	$('saved').fade('in');
	setTimeout(saveDone,1000);
}
function saveDone(){
	$('saved').fade('out');
}
function noLaunch(e){
	alert(e);
}
function checkPromoCode(){
	thisCode=$('promoCode').value;
	if(thisCode!=""){
		SqueezeBox.open('pop_promo_code.php?code='+thisCode,{handler:'iframe',size:{x:400,y:270}});
	}else{
		alert("No Promo Code!");
	}
}
function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}