/*-------------------------------------------------------------------
account.js
For CentreMapsLive Ver. 2
User account management scripts
Lovell Jonhs Ltd.
01/01/10
//------------------------------------------------------------------*/

//-------------------------------------------------------------------------USER ACCOUNT SCRIPTS
//*******************Code to submit the variables form to proceed to another page
function submittheform(theAction){
	if(((theAction == "userorders") || (theAction == "usersavedorders") || (theAction == "costmanagement") || (theAction == "licencemanagement") || (theAction == "userpreviews") || (theAction == "user") || (theAction == "usermanagement") || (theAction == "invoicing")) && ((window.location.pathname.indexOf("login.php") != -1) || (window.location.pathname.indexOf("portalLogin.php") != -1))){
		//Don't allow the user to go to the above pages from the standalone login page
		showDialog('Warning','Please login before proceeding.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
	}else{
		if(theAction == "ordergb"){									//Open GB map page
			if(is_ie6){
				document.varform.action = "mapie6.php";
			}else{
				document.varform.action = "map.php";
			}
			document.varform.region.value = "gb";
			document.varform.submit();
				
		}else if(theAction == "orderni"){							//Open NI map page
			if(is_ie6){
				document.varform.action = "mapie6.php";
			}else{
				document.varform.action = "map.php";
			}
			document.varform.region.value = "ni";
			document.varform.submit();
				
		}else if((theAction == "reload") || (theAction == "home")){	//Show account home page
			document.varform.action = "account_home.php";
			document.varform.submit();
		}else if(theAction == "userorders"){						//Show user orders page
			document.varform.action = "userOrders.php";
			document.varform.submit();
		}else if(theAction == "usersavedorders"){					//Show saved user orders page
			document.varform.action = "userSavedOrders.php";
			document.varform.submit();
		}else if(theAction == "licencemanagement"){					//Show licence management page
			document.varform.action = "licenceManagement.php";
			document.varform.submit();
		}else if(theAction == "costmanagement"){					//Show cost management page
			document.varform.action = "costManagement.php";
			document.varform.submit();
		}else if(theAction == "user"){								//Show user account settings
			document.varform.action = "user.php";
			document.varform.submit();
		}else if(theAction == "invoicing"){							//Show invoicing info page
			document.varform.action = "invoicing.php";
			document.varform.submit();
		}else if(theAction == "companyinfo"){						//Show company info page
			document.varform.action = "companyInfo.php";	
			document.varform.submit();
		}else if(theAction == "usermanagement"){					//Show company users
			document.varform.action = "userManagement.php";	
			document.varform.submit();
		}else if(theAction == "newcompany"){     					//Show input for new company
			document.varform.action = "newCompany.php";	
			document.varform.submit();
		}else if(theAction == "buynow"){							//Adds selected saved items to the shopping basket for purchase		
			if(document.saveorderform.tobuynow.value == ""){
				showDialog('Warning','Please ensure that items to buy are selected.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');	
			}else{
				document.varform.addSaved.value = document.saveorderform.tobuynow.value;   //Flag that when we get to shopping basket we need to transfer contents to temp table to continue with the order
				document.varform.action = "basket.php";	
				document.varform.submit();
			}	
		}else if(theAction == "resupply"){
			document.prevorderform.action = "resupply.php";
			document.prevorderform.target = "_self";
			document.prevorderform.submit();
		}else if(theAction == "userpreviews"){
			document.varform.action = "userPreviews.php";
			document.varform.submit()
		}
	}	
}
//****************Update jobrefs
function updateJobRef(theOID){
	document.prevorderform.upJobref.value = document.getElementById("newjobref" + theOID).value;
	document.prevorderform.upJobrefID.value = theOID;
	document.prevorderform.submit();
}
//*******************Shows the full info regarding the order which is used as a VAT reciept
function showReceipt(theNo){			//Superseeded by PDF functionality
	document.prevorderform.receiptID.value = theNo;
	document.prevorderform.action = "admin/showReceipt.php";
	document.prevorderform.target = "_blank";	
	document.prevorderform.submit();
	document.prevorderform.action = "";
	document.prevorderform.target = "";
}
function showReceiptPDF(theNo,progID,linkLabel){
	if(linkLabel =="On-line Payment"){			//Ensure tool tip is destroyed
		tt_HideTip();
	}
	document.prevorderform.receiptID.value = theNo;	
	document.getElementById(progID).innerHTML = "</em><img src='images/loader.gif'>  Generating...</em>";
	var qstring = "receiptID=" + theNo + "&theProgId=" + progID  + "&theLinkLabel=" + linkLabel;
	var receiptPDFReq = new ajaxObject('admin/showReceiptPDF.php',receiptPDFResponse);
	receiptPDFReq.update(qstring,'POST');
}
//****************Response from generating invoice
function receiptPDFResponse(responseText, responseStatus){
	if(responseStatus == 200){
		//Get the returned values	
		if(responseText != ""){	
			if(responseText.indexOf("TCPDF ERROR") != -1){
				showDialog('Error','There has been a problem generating the receipt. Please try again.<br/><br/><input type="button" value="Close" id="closeButton" onclick="reloadAfterReceiptFail();" >','errordialog');
			}else{
				var tempArray = new Array();
				tempArray = responseText.split("|");	
				//Recreate the link
				if(tempArray[3] == "View"){
					document.getElementById(tempArray[0]).innerHTML = "<p align='center'><a href='javascript:showReceiptPDF(\"" + tempArray[1] + "\",\"" +  tempArray[0] + "\",\"View\");'>View</a></p>";
				}else{
					document.getElementById(tempArray[0]).innerHTML = "<p><a href='javascript:showReceiptPDF(\"" + tempArray[1] + "\",\"" +  tempArray[0] + "\",\"" +  tempArray[3] + "\");' onmouseover='Tip(\"Click for a receipt\");'>" + tempArray[3] + "</a></p>";
				}			
				//Ensure the url agrees with that of the current white label
				var tempURLArray = document.URL.split("/");
				var pdfPath ="";
				for(u=0;u < (tempURLArray.length-1);u++){
					pdfPath += tempURLArray[u] + "/";
				}			
				window.location="downloadPDF.php?name=" + tempArray[2] + ".pdf";						
				//var pdfURL = pdfPath + "admin/excelexports/" + tempArray[2] + ".pdf";	
				//var winName =  "pdfreceipt";
				//var features = "toolbar=no,scrollbars=yes,resizable=yes,width=600,height=600";			
				//window.open(pdfURL,winName,features);    //OLD SHOW in popup
				//document.prevorderform.action = pdfURL;
				//document.prevorderform.target = "_blank";	
				//document.prevorderform.submit();
				//document.prevorderform.action = "";
				//document.prevorderform.target = "";
			}		
		}else{
			showDialog('Error','There has been a problem generating the receipt. Please try again.<br/><br/><input type="button" value="Close" id="closeButton" onclick="reloadAfterReceiptFail();" >','errordialog');
		}
	}
}
//*******************Refreshes the page to show the receipt links when the receipt process fails
function reloadAfterReceiptFail(){
	hideDialog();
	document.invoicingform.submit();
}
//*******************Shows the full info regarding the order item
function getOrder(theOrderNo){
	document.varform.oid.value = theOrderNo;
	document.varform.action = "orderInfo.php";
	document.varform.submit();
}
//*******************Shows the full info regarding the a saved order item
function getSavedOrder(theOrderNo){
	document.varform.oid.value = theOrderNo;
	document.varform.action = "savedOrderInfo.php";
	document.varform.submit();
}
//*******************Go back from orderinfo page
function backFromOrder(backTo){
	document.varform.action = backTo + ".php";
	document.varform.submit();
}
//*******************Removes the saved order item
function removeSavedItem(theOrderID,theAction){
	if(theAction == "prompt"){
		var dialogContent = "This will completely remove this item. Are you sure?<br/><br/><input type='button' value='Yes' id='yesButton' onclick='removeSavedItem(\"" + theOrderID + "\",\"remove\");'>  <input type='button' value='No' id='noButton' onclick='hideDialog();' >"
		showDialog('',dialogContent,'prompt');
	}else if(theAction == "remove"){
		hideDialog();
		document.saveorderform.rem.value = theOrderID;		
		document.saveorderform.action = "userSavedOrders.php";
		document.saveorderform.submit();  
	}
}
//****************Update company info
function updateUserCompanyInfo(theType){
	if(validateCompanyInput()){		//If all OK register the user	
		//Define the query string to pass variables
		var qstring = "companyid=" + document.compform.companyid.value;
		qstring += "&company=" + escapeAmp(document.compform.company.value);
		qstring += "&address1=" + document.compform.address1.value;
		qstring += "&address2=" + document.compform.address2.value;		
		qstring += "&town=" + document.compform.town.value;
		qstring += "&county=" + document.compform.county.value;
		qstring += "&country=" + document.compform.country.value;
		qstring += "&postcode=" + document.compform.postcode.value;
		qstring += "&telephone=" + document.compform.telephone.value;
		qstring += "&fax=" + document.compform.fax.value;
		qstring += "&website=" + document.compform.website.value;
		qstring += "&oneclickorder=" + document.compform.oneclickorder.value;
		qstring += "&notifyid=" + document.compform.notifyid.value;
		qstring += "&adminnotify=" + document.compform.adminnotify.value;
		qstring += "&monthlyspend=" + document.compform.monthlyspend.value;
		qstring += "&ponumber=" + document.compform.ponumber.value;
		qstring += "&paymentmethod=" + document.compform.paymentmethod.value;
		if(theType == "add"){		
			qstring += "&tenkdata=" + document.compform.tenkdata.value;
			qstring += "&accaccess=" + document.compform.accaccess.value;
			qstring += "&paperinvoice=" + document.compform.paperinvoice.value;
			qstring += "&tariffref=" + document.compform.tariffref.value;
			qstring += "&salesperson=" + document.compform.salesperson.value;
			qstring += "&outcode=" + document.compform.outcode.value;
			qstring += "&useronlydata=" + document.compform.useronlydata.value;
			qstring += "&pcharge=" + document.compform.pcharge.value;
		}
		qstring += "&cadd=" + theType;	
		var upCompanyInfoReq = new ajaxObject('upCompany.php', updateCompanyInfoResponse);
		upCompanyInfoReq.update(qstring,'POST');
	}
}
//****************Response from updating company info
function updateCompanyInfoResponse(responseText, responseStatus){
	if(responseStatus == 200){
		if(responseText != ""){
			var tempArray = new Array();
			tempArray = responseText.split("|");
			showDialog('',tempArray[1] + '<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','prompt');
		}else{
			showDialog('Error','There has been a problem updating. Please try again.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','errordialog');	
		}
	}
}
//****************Validate company input 
function validateCompanyInput(){
	//Company name field must not be empty
	if(document.getElementById("company").value == "") {
		showDialog('Warning','Please enter a value for the "Company Name" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("company").focus();
		return (false);
	}
	//Address field must not be empty
	if(document.getElementById("address1").value == "") {
		showDialog('Warning','Please enter a value for the "Address" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("address1").focus();
		return (false);
	}
	//Town field must not be empty
	if(document.getElementById("town").value == "") {
		showDialog('Warning','Please enter a value for the "Town" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("town").focus();
		return (false);
	}
	//Post Code field must not be empty
	if(document.getElementById("postcode").value == "") {
		showDialog('Warning','Please enter a value for the "Post Code" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("postcode").focus();
		return (false);
	}
	//Check Telephone for telephone number
	if(document.getElementById("telephone").value == ""){
		showDialog('Warning','Please enter a value for the "Telephone" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("telephone").focus();
		return (false);
	}
	var checkOK = "0123456789-+() \t\r\n\f";
	var checkStr = document.getElementById("telephone").value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length){
		allValid = false;
		break;
		}
	}		
	if (!allValid){
		showDialog('Warning','Please enter only digit, whitespace and "+()" characters in the "Telephone" field.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
		document.getElementById("telephone").focus();
		return (false);
	}
	//All OK
	return (true);
}
//*******************Show a previous custom product the user has bought data for in the main map window.
function showIt(passedGeom,theData,OVString,prodType,extentInput,theScale,theAllowOverlays,theMaxSize,theDiscountModel,theMinPrice,thePriceperkm,theStyle,theOnlineSize,theDataName,theFullDataName,thePriceCode,theUserShapes,passedGSInfo,passedScales,ismulti,hasSuppliers,theRegion,theInterface,isLimited,fromSB,theFixedIds){		
	//Define the form name to submit based on if come from account or shopping basket
	if(fromSB == "frombasket"){
		var theFormName = "confirmform";
	}else{
		var theFormName = "varform";
	}
	//Remove MySQL formating
	if(passedGeom.indexOf("POINT") == -1){
		var sep1 = passedGeom.indexOf("((");	
		var sep2 = passedGeom.indexOf("))");	
		extentGeom =  passedGeom.substring((sep1+2),sep2);
	}else{
		var sep1 = passedGeom.indexOf("(");	
		var sep2 = passedGeom.indexOf(")");	
		extentGeom =  passedGeom.substring((sep1+1),sep2);
	}
	//Define the type of map extent input
	if((extentInput != "radius") && (extentInput != "extent") && (extentInput != "user")  && (extentInput != "ground") && (extentInput != "draw") && (extentInput != "") && (extentInput != "gspoint") && (extentInput != "gsarea") && (extentInput != "gsmap") && (extentInput != "mapping")){   
		eval("document." + theFormName + ".extentinput.value = 'paper'");
	}else{
		eval("document." + theFormName + ".extentinput.value = '" + extentInput + "'");
	}
	//Store info of the selected previous order in hidden fields
	eval("document." + theFormName + ".papersize.value = '" + extentInput + "'");
	eval("document." + theFormName + ".extent.value = '" + extentGeom + "'");
	eval("document." + theFormName + ".data.value = '" + theData + "'");
	eval("document." + theFormName + ".scale.value = '" + theScale + "'");
	eval("document." + theFormName + ".dataDescript.value = '" + theDataName + "'");
	eval("document." + theFormName + ".fullDataName.value = '" + theFullDataName + "'");
	eval("document." + theFormName + ".productoption.value = 'custom'");	
	if(fromSB == "frombasket"){
		eval("document." + theFormName + ".currentSavedShapes.value = '" + theUserShapes + "'");	
	}
	eval("document." + theFormName + ".userShapes.value = '" + theUserShapes + "'");
	eval("document." + theFormName + ".gsInfo.value = '" + passedGSInfo + "'");
	eval("document." + theFormName + ".thescales.value = '" + passedScales + "'");
	eval("document." + theFormName + ".ismultiextent.value = '" + ismulti + "'");
	eval("document." + theFormName + ".suppliers.value = '" + hasSuppliers + "'");
	eval("document." + theFormName + ".region.value = '" + theRegion + "'");
	eval("document." + theFormName + ".availibility.value = '" + isLimited + "'");
	eval("document." + theFormName + ".fixedIDS.value = '" + theFixedIds + "'");	
	//Show the map page
	if(fromSB == "frombasket"){
		if(is_ie6){
			document.confirmform.action = "mapie6.php";		
		}else{
			document.confirmform.action = "map.php";
		}		
		document.confirmform.submit();
	}else{
		if(is_ie6){
			document.varform.action = "mapie6.php";		
		}else{
			document.varform.action = "map.php";
		}	
		document.varform.submit();		
	}	
}

//-------------------------------------------------------------------BUY SAVED ORDERS SCRIPTS
//*******************Store the ID of the saved order when the user chooses that this is to be purchasd now.
function getBuyNowID(selObj,theID){
	if(selObj.checked){
		selObj.value = theID;
		var theAction = "add";
	}else{
		selObj.value = "";
		var theAction = "rem";
	}	
	var currentVal = document.saveorderform.tobuynow.value;			
	if(theAction == "add"){											//Add to list of IDS
		if(currentVal != ""){
			var newVal = currentVal + "," + theID
		}else{
			var newVal = theID
		}
	}else{															//Remove from list of IDS
		var theBuyNowIDS = new Array();
		theBuyNowIDS = currentVal.split(',');
		newVal = "";
		for(var c=0;c<(theBuyNowIDS.length);c++){
			if(theBuyNowIDS[c] != theID){
				if(newVal == ""){
					newVal = theBuyNowIDS[c];
				}else{
					newVal += "," + theBuyNowIDS[c]; 
				}	
			}
		}
	}	
	document.saveorderform.tobuynow.value = newVal;
}

//-------------------------------------------------------------------------INVOICING

//*******************Changes the year of the monthly invoice values breakdown
function showyearly(selObj){	
	var theYear = selObj.options[selObj.selectedIndex].value;
	document.invoicingform.theSelectedYear.value = theYear;
	document.invoicingform.action = "invoicing.php";
	document.invoicingform.target = '_self';
	document.invoicingform.submit();
}
//*******************Show break down of invoice values ready for on-line payment
function showCompanyInvoice(theInvoiceNo){
	document.invoicingform.invoicenotopass.value = theInvoiceNo;	
	document.invoicingform.action = "companyInvoices.php";
	document.invoicingform.submit();
}
//*******************Go back from user's payment page to invoicingpage
function backFromPayment(){
	document.varform.action = "invoicing.php";
	document.varform.submit();
}
//*******************Mark individual invoice to pay or not
function selInvoiceToPay(selObj,theAmmount,theForm){
	//Store amount to pay
	if(selObj.checked){
		selObj.value = "yes";
	}else{
		selObj.value = "";
	}
	var testObj = document.getElementById("payit");
	if(testObj != null){
		document.companyinvoicesform.payit.checked=false;		//Ensure the select all is turned off		
	}
}
//*******************Show the selected invoice for single user accounts (NOT CALLED)
function showSingleInvoice(theNo,isDummy,invoiceStart,invoiceEnd){
	//Define the page to show	
	if(isDummy == "yes"){
		var theInvoiceType = "dummyinvoice";
	}else{
		var theInvoiceType = "invoice";
	}
	//Set the hidden field and submit
	document.invoicingform.invoicenotopass.value = theNo;
	document.invoicingform.invoicetype.value = theInvoiceType;
	document.invoicingform.invoicestart.value = invoiceStart;
	document.invoicingform.invoiceend.value = invoiceEnd;
	document.invoicingform.action = 'admin/showinvoice.php';
	document.invoicingform.target = '_blank';	
	document.invoicingform.submit();
}
//*******************Show the part payments for company accounts
function showPartPayment(passedId,theAction){
	if(theAction == "officeinvoiced"){							//Part payment for an office that has been invoiced
		document.companyinvoicesform.invoicenotopass.value = passedId;
		document.companyinvoicesform.action = 'partInvoicePayments.php';
	}else if(theAction == "officenotinvoiced"){					//Part payment for an office that has not been invoiced yet		
		document.companyinvoicesform.officetopass.value = passedId;
		document.companyinvoicesform.action = 'partPayments.php';
	}
	document.companyinvoicesform.target = '_self';	
	document.companyinvoicesform.submit();
}
//*******************Show the part payments for single user accounts
function showPartPayment2(passedId,theAction,startDate,endDate){
	if(theAction == "singleinvoiced"){					//Part payment for an single user that has been invoiced
		document.invoicingform.invoicenotopass.value = passedId;
		document.invoicingform.action = 'partInvoicePayments.php';
	}else if(theAction == "singlenotinvoiced"){					//Part payment for an single user that has not been invoiced yet
		document.invoicingform.officetopass.value = passedId;
		document.invoicingform.action = 'partPayments.php';
	}
	document.invoicingform.invoicestart.value = startDate;
	document.invoicingform.invoiceend.value = endDate;	
	document.invoicingform.target = '_self';	
	document.invoicingform.submit();	
}
//*******************Show the selected invoice for company accounts (NOT CALLED)
function showMultiInvoice(theNo,theSrc,isDummy){	
	if(theSrc == "single"){												//**Show an office invoice
		if(isDummy == "yes"){
			var theInvoiceType = "dummyofficeinvoice";
		}else{
			var theInvoiceType = "officeinvoice";
		}
	}else if(theSrc == "all"){											//Show a company invoice (made up of office invoices)	
		if(isDummy == "yes"){
			var theInvoiceType = "dummycompanyinvoice";
		}else{
			var theInvoiceType = "companyinvoice";
		}
	}
	//Set the hidden field and submit
	document.companyinvoicesform.invoicenotopass.value = theNo;
	document.companyinvoicesform.invoicetype.value = theInvoiceType;
	document.companyinvoicesform.action = 'admin/showinvoice.php';
	document.companyinvoicesform.target = '_blank';	
	document.companyinvoicesform.submit();
}
//*******************Pay order on-line (not Needed)
function payOrder(){	
	document.oordersform.action = "onlinePayment.php";
	document.oordersform.target = "_self";
	document.oordersform.submit();
}
//*******************Store the ID of the order when the user chooses that this is to be invoiced or pay now.
function getInvoiceID(selObj,theID){
	//Add ID to the value
	if(selObj.checked){
		selObj.value = theID;
	}else{
		selObj.value = "";
	}	
}

//-------------------------------------------------End of file.
