//This file is used on the store order form used to fullfill Prepaid Wireless orders through Beanstream.
//The form is located at /forms/store-cc-order-form.html

function gup(name){
	//This function is used to extract values or variables from the url string
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null)  
		return "None";
	else
		return results[1];
}

function CancelForm(){
	//This is the function for when a user clicks on the Cancel button on the form.  It resets the form and then forwards the user to sasktel.com
	document.store_order.reset();
	window.location="http://www.sasktel.com/";
}

function PostForm(){
	
	Stamp = new Date();
	Month = Stamp.getMonth();
	Day = Stamp.getDate();
	Year = Stamp.getYear();
	Hours = Stamp.getHours();
	Minutes = Stamp.getMinutes();
	Seconds = Stamp.getSeconds();
	OrderNumberStamp = document.store_order.ref1.value + Month + Day + Year + Hours + Minutes + Seconds;
	document.store_order.trnOrderNumber.value = OrderNumberStamp;
	
	document.store_order.merchant_id.value = wBeanStreamMerchID;
	
	if(document.store_order.ref2.value == 'Basic20')
		{
		document.store_order.trnAmount.value = '22.00';
		tax = "PST=100,GST=100";
		}
	else if(document.store_order.ref2.value == 'Basic50')
		{
		document.store_order.trnAmount.value = '55.00';
		tax = "PST=250,GST=250";
		}
	else
		{
		alert("You need to pick a Top-up amount to continue with your request.");
		return
		}
	verifyMDN(document.store_order.ref1.value);
}

function GetURLPass(){
	document.store_order.ref2.value = gup('prod');
}

function verifyMDN(mdn)
            {
				new Ajax.Request('/Accolade/Validate', {
                    method: 'post',
                    parameters: {mdn: mdn},
                    onSuccess: function(transport){
                        var response = transport.responseText;
                        if(response=='1'){
                            alert('The number you have entered is not a valid SaskTel prepaid cellular phone number.');
							return
                        }
                        else {
 							if((document.store_order.ref1.value != document.store_order.confirmMDN.value) || (document.store_order.ref1.value == ""))
							{
								alert("Please ensure that the numbers you provide match exactly in order to confirm.");
								return
							}
	
						document.store_order.ref4.value = tax;
						document.store_order.submit();
                        }
                    },
                    onFailure: function(){ alert('Sorry your SaskTel prepaid cellular phone number could not be verified at this time.'); }
                });
            }

