var gpr = gpr || {};
gpr.consts = 
{	ajaxRequestHeader: 'X-GPR-AjaxRequest',	ajaxErrorHeader: 'X-GPR-AjaxError',
	registrationFee : -49.99,	memebershipFee :-19.99,	dateSeparator: '/',
	timeStampErrorMessage : 'The data you are about to save is modified by other user, Please review the new data and save again.',
	minYear:1900,	maxYear:2100,	maxDate:'12/31/9999',	dateFormat:'%m/%d/%Y', dateTimeFormat:'%m/%d/%Y %I:%M %P'
};
gpr.registry = 
{	
	list: [],	idSeed: 0,	idPrefix: 'gpr_unique_',
	add: function(obj){this.list[obj.id] = obj;	},	
	get: function(id){	return this.list[id];},	
	uniqueID: function(){	return this.idPrefix + (this.idSeed ++);}
};
gpr.dispatcher = 
{	
	nop: function()	{	},	
	call: function(id, method, param1, param2, param3,param4)	
	{	
		//debugger;	
		var obj = gpr.registry.get(id);			
		if ( typeof obj == 'object' && typeof obj[method] == 'function' )		
		{
			return obj[method](param1, param2, param3,param4);		
		}
		return null;
	},	
	bind0: function(id, method)
	{	
		var obj = gpr.registry.get(id);	
		if ( typeof obj == 'object' && typeof obj[method] == 'function' )	
		{	return this.bindEx0(obj, method);	}	return this.nop;
	},	
	bindEx0: function(obj, method)	
	{	
		return function(param1, param2, param3)	
		{	return obj[method](param1, param2, param3);	}
	},	
	bindEx1: function(obj, method, param1)
	{	
		return function(param2, param3)	
		{		return obj[method](param1, param2, param3);	}
	}
};
gpr.ajax = function()
{	
	var xhr;  	
	if(window.XMLHttpRequest)	
	{ 		xhr = 	new XMLHttpRequest();	}	
	else	
	{		xhr = 	new ActiveXObject("Microsoft.XMLHTTP");
	};	
	this.post = function(url, body, onComplete)	
	{
		if ( onComplete )	
		{	
			var it = this;		
			xhr.onreadystatechange = function()		
			{			
				if ( xhr.readyState == 4 /*complete*/ )		
				{		
					xhr.onreadystatechange = gpr.dispatcher.nop;
					it.checkResponse();
					onComplete(it);
				}
			}		
		}		
		else		
		{			
			xhr.onreadystatechange = gpr.dispatcher.nop;		
		}		
		xhr.open("POST", gpr.environment.virtualPath + url, true);		
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");		
		xhr.setRequestHeader(gpr.consts.ajaxRequestHeader, '1');		xhr.send(body);
	};	
	this.checkResponse = function()
	{		
		if ( xhr.status >= 300 )	
		{			
			this.error = decodeURIComponent(xhr.getResponseHeader(gpr.consts.ajaxErrorHeader)) || xhr.statusText || ('Server Error ' + xhr.status);		
		}
		else
		{
			this.error = '';		
		}	
	};	
	this.responseText = function()
	{		
		return xhr.responseText;	
	};
};
gpr.dumpObj = function(obj, padding)
{	
	padding = padding || '';	
	switch( typeof obj )	
	{		
		case 'number':
		case 'function':		
		case 'boolean':			
			return obj.toString();		
		case 'string':			
			return '\'' + obj + '\'';
		case 'object':
			if ( !obj )
			{
				return 'null';
			}			
			else if ( obj.toLocaleTimeString )			
			{				
				return obj.toDateString(); 			
			}
			else			
			{			
				var html = [];			
				for(var i in obj)		
				{			
					html.push(padding + '    ' + i + ' : ' + gpr.dumpObj(obj[i], padding + '    '));		
				}		
				return '\n' + padding + '{\n' + html.join(',\n') + '\n' + padding + '}';	
			}	
		case 'undefined':
		default:	return '';
	}
};
gpr.secretQuestions = [	'What is your pet\'s name?',	'What was the name of your first school?',	'What is your all-time favorite sports team?',	'What is your father\'s middle name?',	'What make was your first car or bike?'];gpr.countryControl = function (name, selected){ 	return '<select id="'+ name +'">'+			'<option value="USA" '+ (selected == 'USA' ? "selected" : "") +'>USA</option>'+			'</select>';};			gpr.stateControl = function (name, selected, style){ 	return '<select id="'+ name +'" '+ (style || '') + '><option value="">&#160;</option>'+	'<option value="AL" '+ (selected == 'AL' ? "selected" : "") +'>Alabama</option>'+	'<option value="AK" '+ (selected == 'AL' ? "selected" : "") +'>Alaska</option>' +	'<option value="AS"'+ (selected == 'AS' ? "selected" : "") +'>American Samoa</option>' +	'<option value="AZ"'+ (selected == 'AZ' ? "selected" : "") +'>Arizona</option>' +
	'<option value="AR"'+ (selected == 'AR' ? "selected" : "") +'>Arkansas</option>' +
	'<option value="CA"'+ (selected == 'CA' ? "selected" : "") +'>California</option>' +
	'<option value="CO"  '+ (selected == 'CO' ? "selected" : "") + '>Colorado</option>' +
	'<option value="CT"  '+ (selected == 'CT' ? "selected" : "") + '>Connecticut</option>' +
	'<option value="DE"  '+ (selected == 'DE' ? "selected" : "") + '>Delaware</option>' +
	'<option value="DC"  '+ (selected == 'DC' ? "selected" : "") + '>District of Columbia</option>' +
	'<!--option value="FM"  '+ (selected == 'FM' ? "selected" : "") + '>Federated State Micronesia</option-->' +
	'<option value="FL"  '+ (selected == 'FL' ? "selected" : "") + '>Florida</option>' +
	'<option value="GA"  '+ (selected == 'GA' ? "selected" : "") + '>Georgia</option>' +
	'<option value="GU"  '+ (selected == 'GU' ? "selected" : "") + '>Guam</option>' +
	'<option value="HI"  '+ (selected == 'HI' ? "selected" : "") + '>Hawaii</option>' +
	'<option value="ID"  '+ (selected == 'ID' ? "selected" : "") + '>Idaho</option>' +
	'<option value="IL"  '+ (selected == 'IL' ? "selected" : "") + '>Illinois</option>' +
	'<option value="IN"  '+ (selected == 'IN' ? "selected" : "") + '>Indiana</option>' +
	'<option value="IA"  '+ (selected == 'IA' ? "selected" : "") + '>Iowa</option>' +
	'<option value="KS"  '+ (selected == 'KS' ? "selected" : "") + '>Kansas</option>' +
	'<option value="KY"  '+ (selected == 'KY' ? "selected" : "") + '>Kentucky</option>' +
	'<option value="LA"  '+ (selected == 'LA' ? "selected" : "") + '>Louisiana</option>' +
	'<option value="ME"  '+ (selected == 'ME' ? "selected" : "") + '>Maine</option>' +
	'<option value="MH"  '+ (selected == 'MH' ? "selected" : "") + '>Marshall Islands</option>' +
	'<option value="MD"  '+ (selected == 'MD' ? "selected" : "") + '>Maryland</option>' +
	'<option value="MA"  '+ (selected == 'MA' ? "selected" : "") + '>Massachusetts</option>' +
	'<option value="MI"  '+ (selected == 'MI' ? "selected" : "") + '>Michigan</option>' +
	'<option value="MN"  '+ (selected == 'MN' ? "selected" : "") + '>Minnesota</option>' +
	'<option value="MS"  '+ (selected == 'MS' ? "selected" : "") + '>Mississippi</option>' +
	'<option value="MO"  '+ (selected == 'MO' ? "selected" : "") + '>Missouri</option>' +
	'<option value="MT"  '+ (selected == 'MT' ? "selected" : "") + '>Montana</option>' +
	'<option value="NE"  '+ (selected == 'NE' ? "selected" : "") + '>Nebraska</option>' +
	'<option value="NV"  '+ (selected == 'NV' ? "selected" : "") + '>Nevada</option>' +
	'<option value="NH"  '+ (selected == 'NH' ? "selected" : "") + '>New Hampshire</option>' +
	'<option value="NJ"  '+ (selected == 'NJ' ? "selected" : "") + '>New Jersey</option>' +
	'<option value="NM"  '+ (selected == 'NM' ? "selected" : "") + '>New Mexico</option>' +
	'<option value="NY"  '+ (selected == 'NY' ? "selected" : "") + '>New York</option>' +
	'<option value="NC"  '+ (selected == 'NC' ? "selected" : "") + '>North Carolina</option>' +
	'<option value="ND"  '+ (selected == 'ND' ? "selected" : "") + '>North Dakota</option>' +
	'<!--option value="MP"  '+ (selected == 'MP' ? "selected" : "") + '>Northern Mariana Islands</option-->' +
	'<option value="OH"  '+ (selected == 'OH' ? "selected" : "") + '>Ohio</option>' +
	'<option value="OK"  '+ (selected == 'OK' ? "selected" : "") + '>Oklahoma</option>' +
	'<option value="OR"  '+ (selected == 'OR' ? "selected" : "") + '>Oregon</option>' +
	'<option value="PW"  '+ (selected == 'PW' ? "selected" : "") + '>Palau</option>' +
	'<option value="PA"  '+ (selected == 'PA' ? "selected" : "") + '>Pennsylvania</option>' +
	'<option value="PR"  '+ (selected == 'PR' ? "selected" : "") + '>Puerto Rico</option>' +
	'<option value="RI"  '+ (selected == 'RI' ? "selected" : "") + '>Rhode Island</option>' +
	'<option value="SC"  '+ (selected == 'SC' ? "selected" : "") + '>South Carolina</option>' +
	'<option value="SD"  '+ (selected == 'SD' ? "selected" : "") + '>South Dakota</option>' +
	'<option value="TN"  '+ (selected == 'TN' ? "selected" : "") + '>Tennessee</option>' +
	'<option value="TX"  '+ (selected == 'TX' ? "selected" : "") + '>Texas</option>' +
	'<option value="UT"  '+ (selected == 'UT' ? "selected" : "") + '>Utah</option>' +
	'<option value="VT"  '+ (selected == 'VT' ? "selected" : "") + '>Vermont</option>' +
	'<option value="VI"  '+ (selected == 'VI' ? "selected" : "") + '>Virgin Islands</option>' +
	'<option value="VA"  '+ (selected == 'VA' ? "selected" : "") + '>Virginia</option>' +
	'<option value="WA"  '+ (selected == 'WA' ? "selected" : "") + '>Washington</option>' +
	'<option value="WV"  '+ (selected == 'WV' ? "selected" : "") + '>West Virginia</option>' +
	'<option value="WI"  '+ (selected == 'WI' ? "selected" : "") + '>Wisconsin</option>' +
	'<option value="WY"  '+ (selected == 'WY' ? "selected" : "") + '>Wyoming</option></select>';
};gpr.yearControl = function(id){	var html = '<select id="' + id + '" disabled>' ;	var crntYear = (new Date()).getYear();	for( var i = crntYear; i < (crntYear +12); ++i )	{									html += '<option value="' + i + '">' + i  + '</option>';	}	html += '</select>';	return html;};

// added by deepak, Nous 23-Jan-2007
gpr.countryControls = function (name, selected){ 	return '<select id="'+ name +'" disabled>'+			'<option value="USA" '+ (selected == 'USA' ? "selected" : "") +'>USA</option>'+			'</select>';};

// added by deepak, Nous 23-Jan-2007
gpr.stateControls = function (name, selected, style){ 	return '<select id="'+ name +'" '+ (style || '') + ' disabled><option value="">&#160;</option>'+	'<option value="AL" '+ (selected == 'AL' ? "selected" : "") +'>Alabama</option>'+	'<option value="AK" '+ (selected == 'AL' ? "selected" : "") +'>Alaska</option>' +	'<option value="AS"'+ (selected == 'AS' ? "selected" : "") +'>American Samoa</option>' +	'<option value="AZ"'+ (selected == 'AZ' ? "selected" : "") +'>Arizona</option>' +
	'<option value="AR"'+ (selected == 'AR' ? "selected" : "") +'>Arkansas</option>' +
	'<option value="CA"'+ (selected == 'CA' ? "selected" : "") +'>California</option>' +
	'<option value="CO"  '+ (selected == 'CO' ? "selected" : "") + '>Colorado</option>' +
	'<option value="CT"  '+ (selected == 'CT' ? "selected" : "") + '>Connecticut</option>' +
	'<option value="DE"  '+ (selected == 'DE' ? "selected" : "") + '>Delaware</option>' +
	'<option value="DC"  '+ (selected == 'DC' ? "selected" : "") + '>District of Columbia</option>' +
	'<!--option value="FM"  '+ (selected == 'FM' ? "selected" : "") + '>Federated State Micronesia</option-->' +
	'<option value="FL"  '+ (selected == 'FL' ? "selected" : "") + '>Florida</option>' +
	'<option value="GA"  '+ (selected == 'GA' ? "selected" : "") + '>Georgia</option>' +
	'<option value="GU"  '+ (selected == 'GU' ? "selected" : "") + '>Guam</option>' +
	'<option value="HI"  '+ (selected == 'HI' ? "selected" : "") + '>Hawaii</option>' +
	'<option value="ID"  '+ (selected == 'ID' ? "selected" : "") + '>Idaho</option>' +
	'<option value="IL"  '+ (selected == 'IL' ? "selected" : "") + '>Illinois</option>' +
	'<option value="IN"  '+ (selected == 'IN' ? "selected" : "") + '>Indiana</option>' +
	'<option value="IA"  '+ (selected == 'IA' ? "selected" : "") + '>Iowa</option>' +
	'<option value="KS"  '+ (selected == 'KS' ? "selected" : "") + '>Kansas</option>' +
	'<option value="KY"  '+ (selected == 'KY' ? "selected" : "") + '>Kentucky</option>' +
	'<option value="LA"  '+ (selected == 'LA' ? "selected" : "") + '>Louisiana</option>' +
	'<option value="ME"  '+ (selected == 'ME' ? "selected" : "") + '>Maine</option>' +
	'<option value="MH"  '+ (selected == 'MH' ? "selected" : "") + '>Marshall Islands</option>' +
	'<option value="MD"  '+ (selected == 'MD' ? "selected" : "") + '>Maryland</option>' +
	'<option value="MA"  '+ (selected == 'MA' ? "selected" : "") + '>Massachusetts</option>' +
	'<option value="MI"  '+ (selected == 'MI' ? "selected" : "") + '>Michigan</option>' +
	'<option value="MN"  '+ (selected == 'MN' ? "selected" : "") + '>Minnesota</option>' +
	'<option value="MS"  '+ (selected == 'MS' ? "selected" : "") + '>Mississippi</option>' +
	'<option value="MO"  '+ (selected == 'MO' ? "selected" : "") + '>Missouri</option>' +
	'<option value="MT"  '+ (selected == 'MT' ? "selected" : "") + '>Montana</option>' +
	'<option value="NE"  '+ (selected == 'NE' ? "selected" : "") + '>Nebraska</option>' +
	'<option value="NV"  '+ (selected == 'NV' ? "selected" : "") + '>Nevada</option>' +
	'<option value="NH"  '+ (selected == 'NH' ? "selected" : "") + '>New Hampshire</option>' +
	'<option value="NJ"  '+ (selected == 'NJ' ? "selected" : "") + '>New Jersey</option>' +
	'<option value="NM"  '+ (selected == 'NM' ? "selected" : "") + '>New Mexico</option>' +
	'<option value="NY"  '+ (selected == 'NY' ? "selected" : "") + '>New York</option>' +
	'<option value="NC"  '+ (selected == 'NC' ? "selected" : "") + '>North Carolina</option>' +
	'<option value="ND"  '+ (selected == 'ND' ? "selected" : "") + '>North Dakota</option>' +
	'<!--option value="MP"  '+ (selected == 'MP' ? "selected" : "") + '>Northern Mariana Islands</option-->' +
	'<option value="OH"  '+ (selected == 'OH' ? "selected" : "") + '>Ohio</option>' +
	'<option value="OK"  '+ (selected == 'OK' ? "selected" : "") + '>Oklahoma</option>' +
	'<option value="OR"  '+ (selected == 'OR' ? "selected" : "") + '>Oregon</option>' +
	'<option value="PW"  '+ (selected == 'PW' ? "selected" : "") + '>Palau</option>' +
	'<option value="PA"  '+ (selected == 'PA' ? "selected" : "") + '>Pennsylvania</option>' +
	'<option value="PR"  '+ (selected == 'PR' ? "selected" : "") + '>Puerto Rico</option>' +
	'<option value="RI"  '+ (selected == 'RI' ? "selected" : "") + '>Rhode Island</option>' +
	'<option value="SC"  '+ (selected == 'SC' ? "selected" : "") + '>South Carolina</option>' +
	'<option value="SD"  '+ (selected == 'SD' ? "selected" : "") + '>South Dakota</option>' +
	'<option value="TN"  '+ (selected == 'TN' ? "selected" : "") + '>Tennessee</option>' +
	'<option value="TX"  '+ (selected == 'TX' ? "selected" : "") + '>Texas</option>' +
	'<option value="UT"  '+ (selected == 'UT' ? "selected" : "") + '>Utah</option>' +
	'<option value="VT"  '+ (selected == 'VT' ? "selected" : "") + '>Vermont</option>' +
	'<option value="VI"  '+ (selected == 'VI' ? "selected" : "") + '>Virgin Islands</option>' +
	'<option value="VA"  '+ (selected == 'VA' ? "selected" : "") + '>Virginia</option>' +
	'<option value="WA"  '+ (selected == 'WA' ? "selected" : "") + '>Washington</option>' +
	'<option value="WV"  '+ (selected == 'WV' ? "selected" : "") + '>West Virginia</option>' +
	'<option value="WI"  '+ (selected == 'WI' ? "selected" : "") + '>Wisconsin</option>' +
	'<option value="WY"  '+ (selected == 'WY' ? "selected" : "") + '>Wyoming</option></select>';
};

// Modified by deepak, Nous 23-Jan-2007 - Added the disabled attribute
gpr.monthControl = function(id)
{
	var html = '<select id="' + id + '" disabled>' ;
	for( var i = 1; i < 13 ; ++i )
	{							
		html += '<option value="' + (i < 10 ? '0' + i : i) + '">' + (i < 10 ? '0' + i : i) + '</option>';
	}
	html += '</select>';

	return html;
	
};

gpr.sslPath = function() { return 'http://' + document.location.host + gpr.environment.virtualPath; }
gpr.htmlString = function(str)
{
	if ( ! str )
	{
		return '';
	}
	else
    {
		var output = [];
		for (var i = 0; i < str.length; ++i)
		{
			var c = str.charAt(i);
			switch (c)
			{
				case '<':
					output.push('&lt;');
					break;
				case '>':
					output.push('&gt;');
					break;
				case '&':
					output.push('&amp;');
					break;
				case '\"':
					output.push('&quot;');
					break;
				case '\'':
					output.push('&#39;');
					break;
				default:
					output.push(c);
					break;
			}
		}
		return output.join('');
	}
};

gpr.javaString = function(str)
{
	if ( ! str )
	{
		return '';
	}
	else
	{
		var output = [];
		for (var i = 0; i < str.length; ++i)
		{
			var code = str.charCodeAt(i);
			if ( code > 0x0fff )
			{
				output.push('\\u' + code.toString(16));
			}					
			else if ( code > 0x00ff )
			{
				output.push('\\u0' + code.toString(16));
			}
			else if ( code > 0x007f )
			{
				output.push('\\u00' + code.toString(16));
			}
			else
			{
				switch ( str.charAt(i) )
				{
					case '\\':
						output.push('\\\\');
						break;
					case '\"':
						output.push('\\\"');
						break;
					case '\r':
						output.push('\\r');
						break;
					case '\n':
						output.push('\\n');
						break;
					case '\'':
						output.push('\\\'');
						break;
					case '<':
						output.push('\\u003c');
						break;
					case '>':
						output.push('\\u003e');
						break;
					default:
						output.push(str.charAt(i));
						break;
				};
			}
		}
		return output.join('');
	}

};
gpr.isInteger = function(s)
{
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
};

gpr.isNullDate = function(date)
{
    return !date || Date.parse(date).valueOf() == gpr.environment.nullDate.valueOf();
};

gpr.find = function(arr, value)
{
	for( var i = 0; i < arr.length; ++i )
	{
		if ( arr[i] == value )
		{
			return arr[i];
		}
	}
    return null;
};

gpr.findID = function(arr, id, defaultItem)
{
	for( var i = 0; i < arr.length; ++i )
	{
		if ( arr[i].id == id )
		{
			return arr[i];
		}
	}
    return defaultItem;
};

gpr.findPatient = function(collection, id)
	{
		for(i in collection)
		{
			if(collection[i].id == id)
			{
				return collection[i]
			}
		}
		return {};
	}
gpr.findProviderByID = function(arr, id, defaultItem)
{
	for(i in arr )
	{
		if ( arr[i].provider.id == id )
		{
			return arr[i].provider.firstName + '  '  +  arr[i].provider.lastName;
		}
	}
    return defaultItem;
};
gpr.stripCharsInBag = function(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
};

gpr.daysInFebruary = function(year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
};

gpr.daysArray  = function (n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this;
};

gpr.isPhone = function(phone)
{
	  var objRegExp  = /^\d{3}\-\d{3}\-\d{4}$/;
	  return objRegExp.test(phone);
}

gpr.isDate = function (dtStr)
{
	var daysInMonth = gpr.daysArray(12);
	var pos1 = dtStr.indexOf(gpr.consts.dateSeparator);
	var pos2 = dtStr.indexOf(gpr.consts.dateSeparator, pos1+1);
	var strMonth = dtStr.substring(0,pos1);
	var strDay = dtStr.substring(pos1+1,pos2);
	var strYear = dtStr.substring(pos2+1);
	
		strYr=strYear;
		if (strDay.charAt(0) == "0" && strDay.length > 1) strDay=strDay.substring(1);
		if (strMonth.charAt(0)=="0" && strMonth.length >1) strMonth=strMonth.substring(1);
	
		for (var i = 1; i <= 3; i++) 
		{
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
		}
		
		month=parseInt(strMonth);
		day=parseInt(strDay);
		year=parseInt(strYr);
		
		if (pos1==-1 || pos2==-1)
		{
			alert("The date  should be in mm/dd/yyyy format");
			return false;
		}
		
		if (strMonth.length < 1 || month < 1 || month > 12){
			alert("Please enter a valid month");
			return false;
		}
		
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day > gpr.daysInFebruary(year)) || day > daysInMonth[month])
		{
			alert("Please enter a valid day");
			return false;
		}
		
		if (strYear.length != 4 || year==0 || year < gpr.consts.minYear || year > gpr.consts.maxYear)
		{
			alert("Please enter a valid 4 digit year between "+ gpr.consts.minYear + " and " + gpr.consts.maxYear);
			return false;
		}
		
		if (dtStr.indexOf(gpr.consts.dateSeparator, pos2+1)!=-1 || gpr.isInteger(gpr.stripCharsInBag(dtStr, gpr.consts.dateSeparator))==false)
		{
			alert("Please enter a valid date");
			return false;
		}
		
	return true;
};

gpr.pageQuery = function (q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() 
		{ return this.keyValuePairs; 
		};
	this.getValue = function(s)
	 {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return '';
	};
	
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	};
	
	this.getLength = function() { return this.keyValuePairs.length; };	
};

gpr.queryString = function(key,avoidUnEscape)
{
	var page = new gpr.pageQuery(window.location.search);
	if(avoidUnEscape)
	{ 
		var qrystring = location.search
		var keyValuePairs = new Array();

		if (qrystring.length > 0)
		{
			qrystring = qrystring.substring(1, qrystring.length)
		     keyValuePairs = qrystring.split("&")
		     for(i = 0; i < keyValuePairs.length ; i ++)
		  	{
		  		var currentKeyValue = keyValuePairs[i];
		  		if (currentKeyValue.split("=")[0] == key)
					{
						//return keyValuePairs[i].split("=")[1]
						return currentKeyValue.substring(currentKeyValue.indexOf("=")+1,currentKeyValue.length)
					}
			}     
		}
	    return ""
	}
	else
	{
		return unescape(page.getValue(key)); 
	}
	
};

gpr.formatCurrency = function(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
};

gpr.getRadioGroupValue = function(radioGroupID)
{
	var radioGroup = document.getElementsByName(radioGroupID);
	for (i = 0; i < radioGroup.length; ++i)
	{
		var radio = radioGroup[i];
		if ( radio.checked )
		{
			return radio.value;
		}
	}
};

gpr.getList = function (html, list, val, id)
{
		html.push(
		'<select id="' + id + '" >' +
			'<option value=""></option>'
	);

		for ( var a = 0; a < list.length; ++a)
		{
				html.push('<option value="' + list[a].id + '"');
				if ( list[a].id == val )
				{
					html.push(' selected ');

				}
				html.push('>');
				html.push(gpr.htmlString(list[a].name));
				html.push('</option>');
			
		}
	
	html.push('</select>');
};

gpr.getProvidersList = function(id, selectedID, onChange)
{
	var pp = gpr.data.getCurrentPatientCollection('patientProviders');
	onChange = onChange ? 'onchange="' + onChange +'"' : ''
	var html = '<select id="' + id + '" '+ onChange + '><option value=""></option>' ;
	for( var i = 0; i < pp.length; i++ )
	{		
		html += '<option value="' + pp[i].provider.id + '" ' + (pp[i].provider.id == selectedID ? 'selected' : '') + '>' +  pp[i].provider.firstName + ' ' + pp[i].provider.lastName  + '</option>';
	}
	html += '</select>';
	return html;
};

var formatPN = function()
{
	this.zChar = new Array(' ', '(', ')', '-', '.');
	this.maxphonelength = 13;
	this.phonevalue1;
	this.phonevalue2;
	this.cursorposition;

	this.parseForNumber1= function(object){
		this.phonevalue1 = this.parseChar(object.value, this.zChar);
	}
	this.parseForNumber2 = function(object){
		this.phonevalue2 = this.parseChar(object.value, this.zChar);
	}

	this.backspacerUP = function(object,e)  { 
		if(e){ 
			e = e 
		} else {
			e = window.event 
		} 
		if(e.which){ 
			var keycode = e.which 
		} else {
			var keycode = e.keyCode 
		}

		this.parseForNumber1(object)

		if(keycode > 48){
			this.validatePhone(object)
		}
	}

	this.backspacerDOWN = function(object,e)
	  { 
		if(e){ 
			e = e 
		} else {
			e = window.event 
		} 
		if(e.which){ 
			var keycode = e.which 
		} else {
			var keycode = e.keyCode 
		}
		this.parseForNumber2(object)
	} 

	this.getCursorPosition =  function(){
	    
		var t1 = this.phonevalue1;
		var t2 = this.phonevalue2;
		var bool = false
		for (i=0; i<t1.length; i++)
		{
    		if (t1.substring(i,1) != t2.substring(i,1)) {
    			if(!bool) {
    				this.cursorposition=i
    				bool=true
    			}
    		}
		}
	}

	this.validatePhone = function(object){
		var p = this.phonevalue1
		
		p = p.replace(/[^\d]*/gi,"")

		if (p.length < 3) {
			object.value=p
		} else if(p.length==3){
			pp=p;
			d4=p.indexOf('(')
			d5=p.indexOf(')')
			if(d4==-1){
				pp="("+pp;
			}
			if(d5==-1){
				pp=pp+")";
			}
			object.value = pp;
		} else if(p.length>3 && p.length < 7){
			p ="(" + p;	
			l30=p.length;
			p30=p.substring(0,4);
			p30=p30+")"

			p31=p.substring(4,l30);
			pp=p30+p31;

			object.value = pp;	
			
		} else if(p.length >= 7){
			p ="(" + p;	
			l30=p.length;
			p30=p.substring(0,4);
			p30=p30+")"
			
			p31=p.substring(4,l30);
			pp=p30+p31;
			
			l40 = pp.length;
			p40 = pp.substring(0,8);
			p40 = p40 + "-"
			
			p41 = pp.substring(8,l40);
			ppp = p40 + p41;
			
			object.value = ppp.substring(0, this.maxphonelength);
		}
		
		this.getCursorPosition()
		
		if(this.cursorposition >= 0){
			if (this.cursorposition == 0) {
				this.cursorposition = 2
			} else if (this.cursorposition <= 2) {
				this.cursorposition = this.cursorposition + 1
			} else if (this.cursorposition <= 5) {
				this.cursorposition = this.cursorposition + 2
			} else if (this.cursorposition == 6) {
				this.cursorposition = this.cursorposition + 2
			} else if (this.cursorposition == 7) {
				this.cursorposition = this.cursorposition + 4
				e1=object.value.indexOf(')')
				e2=object.value.indexOf('-')
				if (e1>-1 && e2>-1){
					if (e2-e1 == 4) {
						this.cursorposition = this.cursorposition - 1
					}
				}
			} else if (this.cursorposition < 11) {
				this.cursorposition = this.cursorposition + 3
			} else if (this.cursorposition == 11) {
				this.cursorposition = this.cursorposition + 1
			} else if (this.cursorposition >= 12) {
				this.cursorposition = this.cursorposition
			}

			var txtRange = object.createTextRange();
			txtRange.moveStart( "character", this.cursorposition);
			txtRange.moveEnd( "character", this.cursorposition - object.value.length);
			txtRange.select();
		}

	}

	this.parseChar = function(sStr, sChar)
	{
		if (sChar.length == null) 
		{
			this.zChar = new Array(sChar);
		}
		else this.zChar = sChar;
	    
		for (i=0; i < this.zChar.length; i++)
		{
			sNewStr = "";
	    
			var iStart = 0;
			var iEnd = sStr.indexOf(sChar[i]);
	    
			while (iEnd != -1)
			{
				sNewStr += sStr.substring(iStart, iEnd);
				iStart = iEnd + 1;
				iEnd = sStr.indexOf(sChar[i], iStart);
			}
			sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
	        
			sStr = sNewStr;
		}
	    
		return sNewStr;
	}
}

var pNformatString = 'onkeydown="javascript:validator.backspacerDOWN(this, event);" onkeyup="javascript:validator.backspacerUP(this, event);" '


/*
Change By : Manoj 
Changed on : 24-Sept-2007
Purpose  : Added two functions to validate  password;
*/
gpr.validatePassword = function(object,e)
	{
		var SpecialCharsAllowed = null;
		var strErrorMsg = null;
		var pwdChars =("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
		SpecialCharsAllowed = gpr.data.passwordSettings[1].validSpecialChars;
		
		if(SpecialCharsAllowed == '0')
		{
			SpecialCharsAllowed = "!@#$%^&*";
			strErrorMsg = "Allowed special characters are !@#$%^&*"
		}
		else if(SpecialCharsAllowed == '-1')
		{
			SpecialCharsAllowed = null;
			strErrorMsg = "Special characters are not allowed."
		}
		else
		{
			SpecialCharsAllowed = gpr.data.passwordSettings[1].validSpecialChars;
			strErrorMsg = "Allowed special characters are " + SpecialCharsAllowed;
		}
	
		
		var keycode
			var shift
			var kCode
			var validChar
			var actualkey
			var capsKeycode=0;
			var CapsShiftKey=false;
			var capsOn = false;
			validChar = 0
			
			if ( document.all ) 
			{
				capsKeycode=e.keyCode;
				CapsShiftKey=e.shiftKey;
			}
			
			if(e)
			{ 
				e = e 
			}
			else
			{
				e = window.event 
			} 
			if(e.which)
			{ 
				keycode = e.which 
			}
			else
			{
				keycode = e.keyCode 
			}				 
			
			if(SpecialCharsAllowed != null)
			{							
				for (i=0; i < SpecialCharsAllowed.length ;i++)
				{
					if(SpecialCharsAllowed.charAt(i) == String.fromCharCode(event.keyCode))
					{																		
  						validChar=1  								  									
  					}			
  				}  				
  			}
  			
  			if(pwdChars != null)
			{							
				for (i=0; i < pwdChars.length ;i++)
				{
					if(pwdChars.charAt(i) == String.fromCharCode(event.keyCode))
					{																		
  						validChar=1  								  									
  					}			
  				}  				
  			}
  			if(validChar == 0)
  			{
  				alert(strErrorMsg);
  				event.cancelBubble=true;
				event.returnValue=false;
  			}
	}
	
	gpr.onKeyDown = function()
	{
		if(event.ctrlKey)
		{
			event.cancelBubble=true;
			event.returnValue=false;
		}
	}
