// Misc UI management Functions
// J.Pearson

function getDaysUntil(targetDateString) {
msPerDay = 24 * 60 * 60 * 1000 ;
TargetDay = new Date(targetDateString);
timeLeft = (TargetDay.getTime() - (new Date()).getTime());
return Math.floor(timeLeft/msPerDay)
}

// Pay Pal Functions.
// problem with papyal supplied scripts is the ASP.Net multi-form issue.
// These functions Insert HTML Form with auto submit into a hidden iframe. papypal will bust its way out and open its own window.
// Usage : place the calls to these scripts in the href="javascript:PayPalAddToCart(.....)"
//NOTE : WE SHOULD NOW REPLACE THESE WITH URL CALLS
function PayPalAddToCart2(paramsString) {
// accept a string with param1=A;param2=B  style parameters

	MakeHiddenIframe('Iframe1',true)
	var paramArray = paramsString.split(';')
	var strHTML // = '<html><head>' + '</head><body></body>'
	var strHTML = '<html><head></head>'
	strHTML += '<body>'
	strHTML += '<form id="FormPP" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> ' // write a paypal form
	strHTML += '<input type="hidden" name="add" value="1">'
	strHTML += '<input type="hidden" name="cmd" value="_cart">'
    // now add all the supplied param=value pairs
    for(i=0; i<paramArray.length; i++) { 
        strHTML += '<input type="hidden" name="' + paramArray[i].split('=')[0] + '" value="' + paramArray[i].split('=')[1] + '">'
        }
	strHTML += '</form>'
	strHTML += '<script> document.forms[0].submit(); self.close(); ' + '</scr' + 'ipt>'  // submit form and close the popup win (have to break up the script tag as it terminates the doc(!)
	strHTML += '</body></html>' 
	//alert(strHTML)
	WriteHTMLToIframe('Iframe1',strHTML)
}

function PayPalAddToCart(business,item_name,item_number,amount,no_shipping,currency_code) {

	MakeHiddenIframe('Iframe1',true)
	
	var strHTML = '<html><head>' + '</head><body></body>'
	strHTML += '<html><head></head>'
	strHTML += '<body>'
	strHTML += '<form id="FormPP" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> ' // write a paypal form
	strHTML += '<input type="hidden" name="add" value="1">'
	strHTML += '<input type="hidden" name="cmd" value="_cart">'
	strHTML += '<input type="hidden" name="business" value="' + business + '">'
	strHTML += '<input type="hidden" name="item_name" value="' + item_name + '">'
	strHTML += '<input type="hidden" name="item_number" value="' + item_number + '">'
	strHTML += '<input type="hidden" name="amount" value="' + amount + '">'
	strHTML += '<input type="hidden" name="no_shipping" value="' + no_shipping + '">'
	strHTML += '<input type="hidden" name="no_note" value="1">'
	strHTML += '<input type="hidden" name="currency_code" value="' + currency_code + '">'
	strHTML += '<input type="hidden" name="lc"" value="AU">'
    strHTML += '<input type="hidden" name="bn" value="PP-ShopCartBF">'
	strHTML += '</form>'
	strHTML += '<script> document.forms[0].submit(); self.close(); ' + '</scr' + 'ipt>'  // submit form and close the popup win (have to break up the script tag as it terminates the doc(!)
	strHTML += '</body></html>'  
	WriteHTMLToIframe('Iframe1',strHTML)
}


function PayPalBuyNow(business,item_name,item_number,amount,no_shipping,currency_code) {

	MakeHiddenIframe('Iframe1',true)
	
	var strHTML = '<html><head>' + '</head><body></body>'
	strHTML += '<form id="FormPP" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> '
	strHTML += '<input type="hidden" name="add" value="1">'
	strHTML += '<input type="hidden" name="cmd" value="_xclick">'
	strHTML += '<input type="hidden" name="business" value="' + business + '">'
	strHTML += '<input type="hidden" name="item_name" value="' + item_name + '">'
	strHTML += '<input type="hidden" name="item_number" value="' + item_number + '">'
	strHTML += '<input type="hidden" name="amount" value="' + amount + '">'
	strHTML += '<input type="hidden" name="no_shipping" value="' + no_shipping + '">' //no_shipping controls whether user can set shipping details
	strHTML += '<input type="hidden" name="no_note" value="1">'
	strHTML += '<input type="hidden" name="currency_code" value="' + currency_code + '">'
	strHTML += '<input type="hidden" name="lc"" value="AU">'
    strHTML += '<input type="hidden" name="bn" value="PP-BuyNowBF">'
	strHTML += '</form>'
	strHTML += '<script> document.forms[0].submit(); self.close(); ' + '</scr' + 'ipt>'  // submit form and close the popup win (have to break up the script tag as it terminates the doc(!)
	strHTML += '</body></html>'  
	WriteHTMLToIframe('Iframe1',strHTML)
}

function PayPalViewCart(business) {
	MakeHiddenIframe('Iframe1',true)
	var strHTML = '<html><head>' + '</head><body></body>'	
	strHTML += '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> '
	strHTML += '<input type="hidden" name="add" value="1">'
	strHTML += '<input type="hidden" name="cmd" value="_cart">'
	strHTML += '<input type="hidden" name="business" value="' + business + '">'
	strHTML += '<input type="hidden" name="display" value="1">'
	strHTML += '</form>'
	strHTML += '<script> document.forms[0].submit(); self.close(); ' + '</scr' + 'ipt>'  // submit form and close the popup win (have to break up the script tag as it terminates the doc(!)
	strHTML += '</body></html>'  
	WriteHTMLToIframe('Iframe1',strHTML)
}


// IFRAME MANIPULATION ROUTINES

function WriteHTMLToIframe(pFrameID,pHTML) { // Write html content to the doc inseide an iframe element.x-browser need to include the <html></html> tags
	FrameDocument = GetIframeDocument(pFrameID)
	FrameDocument.open(); 
	FrameDocument.write(pHTML);
	FrameDocument.close();
}

function GetIframeDocument(pFrameID) { //  getting a handle to the document within an iframe.x-browser
	oFrame = document.frames ? document.frames[pFrameID] : document.getElementById(pFrameID); // Get the Iframe window object (rather than the html element)
	FrameDocument = oFrame.document || oFrame.contentWindow.document;
	return FrameDocument;
}

function MakeHiddenIframe(pFrameID) {
var iframe = document.createElement("iframe");
	iframe.setAttribute("id", pFrameID);
	iframe.setAttribute("scrolling", "no");
	iframe.setAttribute("frameBorder", "0");
	iframe.style.width = 0+"px";
	iframe.style.height = 0 +"px";
	//iframe.style.display = 'none';
	document.body.appendChild(iframe);
}


//  NEWLY ADDED TO BE TESTED, Needs X-Browser rationalisation

function UItest(){
alert('UItest')
}

function popupWin(URL,Width, Height) {
    version=parseInt(navigator.appVersion);
    if (navigator.appVersion.indexOf('5.')>-1)
    {version=5};
    if (navigator.appVersion.indexOf('6.')>-1)
    {version=6};
    if (((navigator.appName=='Microsoft Internet Explorer') && (version > 4)) || ((navigator.userAgent.indexOf('Netscape') == -1) && (navigator.userAgent.indexOf('Opera') == -1)))
      {
        NewWinWidth=Width;
        NewWinHeight=Height;
        NewWinTop = (screen.availHeight / 2) - (NewWinHeight / 2);
        NewWinLeft = (screen.availWidth / 2) - (NewWinWidth / 2);
      }
    else
      {
        NewWinWidth=Width;
        NewWinHeight=Height;
        NewWinTop = 1;
        NewWinLeft = 1;
      }
   
    window.open(URL,null,"toolbar=no,menubar=no,resizable=yes,scrollbars=1,status=1,height=" + NewWinHeight + ",width=" + NewWinWidth + ",toolbar=0,left=" + NewWinLeft + ",top=" + NewWinTop);
  
}




function getStyleElementObject(objID) {
// Cross-browser Get element by ID, for the style object of the passed element ID
var layerObject
	if (ns){
		if (browserVersion>=5) {
			layerObject = document.getElementById(objID).style;
		} else { 
			layerObject = eval("document."+objID);
		}
	} else {
	layerObject = eval(objID + ".style"); // non ie
	}
return layerObject;
}

// dynamically switching the background image
	function changeBack(divId) {
		if (intBack < 3) {
			intBack = intBack + 1;
		} else {
			intBack = 1;
		}
		if (ns){
			if (browserVersion>=5) {
				layerObject = document.getElementById(divId).style;
			} else { 
				layerObject = eval("document."+divId);
			}
		} else {
			layerObject = eval(divId + ".style");
		}
		layerObject.backgroundImage = 'url(homebkgd'+intBack+'.jpg)';
	}
	
function setObjectColour(objID,strColour) {
// Set the foreground colour of an object (A, not TD)
var layerObject = getStyleElementObject(objID)
layerObject.color = strColour
}

//---------------------------------------------------------------
// PNG  Management Functions
//  IE5.5+ on win32, can display PNGs properly only with AlphaImageLoader;  
// by inspection from Michael Lovitt

function BrowserNeedsAlphaImageLoader() {
// need to determine whether we need to use AlphaImageLoader filters for properly displaying PNG overlay
	return ((browser.isIE55 || browser.isIE6up) && browser.isWin32)
}

function displayPNGImage(strId, imgRef, intWidth, intHeight, strClass, strAlt) {	
//Outputs the image as a div with the AlphaImageLoader, or with a standard image tag. 
// TODO - pass in an onclick event string which is then added to the element - in order to implement hyperlinking.

	if ((browser.isIE55 || browser.isIE6up) && browser.isWin32) {
		document.write('<div style="height:'+intHeight+'px;width:'+intWidth+'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imgRef+'\', sizingMethod=\'scale\')" id="'+strId+'" class="'+strClass+'"></div>');
	} else  {
		document.write('<img src="'+imgRef+'" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />');
	} 
}

function setPNGBackgrounds() {
// Set the backgrounds of several elements to a given PNG image. Argumenet should be supplied as 'elementID1','Imagesource1','elementID2','Imagesource2', etc
 var i,j=0,a=setPNGBackgrounds.arguments;  

 for(i=0;i<(a.length-1);i+=2){
		//alert(a[i] + '  ' +a[i+1]);
		setPNGBackground(a[i],a[i+1]);
	}
}

function setPNGBackground(objID,imgRef) {  
// Set the background of a element(div, table cell) to be a PNG image, allowing for ie inability to display without special filter	
// Must set the height and width of the div in a stylesheet/inline style, else will not display
// Does not seem to handle spaces in folder/file names
	var layerObject = getStyleElementObject(objID) // Get a handle to div tag object
	// Set the background
	if ((browser.isIE55 || browser.isIE6up) && browser.isWin32) {
		layerObject.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgRef+"', sizingMethod='scale')";
	} else  {
		layerObject.backgroundImage = 'url('+imgRef+') ';
	} 
}


var ns = (document.all)?false:true;   // actually ns & mozilla
var browserVersion = parseFloat(navigator.appVersion );

