///////////////////////////////////////////////////////////////////////////////
// File: 	IFrameModal.js
// Purpose:	
//
///////////////////////////////////////////////////////////////////////////////
// Date     Name    Description of Change
// ----------------------------------------------------------------------------
// 06/21/11 NETK    Created.
// 10/17/11 NETK    To return to a specific view from another website, we 
//                  need to simulate a user interaction with the forms and wait
//                  for the forms to be ready before taking steps to return them
//                  to an expected view.  This code used to be in 
//                  DocumentPurchaseHelper.HandleReturn_OPC().  But it would
//                  cause an exception. So moved that code to this file and
//                  called it returnFromPaymentVendor_ShowPurchasedDocuments.
//                  DocumentPurchaseHelper.HandleReturn_OPC calls it.
//                  ZT# 4620 (CLB wrote this comment for NETK based on email
//                  from NETK).
// 11/14/11	NETK	Added returnFromPaymentVendor_ShowParcelTax to be used when
//					returning from payment vendors.
// 11/15/11	NETK	Added ctl00_cphMainApp_SearchDetailsParcel_LinkButtonDocuments
//					to the list of checks for when returning from document purchases.
///////////////////////////////////////////////////////////////////////////////
//                  *** Proprietary Computer Software ***
// No part of the material contained herein may be reproduced in any form or by
// any means, electronic or mechanical, including photocopying, without 
// permission in writing.
//                  (c) GCS Software, Inc. 2011
///////////////////////////////////////////////////////////////////////////////

var IFrameModal = new Object();

IFrameModal.Element = null;

IFrameModal.Init = function() {
    if (!document.getElementById('IFrameModal_Wrapper')) {
        IFrameModal.Element = document.createElement('div');
        IFrameModal.Element.id = 'IFrameModal_Wrapper';
        IFrameModal.Element.innerHTML = '<center><div><table>' +
            '<tr><td id="IFrameModal_WrapperHead">Make a Payment</td></tr>' +
            '<tr><td><iframe id="IFrameModal_IFrame"></iframe></td></tr>' +
            '<tr><td id="IFrameModal_WrapperFoot"></td></tr>' +
            '</table></div></center>';
        document.body.appendChild(IFrameModal.Element);
        IFrameModal.Resize();
        //IFrameModal.Show('about:blank', function() { alert(''); });
    }
    $(document).scroll(function() {
        $(IFrameModal.Element).css('top', $(document).scrollTop());
		// reposition the dialog elements
		$('#IFrameModal_Dialog').height($(document).height());
		$('#IFrameModal_DialogCenter').css('top', (($(window).height()-$('#IFrameModal_DialogCenter').height())/2) + $(document).scrollTop() );
		$('#IFrameModal_DialogCenter').css('left', ($(window).width()-$('#IFrameModal_DialogCenter').width())/2 );
		//
    })
    $(window).resize(function() {
        IFrameModal.Resize();
    });
};

IFrameModal.Resize = function() {
    $(IFrameModal.Element).height($(document).height() - $(document).scrollTop());
    $('#IFrameModal_IFrame').height($(window).height()-120);
	// reposition the dialog elements
	$('#IFrameModal_Dialog').height($(document).height());
	$('#IFrameModal_DialogCenter').css('top', (($(window).height()-$('#IFrameModal_DialogCenter').height())/2) + $(document).scrollTop() );
	$('#IFrameModal_DialogCenter').css('left', ($(window).width()-$('#IFrameModal_DialogCenter').width())/2 );
	//
};

IFrameModal.Show = function(srcUrl, cancelCallback) {
    $('#IFrameModal_WrapperFoot').html('<div id="IFrameModal_DialogButtons" class="IFrameModal_DialogButtons"><input class="submitBtn" type="button" id="IFrameModal_WrapperFootCancelButton" value="Cancel" /></div>');
    $('#IFrameModal_WrapperFootCancelButton').click(function() { IFrameModal.Hide(); cancelCallback(); });
    document.getElementById('IFrameModal_IFrame').src = srcUrl;
    $(IFrameModal.Element).show();
}

IFrameModal.Hide = function() {
    $('#IFrameModal_WrapperFoot').html('&nbsp;');
    document.getElementById('IFrameModal_IFrame').src = 'about:blank';
    $(IFrameModal.Element).hide();
}



IFrameModal.Dialog = function(dTitle,dMessage,dButtons){
	if(document.getElementById('IFrameModal_Dialog')==null){
		var d = document.createElement('div');
		d.id = 'IFrameModal_Dialog';
		d.innerHTML = '<div id="IFrameModal_DialogCenter">'+
			'<div id="IFrameModal_DialogTitle"></div>'+
			'<div id="IFrameModal_DialogMessage"></div>'+
			'<div id="IFrameModal_DialogButtons" class="IFrameModal_DialogButtons"></div>'+
			'</div>';
		document.body.appendChild(d);
	}
	var iHTML = '';
	for(var i in dButtons){
		if(dButtons[i]!=''){
			iHTML += '<input type="button" ';
			if(dButtons[i]!=''){
				iHTML += 'class="submitBtn" onclick="javascript:'+ 
					dButtons[i] +' '+
					'document.getElementById(\'IFrameModal_Dialog\').style.display = \'none\';'+
					'" ';
			}
			iHTML += ' value="'+ i +'" />';
		}
		else{
			iHTML += ' &nbsp; &nbsp; &nbsp; ';
		}
		
	}
	document.getElementById('IFrameModal_DialogTitle').innerHTML = dTitle;
	document.getElementById('IFrameModal_DialogMessage').innerHTML = dMessage;
	document.getElementById('IFrameModal_DialogButtons').innerHTML = iHTML;
	document.getElementById('IFrameModal_Dialog').style.display = 'block';
	
	$('#IFrameModal_Dialog').height($(document).height());
	$('#IFrameModal_DialogCenter').css('top', (($(window).height()-$('#IFrameModal_DialogCenter').height())/2) + $(document).scrollTop() );
	$('#IFrameModal_DialogCenter').css('left', ($(window).width()-$('#IFrameModal_DialogCenter').width())/2 );
	
	
	
}

function returnFromPaymentVendor_ShowParcelTax(){
	if(!document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_TaxesDetails1_ImageButtonModalExit')){
		//alert('cant find ctl00_cphMainApp_SearchDetailsParcel_TaxesDetails1_ImageButtonModalExit');
	}else{
		document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_TaxesDetails1_ImageButtonModalExit').click();
		//WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$cphMainApp$SearchDetailsParcel$TaxesDetails1$ImageButtonModalExit', '', true, '', '', false, false));
	}
}

function returnFromPaymentVendor_ShowPurchasedDocuments(sentConfirmationNumber,loopRunning,hideIFrameModal) {
	if(hideIFrameModal==true){IFrameModal.Hide();}
    var loopEnd = false;
	if (!loopRunning) {
	
		if (document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_LinkButtonViewPurchasedDocuments')) {
			WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMainApp$SearchDetailsParcel$LinkButtonViewPurchasedDocuments", "", true, "", "", false, true));
		}
		else if (document.getElementById('ctl00_cphMainApp_SearchDetailsDocument_LinkButtonViewPurchasedDocuments')) {
			WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMainApp$SearchDetailsDocument$LinkButtonViewPurchasedDocuments", "", true, "", "", false, true));
		}
		else if (document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_LinkButtonDocuments')){
			document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_LinkButtonDocuments').click(); 
		}
	}
    
    if(
        document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_PurchasedDocumentDetails_txtConfirmationNumber')
    ) {
    	$('#ctl00_cphMainApp_SearchDetailsParcel_PurchasedDocumentDetails_txtConfirmationNumber').val(sentConfirmationNumber);
    	if (document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_PurchasedDocumentDetails_ButtonGetPurchasedDocuments')) {
    		document.getElementById('ctl00_cphMainApp_SearchDetailsParcel_PurchasedDocumentDetails_ButtonGetPurchasedDocuments').click(); 
    	}	
    	loopEnd = true;
    }

    if (document.getElementById('ctl00_cphMainApp_SearchDetailsDocument_PurchasedDocumentDetails_txtConfirmationNumber')) {
    	document.getElementById('ctl00_cphMainApp_SearchDetailsDocument_PurchasedDocumentDetails_txtConfirmationNumber').value = sentConfirmationNumber;
    	if (document.getElementById('ctl00_cphMainApp_SearchDetailsDocument_PurchasedDocumentDetails_ButtonGetPurchasedDocuments')) {
    		document.getElementById('ctl00_cphMainApp_SearchDetailsDocument_PurchasedDocumentDetails_ButtonGetPurchasedDocuments').click(); 
    	}
    	loopEnd = true;
    }
	
    if (loopEnd) {
    	window.parent.IFrameModal.Hide();
    }
	
    else {
        // loop again to wait until page state is ready for the call
        setTimeout(function() {
    	    returnFromPaymentVendor_ShowPurchasedDocuments(sentConfirmationNumber, true);
        }, 1000);
    }
}


