//TODO add code to use the themes
var gjsGappCloseWindowImg = '<img src="includes/GappEdit/themes/gappedit/images/GappCloseWindow.png" alt="Click to Close" width="17" height="16" border="0">';
var gjsGappPrintWindowImg = '<img src="includes/GappEdit/themes/gappedit/images/GappPrintWindow.png" alt="Click to Print" width="18" height="17" border="0">';

/**
* The overlib popup conform box
**/
function confirmDialog_O(q, caption, link, modal){
	
	q = (q || ''); 
	caption = (caption || '');
	target = (link || '');
	modal = (modal || false);
	yesFunc = "confirmYes('"+target+"');";
//alert(yesFunc);

 var confirmDialog_O = q
  +'<form action="javascript:void(0);">'
  +'<input type="submit" '
  +'onclick="cClick(); '+yesFunc
  +'return false;" value=" Yes ">&nbsp;&nbsp;'
  +'<input type="submit" '
  +'onclick="cClick();'
  +'return false;" value=" No ">'
  +'<\/form>';		
//alert(myConfirmDialog);
	
	//Modify the styling appropriately for your site
	overlib(confirmDialog_O, EXCLUSIVEOVERRIDE,
	  caption?CAPTION:DONOTHING, caption?caption:DONOTHING, CLOSETEXT,'',
	  CAPTIONPADDING,4, TEXTPADDING,10, BORDER,2,
	  modal?MODAL:DONOTHING,
	  WRAP, STICKY, EXCLUSIVE, SCROLL, MIDX,0, MIDY,0);
	
} 

function confirmYes(link){
//http://localhost/~george/ravenGapp/modules.php?name=NAD&op=assets&subaction=deletesql&primary_key=id&primary_key_val=4&page=1&modheading=_ASSETS%3A%3A_DELETERECORD
//http://localhost/~george/ravenGapp/modules.php?name=Your_Account&op=new_user
//http://localhost/~george/phpMyAdmin/sql.php?db=ravenGapp&table=nuke_nad_assets&token=a1320a5b075cd2151a5812aad8b566c0&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60nuke_nad_assets%60+WHERE+%60nuke_nad_assets%60.%60id%60+%3D+4+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Fdb%3DravenGapp%26table%3Dnuke_nad_assets%26token%3Da1320a5b075cd2151a5812aad8b566c0%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560nuke_nad_assets%2560%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties_structure.php
//alert(escape(link));
	target = escape(link);
//alert(target);
	location.href = unescape(target);
}

/**
* This function is used to implement AJAX DB help for the GappEdit class
* It is called from GappFunctions.php/gappHelp()
*/
function AJAXHELP(pCaption){
	sCaption =(pCaption || 'Help');
	
//alert('AJAXHELP');
	return overlib(OLresponseAJAX, STICKY, BELOW, RIGHT, CAPTION, sCaption, STATUS, sCaption, 
  	CLOSETEXT, DRAGGABLE,DRAGCAP,gjsGappCloseWindowImg ,CLOSECLICK, WIDTH, 300, OFFSETY, 20, 
  	FGCOLOR, '#ffffff', BGCOLOR, '#000000', TEXTCOLOR, '#000000', CAPCOLOR, '#ffffff', 
  	CLOSECOLOR, '#ffffff', CAPICON, 'includes/GappEdit/themes/gappedit/images/GappHelp.png', 
  	BORDER, '2');
}

function AJAXPRINT() {
//alert('AJAXPRINT');
//alert(OLresponseAJAX);
  pWidth=800;
  pHeight=600;
  var sStyle='<style type=\"text/css\">.ovfl{width:'+pWidth+'px; height:'+pHeight+'px; border:1px none #000000; padding:6px; overflow:auto;}</style>';

  return overlib(sStyle+'<div class="ovfl" id="ovflo">'+OLresponseAJAX+'</div>',TEXTPADDING,0, 
    CAPTIONPADDING,4, CAPTION, 'P', CLOSETEXT, gjsGappCloseWindowImg,
    PRINT,PRINTTEXT,gjsGappPrintWindowImg,
    WRAP, BORDER,2,STICKY,CLOSECLICK,MIDX,0,RELY,100,DRAGGABLE,DRAGCAP);
    
}

function getBaseURL() {
   return location.protocol + "//" + location.hostname + 
      (location.port && ":" + location.port);// + "/";
}
//function getBaseURL() {
//    var url = location.href;  // entire url including querystring - also: window.location.href;
//    var baseURL = url.substring(0, url.indexOf('/', 14));
//
//    if (baseURL.indexOf('http://localhost') != -1) {
//        // Base Url for localhost
//        var url = location.href;  // window.location.href;
//        var pathname = location.pathname;  // window.location.pathname;
//        var index1 = url.indexOf(pathname);
//        var index2 = url.indexOf("/", index1 + 1);
//        var baseLocalUrl = url.substr(0, index2);
//
//        return baseLocalUrl + "/";
//    }
//    else {
//        // Root Url for domain name
//        return baseURL + "/";
//    }
//}

/**
* pSettings is the property setting for the Dialog
* "dialogType" : "html"(default), "ajax", "iframe"
* "dialogHREF" : "Nothing to Show"(default), 
* "title" : "Dialog"(default), 
* "width" : 300(default), 
* "height" : "auto"(default), 
*/
function gappShowDialog(pSettings){
  //alert("Test");return;
  try{
    var _dialog = $('<div></div>');
    var jsonOBJ = jQuery.parseJSON(pSettings);
    //alert(obj);
    dialogType = (jsonOBJ.dialogType||'html');
    dialogHREF = (jsonOBJ.dialogHREF||'Nothing to show');
    dialogTitle = (jsonOBJ.title||'Dialog');
    dialogWidth = (jsonOBJ.width||300);
    dialogHeight = (jsonOBJ.height||'auto');
    //alert(dialogType);
    if(dialogType == 'ajax'){
      _dialog = _dialog.load(dialogHREF, function(response, status, xhr){
        if (status == "error") {
          var msg = "Sorry but there was an error: ";
          $(this).html(msg + xhr.status + " " + xhr.statusText);
        }
      })
    }else if(dialogType == 'iframe'){
			_dialog = _dialog.html('<iframe id="modalIframeId" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" />')
			//$("#modalIframeId").attr("src",dialogHREF); 
    }else{//'html'
      _dialog = _dialog.html(dialogHREF);
    }
    //_dialog = _dialog.dialog({
    _dialog = _dialog.wijdialog({
				title: dialogTitle,
				width: dialogWidth,
				height: dialogHeight,
				modal: true,
				close:function(event, ui) { $(this).remove(); },
        captionButtons: {
            pin: { visible: false },
            refresh: { visible: false },
            toggle: { visible: false },
            minimize: { visible: false },
            maximize: { visible: true }
        }
    });
    
		$("#modalIframeId").attr("src",dialogHREF); 
    
    //return;
    
  }catch(e){
    alert(e);
  }
}

/**
* (jsonWinSettings) = '{REF:\'CENTER\'}' for setting the window center
*/
function showDialogIFRAME(pHREF,pCaption,pWidth,pHeight,jsonWinSettings){
//alert('jSettings:'+pHREF);return;
  if(isValidURL(pHREF)){//defined in gappJGappFunc.js
    var sPrintScript = pHREF;
  }else{
    var sPrintScript = '';
  }
  jsonWinSettings = (jsonWinSettings || '{}');
  eval('var _ws='+jsonWinSettings);
//alert('jSettings:'+sPrintScript);
//alert('jSettings:'+pHREF);
//alert('jSettingsPRINT: '+_ws.PRINT);
//alert('jSettingsPRINTTEXT: '+_ws.PRINTTEXT);
  sCaption = (pCaption || ' ');
  iWidth = (pWidth || 450);
  iHeight = (pHeight || 350);
  
  sFGColor=(_ws.FGCOLOR || '#FFFFFF');
  sTextPadding=(_ws.TEXTPADDING || 0);
  sBorder=(_ws.BORDER || 2);
  sScroll=(_ws.SCROLL || '');
  sPrintText=(_ws.PRINTTEXT || '');
  if(sPrintText=='icon'){
    sPrintText=gjsGappPrintWindowImg;
  }
  sRef=(_ws.REF || '');
  if(sRef=='CENTER'){//d
    sWinPos='MIDX,0,MIDY,0';//d
  }else{//d
    sWinPos="REF,'"+sRef+"',REFC,'LL',REFP,'UL'";//d
  }//d
//alert('sWinPos:'+sWinPos);

  //var sWinSettings = "overlib(OLiframeContent(pHREF, iWidth, iHeight, 'ifajax', 1),"+
  var sWinSettings = "overlib(OLiframeContent(sPrintScript, iWidth, iHeight, 'ifajax',1),"+
  "WIDTH,iWidth, FGCOLOR,sFGColor,TEXTPADDING,sTextPadding,BORDER,sBorder,SCROLL,STICKY,CLOSECLICK,CLOSETEXT,gjsGappCloseWindowImg,"+
  "DRAGGABLE,CAPTIONPADDING,4,CAPTION,sCaption,"+
  "_ws.PRINT,PRINTTEXT,sPrintText,PRINTJOB,'myPrintJob(\""+pHREF+"\",\""+sCaption+"\")',"+sWinPos+");";
//alert('jSettings:'+sWinSettings);
  
  eval(sWinSettings);
  
  //To inject HTML code into the iFrame
  if(sPrintScript==''){
    var ifrm = document.getElementById('ifajax');
    ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
    ifrm.document.open();
    ifrm.document.write(unescape(pHREF));
    ifrm.document.close();
  }
  
  return
//alert('showDialogIFRAME:');
  
}
/*
* NOTE I struggle to get a scroll bar when HTML content is larger than width and height
*
* pHREF is an href to a file you want to show, or a string
* jsonWinSettings eg. $_windowArray="{\'BORDER\':10,\'PRINTTEXT\':\'icon\'};";//settings for the popup window
*/
function showDialogHTML(pHREF,pCaption,pWidth,pHeight,jsonWinSettings){
  //jsonWinSettings = (jsonWinSettings || '{};');
  jsonWinSettings = (jsonWinSettings || '{}');
  eval('var _ws='+jsonWinSettings);
//alert('showDialog:'+pHREF);
  sCaption = (pCaption || ' ');
  iWidth = (pWidth || 450);
  iHeight = (pHeight || 350);
  sFGColor=(_ws.FGCOLOR || '#FFFFFF');
  sTextPadding=(_ws.TEXTPADDING || 0);
  sBorder=(_ws.BORDER || 2);
  sScroll=(_ws.SCROLL || '');
  sMODAL=(_ws.MODAL+',' || '');
  sPrintText=(_ws.PRINTTEXT || '');
  if(sPrintText=='icon'){
    sPrintText=gjsGappPrintWindowImg;
  }
  sRef=(_ws.REF || '');

  if(sRef=='CENTER'){
    sWinPos='MIDX,0,MIDY,0';
  }else{
    sWinPos="REF,'"+sRef+"',REFC,'LL',REFP,'UL'";
  }
  
$('<div></div>')
			//.load(pHREF, function(response, status, xhr){
      //  if (status == "error") {
      //    var msg = "Sorry but there was an error: ";
      //    $(this).html(msg + xhr.status + " " + xhr.statusText);
      //  }
      //})
			//.html(pHREF)
			.html('<iframe id="modalIframeId" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" />')
			.dialog({
				//autoOpen: false,
				title: sCaption,//$link.attr('title'),
				width: iWidth,
				height: iHeight,
				modal: true,
			}); 
			
			$("#modalIframeId").attr("src",pHREF); 
  return
}
function showDialogHTML_Old(pHREF,pCaption,pWidth,pHeight,jsonWinSettings){
  //jsonWinSettings = (jsonWinSettings || '{};');
  jsonWinSettings = (jsonWinSettings || '{}');
  eval('var _ws='+jsonWinSettings);
//alert('showDialog:'+pHREF);
  sCaption = (pCaption || ' ');
  iWidth = (pWidth || 450);
  iHeight = (pHeight || 350);
  sFGColor=(_ws.FGCOLOR || '#FFFFFF');
  sTextPadding=(_ws.TEXTPADDING || 0);
  sBorder=(_ws.BORDER || 2);
  sScroll=(_ws.SCROLL || '');
  sMODAL=(_ws.MODAL+',' || '');
  sPrintText=(_ws.PRINTTEXT || '');
  if(sPrintText=='icon'){
    sPrintText=gjsGappPrintWindowImg;
  }
  sRef=(_ws.REF || '');

  if(sRef=='CENTER'){
    sWinPos='MIDX,0,MIDY,0';
  }else{
    sWinPos="REF,'"+sRef+"',REFC,'LL',REFP,'UL'";
  }
  var sStyle="";
  var sWinSettings = "overlib("+sStyle+"<div class=\"ovfl\" id=\"ovflo\">"+pHREF+"</div>,DECODE,"+sMODAL+"WIDTH,iWidth,HEIGHT,iHeight,FGCOLOR,sFGColor,"+
                     "TEXTPADDING,sTextPadding,BORDER,sBorder,STICKY,CLOSECLICK,CLOSETEXT,gjsGappCloseWindowImg,"+
                     "PRINT,PRINTTEXT,sPrintText,PRINTJOB,'myPrintJob(\""+pHREF+"\",\""+sCaption+"\")',"+
                     "DRAGGABLE,DRAGCAP,CAPTIONPADDING,4,CAPTION,sCaption,"+sWinPos+");";
  
//  var sStyle=encodeURI('<style>.ovfl{width:'+pWidth+'px; height:'+pHeight+'px; border:1px none #000000; padding:6px; overflow:auto;}</style>');
//  sStyle += encodeURI('<div class=\"ovfl\" id=\"ovflo\">');
//  sStyle += pHREF;
//  sStyle +=encodeURI('</div>');
//  var sWinSettings = "overlib("+sStyle+",DECODE,WIDTH,iWidth,HEIGHT,iHeight,FGCOLOR,sFGColor,"+
//                     "TEXTPADDING,sTextPadding,BORDER,sBorder,STICKY,CLOSECLICK,CLOSETEXT,gjsGappCloseWindowImg,"+
//                     "PRINT,PRINTTEXT,sPrintText,PRINTJOB,'myPrintJob(\""+pHREF+"\",\""+sCaption+"\")',"+
//                     "DRAGGABLE,DRAGCAP,CAPTIONPADDING,4,CAPTION,sCaption,"+sWinPos+");";
//alert(sWinSettings);
  eval(sWinSettings);
  return

//  var sStyle='<style type=\"text/css\">.ovfl{width:'+pWidth+'px; height:'+pHeight+'px; border:1px none #000000; padding:6px; overflow:auto;}</style>';
//  return overlib(sStyle+'<div class="ovfl" id="ovflo">'+unescape(pHREF)+'</div>',WIDTH,iWidth,HEIGHT,iHeight,FGCOLOR,sFGColor,
//  TEXTPADDING,sTextPadding,BORDER,sBorder,STICKY,CLOSECLICK,CLOSETEXT,gjsGappCloseWindowImg,
//  PRINT,PRINTTEXT,sPrintText,PRINTJOB,'myPrintJob("'+escape(pHREF)+'","'+sCaption+'")',
//  DRAGGABLE,CAPTIONPADDING,4,CAPTION,sCaption,MIDX,0,MIDY,0);//REF,sRef, REFC,'LL',REFP,'UL');

}

function showDialogAJAX(pHREF, pCaption, pWidth, pHeight){
//alert('showDialog:'+pHREF);
  sCaption = (pCaption || ' ');
  iWidth = (pWidth || 450);
  iHeight = (pHeight || 350);
//alert('showDialog Width:'+iWidth+' Height:'+iHeight);
  
  return OLgetAJAX(pHREF, function(){ajaxWinResponse(pCaption,iWidth,iHeight);});
         
//alert('showDialog:'+pHREF);
}

function ajaxWinResponse(pCaption, pWidth, pHeight){
	sCaption =(pCaption || ' ');
  var sStyle='<style type=\"text/css\">.ovfl{width:'+pWidth+'px; height:'+pHeight+'px; border:1px none #000000; padding:6px; overflow:auto;}</style>';

//alert(pHeight);
	return overlib(sStyle+'<div class="ovfl">'+OLresponseAJAX+'</div>',WIDTH,pWidth,HEIGHT,pHeight, STICKY, BELOW, RIGHT, CAPTION, sCaption, STATUS, sCaption, 
  	DRAGGABLE,DRAGCAP,CLOSETEXT, gjsGappCloseWindowImg ,CLOSECLICK, OFFSETY, 20, 
  	FGCOLOR, '#ffffff', BGCOLOR, '#000000', TEXTCOLOR, '#000000', CAPCOLOR, '#ffffff', 
  	CLOSECOLOR, '#ffffff',  
  	BORDER, '2');
}

/*
My idea is to add this functionality to gapptype_image
var printimg = '<img src="print_ol.gif" alt="Print" width="21" height="15" border="0">';
txt = TextStr[1] = '<img src="1.jpg" width="400" height="400">';
<img src="1_tn.jpg" width="100" height="100" title="Group 1" alt="Group 1" class="thumb" onClick="return picsOverlib(TextStr[1], CapStr[1]);">
*/    
function showDialogIMAGE(pHREF, pCaption, pWidth, pHeight){
//alert('showDialogIMAGE:');
  sCaption = (pCaption || ' ');
  iWidth = (pWidth || 800);
  iHeight = (pHeight || 600);
  var sStyle='<style type=\"text/css\">.ovfl{width:'+pWidth+'px; height:'+pHeight+'px; border:1px none #000000; padding:6px; overflow:auto;}</style>';
  
  return overlib(sStyle+'<div class="ovfl" id="ovflo">'+pHREF+'</div>',WIDTH,iWidth,HEIGHT,iHeight,MIDX,0,MIDY,0,
  HIDESELECTBOXES,HIDEBYIDNS4,FGCOLOR,'#FFFFFF',TEXTPADDING,0,BORDER,2,STICKY,CLOSECLICK,CLOSETEXT,gjsGappCloseWindowImg,
  PRINT,PRINTTEXT,gjsGappPrintWindowImg,DRAGGABLE,DRAGCAP,CAPTIONPADDING,4,CAPTION,sCaption);
  //REF,'$this->fieldnameform', REFC,'LL',REFP,'UL', STATUS,'Lookup');
}

/*
pMessage is HTML text
*/
function showMessage(pMessage, pCaption, pPos, pWidth, pHeight, pTimeout){
  sCaption = (pCaption || '');
  if(sCaption==''){sCloseText='';}else{sCloseText=gjsGappCloseWindowImg;}
  iWidth = (pWidth || 0);
  iHeight = (pHeight || 0);
  if(pPos==null){
    iMIDX=0;iMIDY=0;
  }else if(pPos=='TOPLEFT'){
    iMIDX=-10000;iMIDY=-10000;
  }else if(pPos=='TOPRIGHT'){
    iMIDX=10000;iMIDY=-10000;
  }else if(pPos=='BOTTOMLEFT'){
    iMIDX=-10000;iMIDY=10000;
  }else if(pPos=='BOTTOMRIGHT'){
    iMIDX=10000;iMIDY=10000;
  }
  iTimeout = (pTimeout || 0);
  
  return overlib(pMessage,CAPTION,sCaption,
//   STICKY,CLOSECLICK, CLOSETEXT,sCloseText,WIDTH,iWidth,HEIGHT,iHeight, MIDX,0, MIDY,0, WRAP, SCROLL, TIMEOUT, 2000);  
   STICKY,CLOSECLICK, CLOSETEXT,sCloseText,WIDTH,iWidth,HEIGHT,iHeight, MIDX,iMIDX, MIDY,iMIDY, WRAP, SCROLL, TIMEOUT, iTimeout);  
}

function showToolTip(pToolTip){
  return overlib(pToolTip, NOFOLLOW, DELAY, 500, TIMEOUT, 5500, WRAP, HAUTO, FGCOLOR, '#ffffee', AUTOSTATUS);
}
function showToolTip2(pToolTip){
  return overlib2(pToolTip, NOFOLLOW, DELAY, 500, TIMEOUT, 5500, WRAP, HAUTO, FGCOLOR, '#ffffee', AUTOSTATUS);
}
function hideWindow(){
  return nd();
}

/*
* External function invoked by print link or button with the PRINTJOB property for
* the showDialogIFRAME() and showDialogHTML()
*
*/
function myPrintJob(TheText, TheCaption) {
//alert('myPrintJob:'+TheText);

  // If no input, do nothing
  if (!TheText) return;

  if(isValidURL(TheText)){//defined in gappJGappFunc.js
    var sPrintScript = '<script type="text/javascript">self.location.href=\''+TheText+'\';PrintAndClose();<\/script>'
  }else{
    var sPrintScript = '<script type="text/javascript">self.document.write(unescape(\''+TheText+'\'));PrintAndClose();<\/script>'
  }

 // Create the HTML with an onload printing function for the temporary printing window
 var childWindowText =
   '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" '
  +'"http://www.w3.org/TR/html4/loose.dtd">'
  +'<html><head><title>'+(TheCaption?TheCaption:'OLprint job')+'<\/title>'
  +'<script type="text/javascript">'
  +'function PrintAndClose() {'
  +' if (window.print) {'
  +'  var t=window.open("","","resizable=0,width=10,height=10,status=0,location=0,toolbar=0,menubar=0,scrolling=0,screenX=50,left=50,screenY=20,top=20");'
  +'  t.opener.print();'
  +'  t.close();'
  +'  setTimeout("self.close()",10);'
  +' } else {'
  +'  alert(\'Use Control-P to print, then close this window.\');'
  +' return false;'
  +' }'
  +'}'
  +'<\/script>'
  +'<\/head>'
  +'<body>'
  +sPrintScript
  +'<\/body><\/html>';
 
  // Create a temporary window,
  // pass it the markup which will print itself,
  // and then close that window. 
  PrJob = window.open('','printjob',
     'resizable=0,width=690,height=480,status=0,location=0,toolbar=0,'
    +'menubar=0,scrolling=0,screenX=50,left=50,screenY=20,top=20');
    PrJob.document.write(childWindowText);
    PrJob.document.close();

 return;
}
