String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

function relocate(url) {
	window.location = url;
}

function handleSessionValidatity(blnValid) {
	if (typeof blnValid == 'undefined' || blnValid == 'false' ) {
		relocate('index.cfm?blnTimeout=1')
	}
}

function getCurrentStepId() {
	//get form id's
	try {
		//return parseInt($.GetId($.jqURL.qs()));
		return parseInt($.GetId($.jqURL.get('go')));
	}
	catch(err) {
		return 1;
	}
}

function getFieldPrefix(chrField,numElementsToDiscard) {
	var numFieldParts = $.ListLen(chrField,'_');
	var chrFieldPrefix = chrField;
	for (i=1;i<=numElementsToDiscard;i++) {
		chrFieldPrefix = $.ListDeleteAt(chrFieldPrefix,numFieldParts-i+1,'_');
	}
	return chrFieldPrefix;
}

function checkChars(e,charset,type) {
	if (isValidKeyCodeOperation(e)) {
		return true;
	} else {
		var k = getKeyCode(e);
		//numeric key pad
		if (k > 95 && k < 106) {
			k = k - 48;
		}
		charInd = charset.indexOf(String.fromCharCode(k));
		//alert(String.fromCharCode(k));
		if (type == 'allow') {
			return (charInd != -1);
		} else {
			return (charInd == -1);
		}
	}
}

function checkCharsAfter(chrFieldId,charset,formatType) {
	chrFieldSel = '#' + chrFieldId;
	chrFieldVal = $(chrFieldSel).val();
	chrChar = $.Mid(chrFieldVal,chrFieldVal.length,1);
	if (!$.Len(chrChar)) {
		return true;
	} else {
		charInd = charset.indexOf(chrChar);
	}
	if (charInd == -1) {
		chrNewVal = $.Left(chrFieldVal,chrFieldVal.length-1);
		$(chrFieldSel).val(chrNewVal)
	} else {
		if (typeof formatType != 'undefined') {
			switch($.LCase(formatType)) {
				case 'upper':
					$(chrFieldSel).val(chrFieldVal.toUpperCase())
					break;
				case 'capitalize':
					$(chrFieldSel).val(capitalize(chrFieldVal));
					break;
			}
		}
	}
}


function getKeyCode(e) {
	return document.all?parseInt(e.keyCode): parseInt(e.which);
}

/*
function isNumericKeyCode(event)
{

	 //don't allow negatives 
	 if (event.keyCode == 109 || event.keyCode == 189) {
	 	return false;
	 }	else {
		  if  ( isValidKeyCodeOperation(event) ||
		      (event.keyCode >= 48 && event.keyCode <= 57) || 
		      (event.keyCode >= 96 && event.keyCode <= 105) ) {
		    return true; }
		  else {
		    return false;
		  }
	}
}*/

function isValidKeyCodeOperation(e) {
	var k = getKeyCode(e);
	 if  (k == 8 || k == 9 || k == 16 || k == 46 || k == 37 || k == 39) {
	 	return true;
	 } else {
	 	return false;
	 }
	
} 

function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
   
	this.removeItem = function(in_key)
	{
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_value = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_value;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}

			this.items[in_key] = in_value;
		}
	   
		return in_value;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}
}

// Trim,Rtrim,Ltrim methods added to all strings
// eg var myString=" hello world " myString.trim();

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function capitalize(str_sentence)
{
    return str_sentence.toLowerCase().replace(/\b[a-z]/g, convertToUpper);

    function convertToUpper()
    {
        return arguments[0].toUpperCase();
    }
}

function showNoQuoteMessage() {
	
	var phonenumber = $('#hidden_phoneNumber').val();
	var message1 = "Unable to provide a quote";
	
	if ($('#hidden_affinity').val() == "RIDE"){
	 var message2 = "Unfortunately we are unable to provide you with an online quotation. Please give us a call on <span style='color: red'>"+phonenumber+"</span> so we can discuss your requirements.";
	 showModal(message1,message2);
	 //showModal("Unable to provide a quote","Unfortunately we are unable to provide you with an online quotation. Please give us a call on <span style='color: red'>0844 800 4313</span> so we can discuss your requirements.");
	}
	else{
		switch($('#hidden_affinity').val()) {
			case 'MCD':
				phoneNumber='0844 800 0955';
				break;
			case 'FOWL':
				phoneNumber='0844 800 6052';
				break;
			case 'ENDS':
				phoneNumber='0844 800 6028';
				break;
		}
		var message2 = "Unfortunately we are unable to provide you with an online quotation. Please give us a call on <span style='color: red'>"+phonenumber+"</span> (Mon-Thurs 09.00-19.00,Fri 09.00-18.00, Sat 09.00-16.00) so we can discuss your requirements.";
		showModal(message1,message2);
	}
}

function showPostCodeError() {
	showModal("You have changed the Postcode","Please click on Get my address.");
}	


