// This is a general popup function for states that require a special announcement at the time of ordering or previewing reports

var	pv_popupPath = "/as/";				// Preset for PV site - may be different for affiliate sites
var pv_VICPopupShown = false;			// Already shown in this page? We only want to show it once
var pv_SAPopupShown = false;
var pv_QLDPopupShown = false;
var pv_WAPopupShown = false;
var pv_TANDCPopupShown = false;
var YourAffiliateID = 0;					// YourAffiliateID term needs to be replaced with a real ID number by affiliate staff
var pv_form = null;								// Initialize form reference. Before referencing the form pv_findForm(fName) function must be called.

function pv_findForm(fName) {
	var oForm;
	if (document.all)
		oForm = document.all[fName];
	else
		for (var i=0; i<document.forms.length; i++)
			if (document.forms[i].name == fName) {
				oForm = document.forms[i];
				break;
			}
	if (oForm && oForm.tagName == "FORM")
		pv_form = oForm;
}

function chkPopup(btName) {
	var rName;
	var checkStates = "";
	//alert(pv_form);
	//alert(pv_form.pv_btPressed);
	//alert(pv_reportType);
	if(!pv_form ||(!pv_form.pv_btPressed && pv_reportType != 4))
	{
		if (btName == "pv_reportAddToCart")
			alert("Please wait until the page is fully loaded and click on 'Add To Cart' again.");
		else
			alert("Please wait until the page is fully loaded and click on 'Preview' again.");
		return;
	}
	
	// ** removal of popup T&C
	if (pv_form.pv_btPressed) 
		pv_form.pv_btPressed.value = btName;
	pv_form.submit();
	return;
	
	switch (pv_reportType) {
		case 2:
			rName = "PSH";
			checkStates = "VIC";
			pv_form.pv_btPressed.value = btName;
			if (btName == "pv_reportAddToCart") checkStates += "+SA+QLD+WA";
			break;
		case 3:
			rName = "IPR";
			checkStates = "VIC";
			pv_form.pv_btPressed.value = btName;
			if (btName == "pv_reportAddToCart") checkStates += "+SA+QLD+WA";
			break;
		case 4:
			rName = "IR";
			checkStates = "SA+QLD+WA";
			break;
		default:
			return;
	}
	pv_StatePopup(pv_GetState(),checkStates,rName,btName);
}

function pv_StatePopup(selState, checkStates, rType, btName){
//	selState = what state is selected in the form; 3 char code (NSW,VIC,SA,...)
//	checkStates = which states are relevant with this action (button); state char codes separated by +
//	rType = which report (PSH, IPR)
//	btName = goahead button name (pv_reportAddToCart, pv_reportFreeSample)

	var wHeight, wParams;	// popup window parameters
	var wUrl = pv_popupPath+"pv-fPopup.html?"
	
	with (pv_form) {
		if (checkStates.indexOf(selState) > -1) { 	// state requires popup?
			switch (selState) {
				case "VIC":
					if (!pv_VICPopupShown) {  
						wHeight = 320+25*(rType == "IPR");	// make IPR popup slightly larger
						wParams = "top=210,height="+wHeight+",left=240,width=500,scrollbars=1";
						wUrl += "title=About+Victorian+Reports";
						wUrl += "&btCont=";
						switch (btName) {
							case "reportAddToCart": 
							case "pv_reportAddToCart": 
								wUrl += "Add+to+cart";
								break;
							case "reportFreeSample":
							case "pv_reportFreeSample": 
								wUrl += "Generate+preview";
								break;
						}
						wUrl += "&btCancel=Close+this+window";
						wUrl += "&popupQS=content%3D"+rType+"%2Bpage%2523VIC_popup";
						window.open(wUrl,"popup",wParams);
						VICPopupShown = true;
						return;
					}
				break;
				case "SA":
					wParams = "top=110,height=460,left=240,width=600,scrollbars=1";
					wUrl += "title=Terms+%26+Conditions+for+South+Australian+Reports";
					wUrl += "&btCont=I+agree&btCancel=I+disagree";
					wUrl += "&popupQS=content%3DCommon%2523SA_disclaimer";
					window.open(wUrl,"popup",wParams);
					pv_SAPopupShown = true;
					return;
				break;
				case "QLD":
					wParams = "top=110,height=460,left=240,width=600,scrollbars=1";
					wUrl += "title=Terms+%26+Conditions+for+Queensland+Reports";
					wUrl += "&btCont=I+agree&btCancel=I+disagree";
					wUrl += "&popupQS=content%3DCommon%2523QLD_disclaimer";
					window.open(wUrl,"popup",wParams);
					pv_QLDPopupShown = true;
					return;
				break;
				case "WA":
					wParams = "top=110,height=460,left=240,width=600,scrollbars=1";
					wUrl += "title=Terms+%26+Conditions+for+Western+Australian+Reports";
					wUrl += "&btCont=I+agree&btCancel=I+disagree";
					wUrl += "&popupQS=content%3DCommon%2523WA_disclaimer";
					window.open(wUrl,"popup",wParams);
					pv_WAPopupShown = true;
					return;
				break;
				case "TANDC":
					wParams = "top=110,height=460,left=240,width=600,scrollbars=1";
					Window.open("TANDC_popup.php?nForm=0&smsReg=no","popup",wParams);
					pv_TANDCPopupShown = true;
					return;
				case "TANDCSMS":
					wParams = "top=110,height=460,left=240,width=600,scrollbars=1";
					Window.open("TANDC_popup.php?nForm=0&smsReg=yes","popup",wParams);
					pv_TANDCPopupShown = true;
					return;
				break;
			} 
		}
		
		submit();	// if state doesn't require popups then submit the form
	} 
}

function pv_GetState(){
// gets the state from the form's select field; if unselected then tries to guess it from the postcode

	var selState;
	
	with (pv_form) {
		if (pv_reportState.tagName == "INPUT") selState = pv_reportState.value;
		else selState = pv_reportState.options[pv_reportState.selectedIndex].text;
		if (selState == "Select..." && typeof pv_reportPostcode == "object")
			switch (pv_reportPostcode.value.charAt(0)) {
				case "3":
					selState = "VIC"
					break;
				case "5":
					selState = "SA"
					break;
				case "4":
					selState = "QLD"
					break;
				case "6":
					selState = "WA"
					break;
			}
		return selState;
	}
}
