var browserCheck = (document.all) ? 1 : 0;

function changeOrderStatus(orderId,status){
     var res = document.getElementById("campaignTable");  
     PageMethods.ChangeOrderStatus(orderId,status,OnchangeOrderStatusComplete,callError,res); 
}

function OnchangeOrderStatusComplete(result,res,methodName){
 if (result.length >0){
      res.innerHTML = result;
   }else{ alert('Status cannot be changed.'); }
}

//-------------------------------------------------------------------------------------------------------------
function saveCampaign(memberId,campaignId)
{
    var cName =  document.getElementById('txtCampaignName').value;

    if (cName.length > 0){
     document.getElementById('ajxIMG').style.visibility = 'visible';
     var res = document.getElementById("campaignTable");  
     PageMethods.WriteCampaign(memberId,campaignId,cName,OnSaveCampaignComplete,callError,res); 
    }else{
     alert('Please enter a name for this campaign.');
    }
}

//--new campaign creation - 
function OnSaveCampaignComplete(result,res,methodName){  
     res.innerHTML = result;
     closeDialog();
}

//-------------------------------------------------------------------------------------------------------------
function deleteCampaign(campaignId,memberId){
   var res = document.getElementById("campaignTable"); 
   PageMethods.DeleteCampaign(campaignId,memberId,OnDeleteCampaignComplete,callError,res); 
}

//--if delete is sucessful result will be the html to populate the table span
function OnDeleteCampaignComplete(result,res,methodName){ 
   if (result.length >0){
      res.innerHTML = result;
   }else{ alert('You cannot delete a campaign with orders linked to it. Move the orders to another campagin, then delete.'); }
}
//------------------------------------------------------------------------------------------------------------


function newCampaign(memberId){
  var width  = '350px';
  var height = '200px';

  document.getElementById('dlgTitle').innerHTML = "Create New Campaign";


  document.getElementById('dlgArea').innerHTML = 
                  "<table style='width:100%; height:" + height +  "; font-size: 10pt;border-top: gainsboro 1px solid;border-bottom: gainsboro 1px solid; color: #393938;'>" +
                   "<tr><td align=right >" +
                   "</td></tr><tr><td height='35px'  valign=top colspan='2' >" +
                   "Campaigns enable you to group orders together, for easy management for delivery across multiple sites.  <A HREF='help.aspx?topic=Campaign' target='_blank' >Learn more</a>" +
                   "</td>" +
                   "</tr><tr><td align='right' width='150px' style='font-size:9pt;' >Campaign Name:</td>" +
                   "<td >" +
                   "<input id='txtCampaignName' maxlength=50  width='175px' style='font-size:10pt;width:175px;border-right: gainsboro 1px solid; border-top: gainsboro 1px solid; " +
                   " border-left: gainsboro 1px solid; border-bottom: gainsboro 1px solid; height: 15px' type='text' />" +
                   "</td></tr>" +
                   "<tr><td></td><td style='height: 20px' align='right' valign='bottom'  >" +
                   "<img id='ajxIMG' align='abstop' style='visibility:hidden' src='i/loader.gif' />&nbsp;&nbsp;" +  
                   "<a href='javascript:saveCampaign(" + memberId + ",0)' ><img border='0'  src='i/button_save.gif' /></a>&nbsp;&nbsp;" + 
                   "<a href='javascript:closeDialog()'  ><img border='0' src='i/button_cancel.gif' /></a>&nbsp;&nbsp;" +
                   "</td></tr></table>";
                  
  
    showDialog(width,height);
    
}

//--edit campaign name
function editCampaign(memberId,campaignId,name){
  var width  = '350px';
  var height = '200px';

  document.getElementById('dlgTitle').innerHTML = "Edit Campaign - " + name;
    
  document.getElementById('dlgArea').innerHTML = 
                  "<table style='width:100%; height:" + height +  "; font-size: 10pt;border-top: gainsboro 1px solid;border-bottom: gainsboro 1px solid; color: #393938;'>" +
                  "<tr><td align=right >" +
                   "</td></tr><tr><td height='35px'  valign=top colspan='2' >" +
                   "Campaigns enable you to group orders together, for easy management for delivery across multiple sites.  <A HREF='help.aspx?topic=Campaign' target='_blank' >Learn more</A><br />" +
                   "</td>" +
                   "</tr><tr><td  align='right' width='150px' style='font-size:9pt;' >Campaign Name:</td>" +
                   "<td >" +
                   "<input id='txtCampaignName' maxlength=50  width='175px' style='font-size:10pt;width:175px;border-right: gainsboro 1px solid; border-top: gainsboro 1px solid; " +
                   " border-left: gainsboro 1px solid; border-bottom: gainsboro 1px solid; height: 15px' type='text' value='" + name + "' />" +
                   "</td></tr>" +
                   "<tr><td></td><td style='height: 20px' align='right' valign='bottom' >" +
                   "<img id='ajxIMG' align='abstop' style='visibility:hidden' src='i/loader.gif' />&nbsp;&nbsp;" +  
                   "<a href='javascript:saveCampaign(" + memberId + "," + campaignId + ")' ><img border='0'  src='i/button_save.gif' /></a>&nbsp;&nbsp;" + 
                   "<a href='javascript:closeDialog()'  ><img border='0' src='i/button_cancel.gif' /></a>&nbsp;&nbsp;" +
                   "</td></tr></table>";
  
    showDialog(width,height);
  
}

//---------------------------------------------------------------------------------------------------------------------

//--Enables user to edit an order -  can only change delivery type
function editOrder(orderId){
   var res = document.getElementById("campaignTable"); 
   PageMethods.FetchOrder(orderId,OnFetchOrderComplete,callError,res); 
}


//--show edit dialog
function OnFetchOrderComplete(result,res,methodName){  
     var width  = '400px';
     var height = '250px';
         
     document.getElementById('dlgTitle').innerHTML = "Edit Order - " + result.Desc;
  
     document.getElementById('dlgArea').innerHTML = 
                   "<table style='width:100%; height:" + height +  "; font-size: 10pt;border-top: gainsboro 1px solid;border-bottom: gainsboro 1px solid; color: #393938;'>" +
                  "<tr><td height='35px'  valign=top colspan='2' >" +
                   "Change this Order's Target url, Delivery speed & campaign Assignment.<A HREF='help.aspx?topic=Order' target='_blank' >Learn more</A><br />" +
                   "</td></tr>" +
                   "<tr><td align='right' width='100px' style='font-size:9pt;'  >Target Url:</td>" +
                   "<td width='300px' >" +
                   "<input id='txtTargetUrl' maxlength=1000  width='275px' style='font-size:10pt;width:275px;border-right: gainsboro 1px solid; border-top: gainsboro 1px solid; " +
                   " border-left: gainsboro 1px solid; border-bottom: gainsboro 1px solid; height: 15px' type='text' value='" + result.TargetUrl + "' />" +
                   "</td></tr>" +
                   "<tr><td align='right' width='100px' style='font-size:9pt;' >Max daily visitors:</td>" +
                   "<td width='300px' >" +
                   result.cbDayMax +
                   "</td></tr>" +
                   "<tr><td align='right' width='100px' style='font-size:9pt;' >Campaign:</td>" +
                   "<td width='300px' >" +
                   result.cbCampaign +
                   "</td></tr>" +
                   "<tr><td></td><td style='height: 20px' align='right' valign='bottom' >" +
                   "<img id='ajxIMG' align='abstop' style='visibility:hidden' src='i/loader.gif' />&nbsp;&nbsp;" +  
                   "<a href='javascript:saveOrder(" + result.Id +")' ><img border='0'  src='i/button_save.gif' /></a>&nbsp;&nbsp;" + 
                   "<a href='javascript:closeDialog()'  ><img border='0' src='i/button_cancel.gif' /></a>&nbsp;&nbsp;" +
                   "</td></tr></table>";

     showDialog(width,height);

}



function saveOrder(orderId){
    var cUrl        =  document.getElementById('txtTargetUrl').value;
    var campaignId  =  document.getElementById('cbCampaign').options[document.getElementById('cbCampaign').selectedIndex].value; 
    var dayMax      =  document.getElementById('cbDayMax').options[document.getElementById('cbDayMax').selectedIndex].value; 

   if (cUrl.length > 0){
     document.getElementById('ajxIMG').style.visibility = 'visible';
     var res = document.getElementById("campaignTable"); 
     PageMethods.SaveOrder(orderId,cUrl,campaignId,dayMax,OnSaveOrderComplete,callError,res); 
    }else{
     alert('Please enter a valid target url for this order.');
    }
}


function OnSaveOrderComplete(result,res,methodName){  
     if (result.length > 0){
        res.innerHTML = result;
        closeDialog();
    }else{
    document.getElementById('ajxIMG').style.visibility = 'hidden';
     alert('Invalid url, please check.');
    // document.getElementById("txtTargetUrl").style.backColor = #
    }

}

//---------------------------------------------------------------------------------------------------------------------

//--Sets an opaque background behind dialog
function setBack()
{
 var opBack =  document.getElementById('back');
     opBack.style.visibility = 'visible';
     opBack.style.top = 0;
     opBack.style.left = 0;
     opBack.style.width = '100%';
     opBack.style.height = '100%';
}

//--removes back div
function clearBack() {document.getElementById('back').style.visibility = 'hidden';}

function showDialog(w,h){
    setBack();
    var dlg = document.getElementById('dialog');
    dlg.style.visibility = "visible";
    dlg.style.zIndex = 1000;
    dlg.style.display = "block";
   // dlg.style.left = "50%";
   // dlg.style.top = "50%";
    dlg.style.height = h;
    dlg.style.width = w;
    
   // var x = (window.innerWidth / 2) - (dlg.offsetWidth / 2);
   // var y = (window.offsetHeight / 2) - (dlg.offsetHeight / 2); 
  //  dlg.style.marginLeft = "-" + parseInt(dlg.offsetWidth / 2) + "px";
  //  dlg.style.marginTop = "-" + parseInt(dlg.offsetHeight / 2) + "px";
}

function closeDialog(){
     document.getElementById('dialog').style.visibility = 'hidden';
     clearBack();
     document.getElementById('ajxIMG').style.visibility = 'hidden';
}


function callError(error,userContext,methodName){  
if(error !== null){   alert(error.get_message()); 
}  }



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}


//////////////////Div drag and drop////////////////////////////////////////////////////////////////////////

// Determine browser and version.
function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {


//dragObj.elNode.style.left =0;
//dragObj.elNode.style.top =0;


  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  
  

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;
  
  

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function GetTodayClicks(){  var res = document.getElementById("clicksToday");  PageMethods.GetClickCount("0",OnGetTodayClicksComplete,callError,res);  }
function OnGetTodayClicksComplete(result,res,methodName){  res.innerHTML = result;  tId = setTimeout("GetTodayClicks()",2000);    }
function callError(error,userContext,methodName){  
if(error !== null){  
//alert(error.get_message()); 
}  }



//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//flow manager

function createFlow(memberId)
{   
  var width  = '350px';
  var height = '200px';

  document.getElementById('dlgTitle').innerHTML = "Create New Flow";


  document.getElementById('dlgArea').innerHTML = 
                  "<table style='width:100%; height:" + height +  "; font-size: 10pt;border-top: gainsboro 1px solid;border-bottom: gainsboro 1px solid; color: #393938;'>" +
                   "<tr><td align=right >" +
                   "</td></tr><tr><td height='35px'  valign=top colspan='2' >" +
                   "Create a traffic flow from your website. All clicks sent to your Flow Url will be credited to your account. <A HREF='help.aspx?topic=Flow' target='_blank' >Learn more</a>" +
                   "</td></tr>" +
                   "<tr><td align='right' width='150px' style='font-size:9pt;' >Site Domain(e.g mysite.com):</td>" +
                   "<td>" +
                   "<input id='txtDomain' maxlength=50  width='175px' style='font-size:10pt;width:175px;border-right: gainsboro 1px solid; border-top: gainsboro 1px solid; " +
                   " border-left: gainsboro 1px solid; border-bottom: gainsboro 1px solid; height: 15px' type='text' />" +
                   "<span id=domError></span>" +
                   "</td></tr>" +
                   "<tr><td align='right' width='150px' style='font-size:9pt;' >Return Url(e.g mysite.com/in.php):</td>" +
                   "<td>" +
                   "<input id='txtReturnUrl' maxlength=50  width='175px' style='font-size:10pt;width:175px;border-right: gainsboro 1px solid; border-top: gainsboro 1px solid; " +
                   " border-left: gainsboro 1px solid; border-bottom: gainsboro 1px solid; height: 15px' type='text' />" +
                   "<span id=rtnUrlError></span>" +
                   "</td></tr>" +
                   "<tr><td></td><td style='height: 20px' align='right' valign='bottom'  >" +
                   "<img id='ajxIMG' align='abstop' style='visibility:hidden' src='i/loader.gif' />&nbsp;&nbsp;" +  
                   "<a href='javascript:saveFlow(" + memberId + ",0)' ><img border='0'  src='i/button_save.gif' /></a>&nbsp;&nbsp;" + 
                   "<a href='javascript:closeDialog()'  ><img border='0' src='i/button_cancel.gif' /></a>&nbsp;&nbsp;" +
                   "</td></tr></table>";
  
    showDialog(width,height);

}

//-------------------------------------------------------------------------------------------------------------
function saveFlow(memberId,flowId)
{
    var cDomain    =  document.getElementById('txtDomain').value;
    var cReturnUrl =  document.getElementById('txtReturnUrl').value;

    if (cDomain.length > 0 && cReturnUrl.length > 0){
     document.getElementById('ajxIMG').style.visibility = 'visible';
     var res = document.getElementById("flowsTable");  
     PageMethods.WriteFlow(memberId,flowId,cDomain,cReturnUrl,cReturnUrlOnSaveFlowComplete,callError,res); 
    }else{
     alert('Please enter the domain where the traffic is comming from and the return url where unsold clicks should be returned to.');
    }
}

//--new campaign creation - 
function cReturnUrlOnSaveFlowComplete(result,res,methodName){  
     
     if(result[0] == "true" && result[1] == "true")
     {
        res.innerHTML = result[2];
        
        closeDialog();
        
     }     
     else
     {  
         document.getElementById('ajxIMG').style.visibility = 'hidden';
         
         if (result[0] == "false")
         {
         document.getElementById('txtDomain').style.backgroundColor = '#CD2626';
         document.getElementById('domError').innerHTML = "Invalid Domain!";
         }
         
         if (result[1] == "false")
         {
         document.getElementById('txtReturnUrl').style.backgroundColor = '#CD2626';
         document.getElementById('rtnUrlError').innerHTML = "Invalid url!";
         }
         
         
         
     }
     
     
}

//-------------------------------------------------------------------------------------------------------------
var show;
function exp()
{
   if (show == null || show == true){
    document.getElementById('info').style.visibility = 'hidden';
    show = false;
   }else
   {
     document.getElementById('info').style.visibility = 'visible';
     show = true;
   }
}

//----------------------------------------------------------------------------------------------------------
//get ScreenCapForPop

//-------------------------------------------------------------------------------------------------------------
function GetScreenCap(domainUrl,flowId){
   var res = document.getElementById("imgCap_" + flowId); 
   PageMethods.GetScreenCap(domainUrl,OnGetScreenCapComplete,callError,res); 
}

//--if delete is sucessful result will be the html to populate the table span
function OnGetScreenCapComplete(result,res,methodName){ 
       res.innerHTML = result;
}
//------------------------------------------------------------------------------------------------------------








