aDlgs = new Array;

function alreadyPopped()
{
    return (this.objWindow && this.objWindow.open && !this.objWindow.closed)
}

function dtpCheck(form)
{
    if(form.dtp_box.value == "")
	    form.dtp_box.value = "no_protection";
	else
	    form.dtp_box.value = "";
}

function dtpBrand(form)
{
    if(form.dtp_box_brand.value == "protection")
	    form.dtp_box_brand.value = "no_protection";
	else
	    form.dtp_box_brand.value = "protection";
}

function dtpOrg(form)
{
    if(form.dtp_box_org.value == "protection")
	    form.dtp_box_org.value = "no_protection";
	else
	    form.dtp_box_org.value = "protection";
}

function dtpCheck_notif(form)
{
    if(form.dtp_box_notif.value == "")
	    form.dtp_box_notif.value = "no_protection";
	else
	    form.dtp_box_notif.value = "";
}

function dtpCheck_email(form)
{
    if(form.emailprotection.value == "true")
	    form.emailprotection.value = "false";
	else
	    form.emailprotection.value = "true";
}

function populateBankAddress(form)
{
    if(!form.dd_sort.value)
	    {
	    alert("Please complete the 'Branch Sort Code' field before looking up branch information");
		form.dd_sort.focus();
		return;
		}
		
	if(form.dd_bnk.value || form.dd_ln1.value || form.dd_ln2.value || form.dd_ln3.value || form.dd_pcd.value)
	    {
        if(!confirm("Bank branch information you have so far supplied will be overwritten.  Do you wish to continue?")) 
		    return;
		}
   

    window.location = ("checkout.cfm?action=ddaddpop&dd_accn=" + form.dd_accn.value + "&dd_sort=" + form.dd_sort.value + "&dd_acc=" + form.dd_acc.value + "&this_step=" + form.this_step.value + "&step_number=" + form.step_number.value);
}

function popUp()
{
    if(!this.Popped())
        {
        this.objWindow = window.open('address_lookup.cfm', this.sWindowName,
                        'toolbar=no,location=no,directories=no,status=yes,'+
                        'menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,'+
                        'width=600,height=500')
        window.onUnload = eventPopDownAll
        }

    this.objWindow.focus()
}

function popUpWindow()
{
    if(!this.Popped())
        {
			this.objWindow = window.open('address_lookup.cfm', this.sWindowName,
                        'toolbar=no,location=no,directories=no,status=yes,'+
                        'menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,'+
                        'width=600,height=500')
			window.onUnload = eventPopDownAll
        }

    this.objWindow.focus()
}

function popupReminder()
{
    reminder = window.open('reminder.cfm', 'reminder',
                        'toolbar=no,location=no,directories=no,status=yes,'+
                        'menubar=no,resizable=yes,copyhistory=no,scrollbars=no,'+
                        'width=500,height=300,left=400,top=200')
    reminder.focus();
}


function PopUpDlg (sFormName, aFields)
{
    this.sFormName = sFormName
    this.aFields = aFields
    this.PopUp = popUpWindow;
    this.Popped = alreadyPopped
}

function transferAddress(action)
{
    var fieldCount = 0;

	if(aDlgs["addressLookup"].objWindow.document.forms["formPopup"].elements["add_ln1"])
	    for (var field in aDlgs["addressLookup"].aFields)
	        {
			if(document.forms[aDlgs["addressLookup"].sFormName].elements[aDlgs["addressLookup"].aFields[field]])
				document.forms[aDlgs["addressLookup"].sFormName].elements[aDlgs["addressLookup"].aFields[field]].value = aDlgs["addressLookup"].objWindow.document.forms["formPopup"].elements[aDlgs["addressLookup"].aFields[field]].value;
	        fieldCount++;
	        }

	aDlgs["addressLookup"].objWindow.close();
}

function eventPopDownAll()
{
    for(var ob in aDlgs)
        if(aDlgs[ob].Popped())
            aDlgs[ob].objWindow.close()
}

function PopUpAddress(sName, objPopUp)
{
    if(!aDlgs[sName])
        {
        objPopUp.sWindowName = sName
        aDlgs[sName] = objPopUp
        }

    if(!aDlgs[sName].Popped())
        aDlgs[sName].PopUp()
	
    return (false);
}

function popAddressLookup()
{
	PopUpAddress("addressLookup", new PopUpDlg('theForm',new Array('add_cpy', 'add_ln1', 'add_ln2', 'add_ln3', 'add_ln4', 'add_ln5', 'add_ln6', 'add_pcd')))
}
	
function popDown(form)
{
    if(form.page_action.value != "single_address")
	    return true;
	
    if(opener)
	    {
        opener.transferAddress();
		return false;
		}
		
	return false;
}	

// Function to open popup window
function open_window(winWidth, winHeight, url) 
{
    var toppos;
	var leftpos;

	// Ensure desired window dimensions do not exceed available screen dimensions
	if (winHeight > screen.availHeight)
	    winHeight = screen.availHeight;
	if (winWidth > screen.availWidth)
	    winWidth = screen.availWidth;
	    
  	var toppos=(screen.availHeight/2)-(winHeight/2);
  	var leftpos=(screen.availWidth/2)-(winWidth/2);
	
  	windowprops = "height=" + winHeight + ",width=" + winWidth + ",left="+leftpos+ 
	",top="+toppos+",location=no,"
  	+ "scrollbars=yes,menubars=no,toolbars=no,resizable=no,statusbar=no,locationbar=no"
  	+ "personalbar=no,modal=yes";
  	newWind = window.open(url, "open",windowprops);
}

// Function checks to see if any upsell offers are available for the purchased item and if so,
// pops up a window containing the upsell offers
function checkForSpecial(form, type)
{
    var special_match = /\/SPECIAL$/;
	var result;
	var cat_code;
	var offer_to_upsell;
	
    // Check to see whether special offer exists
	if (type == "SUB")
	    {
		for (var i = 0; i < form.sub_offer.length; i++)
		    if (form.sub_offer[i].checked)
			    {
			    cat_code = form.sub_offer[i].value;
				result = cat_code.match(special_match);
				// Remove "/SPECIAL" string from end of cat code
				form.sub_offer[i].value = form.sub_offer[i].value.replace(special_match, "");
				offer_to_upsell = form.sub_offer[i].value;
				break;
				}			
		
		// Special offer found
		if (result != null)
			// Open popup to display special offer
		    special = open_window(800, 480, 'special_sub.cfm?offer_to_upsell=' + offer_to_upsell);
		}    // END OF type = SUB
	
    // Check to see whether special offer exists
	else if (type == "DSUB")
	    {
		for (var i = 0; i < form.dsub_offer.length; i++)
		    if (form.dsub_offer[i].checked)
			    {
			    cat_code = form.dsub_offer[i].value;
				result = cat_code.match(special_match);
				// Remove "/SPECIAL" string from end of cat code
				form.dsub_offer[i].value = form.dsub_offer[i].value.replace(special_match, "");
				offer_to_upsell = form.dsub_offer[i].value;
				break;
				}			
				
		// Special offer found
		if (result != null)
			// Open popup to display special offer
		    special = open_window(800, 480, 'special_giftsub.cfm?offer_to_upsell=' + offer_to_upsell);
		}    // END OF type = DSUB
	
	return true;
}    // END OF checkForSpecial

function popupUpsell(code, type)
{
    var url;			// URL of popup window
	
    // Special offer URL dependent on item type
	if (type == "SUB")
	    url = "special_sub.cfm";
	else if (type == "DSUB")
	    url = "special_giftsub.cfm";

	// Open popup to display special offer
    special = open_window(800, 480, url + '?offer_to_upsell=' + code);
}   // END OF popupUpsell
