var gapDHTMLWin;
function doGacAction(gacaction,par1,val1){
    gacaction = '/advertisers/gac/'+gacaction;
	pars = '';
	pars += par1 + '=' + val1;
	var opt = {
		asynchronous: true,
		encoding: 'UTF-8',
		contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
	    method: "post",
	    postBody: pars ,
	    onSuccess:
	    	function(t) {
	    		// Do something on success

	    		//document.getElementById("gap-wrapper").innerHTML = t.responseText;
				if (gacaction!='toggletips'){
					loadgacWidget();
				}
	    	},
	    on404:
	    	function(t) {
		        alert("Error 404: Error accessing server.");
		    },
	    onFailure:
	    	function(t) {
	        	alert("Error 500: ->"  + t.status + ': ' + t.statusText );

		   	}
	}

	new Ajax.Request(gacaction , opt);
	// Update the dynamic preview as soon as the content is submitted
}
function printPartOfPage(elementId)
{
	var printContent = document.getElementById(elementId);
	var windowUrl = 'about:blank';
	var windowName = 'Print' + new Date().getTime();
	var printWindow = window.open(windowUrl, windowName, 'left=0,top=0,width=0,height=0');

	printWindow.document.write(printContent.innerHTML);
	printWindow.document.getElementById('print_span').style.display='none';
	printWindow.document.close();
	printWindow.focus();
	printWindow.print();
	printWindow.close();
}

function togglegacWidget(dest){
	doGacAction('togglewidget','dest',dest);
}
function gacGoNext(){
	if (gapDHTMLWin) dhtmlwindow.close(gapDHTMLWin);
	doGacAction('gotonextstep');
}
function gacAdBuiltOff(){
	window.location = '/advertisers';
}
function gacMediaSelectedOff(){
	window.location = '/advertisers';
}
function gacGoPrev(){
	if (gapDHTMLWin) dhtmlwindow.close(gapDHTMLWin);
	doGacAction('gotoprevstep');
}
function loadgacWidget(popupId) {
	// Submit the content, and handle response
    if(popupId==null)
	{
		popupId='';
	}
	var opt = {
		asynchronous: true,
		encoding: 'UTF-8',
		asynchronous: true,
		contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
	    method: "post",
	    postBody: 'popupId='+popupId ,
	    onSuccess:
	    	function(t) {
	    		// Do something on success
                responsestr = t.responseText;
				var arr = new Array();
				arr = responsestr.split("@@@");
				if (arr[0] != ' ') {
					document.getElementById("gap-wrapper").innerHTML = arr[0];
				}
				if (arr[1]!=' '){
				    document.getElementById("gapTips").innerHTML=arr[1];
					if (document.getElementById("gapTipDHTML")){
						gapDHTMLWin.load('div', 'gapTips', 'Usage Tips');
					}
				}								
                if(document.getElementById("suggest_gac")){
					if (document.getElementById("gac_checkbox").checked){
						document.getElementById("suggest_gac").style.display='none';
					}else{
						//if (checkCookie()){
						if (false){
						document.getElementById("suggest_gac").style.display='inline';
						}
					}
				}
				if (arr[2])
				{
				     //alert('You are now in Guide Ad Creation mode.\n\nWe are redirecting you to the logical next step for your current status,\nbut if that is not where you want to be, please turn off the Guided Ad Creation ,\nusing the [checkbox] option above.');
					document.location=arr[2];
				}else{
					ToggleTips();
				}
	    	},
	    on404:
	    	function(t) {
		        alert("Error 404: Error getting Widget.");
		    },
	    onFailure:
	    	function(t) {
	        	alert("Error 500: ->"  + t.status + ': ' + t.statusText );

		   	}
	}
	new Ajax.Request('/advertisers/gac/getwidget' , opt);
	// Update the dynamic preview as soon as the content is submitted

}
function ToggleTips(tflag){
	if (document.getElementById("gapCollapse")&&(document.getElementById("toggleUsageTips").checked||tflag)) {
		var obj = document.getElementById('gapTips');
		obj.style.filter = "alpha(opacity=1)"; // IE
		obj.style.opacity = 0.01; // Firefox, etc..
		obj.style.display = '';
		xheight=obj.offsetHeight;


		obj.style.display = 'none';
		obj.style.filter = "alpha(opacity=100)"; // IE
		obj.style.opacity = 1; // Firefox, etc..


	 gapDHTMLWin=dhtmlwindow.open('gapTipDHTML', 'div', 'gapTips', 'Usage Tips ', 'width=300px,height='+xheight+'px,right=0;top=150px,resize=1,scrolling=0');
	 document.getElementById('gapTipDHTML').style.left="";
	 document.getElementById('gapTipDHTML').style.right="0";
	}

}
function ToggleTipsCheck(tflag){
	if (tflag){
		ToggleTips()
	}else{
		gapDHTMLWin.hide();
	}
	doGacAction('toggletips','tflag',tflag);
}
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Ted Man |  */

function getCookie(c_name) {
		if (document.cookie.length>0) {
			 c_start=document.cookie.indexOf(c_name + "=")
			 if (c_start!=-1) {
    		c_start=c_start + c_name.length+1 
    		c_end=document.cookie.indexOf(";",c_start)
    		if (c_end==-1) c_end=document.cookie.length
    				return unescape(document.cookie.substring(c_start,c_end))
      } 
  		}
		return ""
}
	
function setCookie(c_name,value,expiredays) {
		var exdate=new Date()
		exdate.setDate(exdate.getDate()+expiredays)
		document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}
		
function checkCookie() {
		var todaysdate = new Date()
		var day = todaysdate.getDay()
		
		switch (day) {
			 case 1:
					 day = "Monday"
					 break
			 case 2:
					 day = "Tuesday"
					 break
			 case 3:
					 day = "Wednesday"
					 break
			 case 4:
					 day = "Thursday"
					 break
			 case 5:
					 day = "Friday"
					 break
			 case 6:
					 day = "Saturday"
					 break
			 case 0:
					 day = "Sunday"
					 break
		}
		
  var thedate = getCookie('thedate')
		xreturn=false;
		if (thedate != null && thedate != "") {
			 if (day == thedate) {
			 } else {
				  xreturn=true;
			 }
		} else {
 			thedate = day

 			if (thedate!=null && thedate!="") {
    		setCookie('thedate', thedate, 365)
    		xreturn=true;
    }
  }
  return xreturn;
}


