var currentUrl; function url(page) { this.currentUrl = page; this.setParameter = setParameter; this.setParameterAccepted = setParameterAccepted; this.addParameter = addParameter; this.addParameterAccepted = addParameterAccepted; this.getParameter = getParameter; this.removeParameter = removeParameter; this.setPage = setPage; this.getUrl = getUrl; } function setParameter(key, value) { this.setParameterAccepted(key, value, false); } function setParameterAccepted(key, value, acceptEmpty) { if (!acceptEmpty && (value == null || value.length == 0)) { this.removeParameter(key); return; } key = encodeURI(key); value = encodeURIComponent(value).replace("\"","\\\"").replace("'","\\'"); this.currentUrl = this.currentUrl.split('#')[0]; var qs = this.currentUrl.split('?'); if (qs.length <= 1){ this.currentUrl = this.currentUrl + "?" + key + "=" + value; return; }; var kvp = qs[1].split('&'); var i=kvp.length; var x; while(i--) { x = kvp[i].split('='); if (x[0]==key) { x[1] = value; kvp[i] = x.join('='); break; } } if(i<0) {kvp[kvp.length] = [key,value].join('=');} this.currentUrl = qs[0] + "?" + kvp.join('&'); } function addParameter(key, value) { this.addParameterAccepted(key, value, false); } function addParameterAccepted(key, value, acceptEmpty) { if (!acceptEmpty && (value == null || value.length == 0)) { this.removeParameter(key); return; } key = encodeURI(key); value = encodeURIComponent(value).replace("\"","\\\"").replace("'","\\'"); this.currentUrl = this.currentUrl.split('#')[0]; var qs = this.currentUrl.split('?'); if (qs.length <= 1){ this.currentUrl = this.currentUrl + "?" + key + "=" + value; return; }; var kvp = qs[1].split('&'); kvp.push(key+'='+value); this.currentUrl = qs[0] + "?" + kvp.join('&'); } function getParameter(key){ var regexS = "[\\?&]"+key+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( this.currentUrl ); if(results == null) return ""; else return decodeURI(results[1]); } function removeParameter(key){ var regex = new RegExp( "&"+key+"=[^&#]*" ); this.currentUrl = this.currentUrl.replace(regex, ""); regex = new RegExp( "\\?"+key+"=[^&#]*[&#]" ); this.currentUrl = this.currentUrl.replace(regex, "?"); } function setPage(page){ var reg = new RegExp("^.*\\?", "i"); if (this.currentUrl.match(reg)) this.currentUrl = this.currentUrl.replace(reg, page + "?"); else { this.currentUrl = page; } } function getUrl(){ return decodeURI(this.currentUrl); } function validateDateFormat(dateString) { if(dateString==null) return null; if(dateString.length!=10) return null; var tab = dateString.split("-"); if(tab==null) return null; if(tab.length!=3) return null; //validate days var days = tab[0]; if(days==null || days.length!=2 || !isIntValue(days)) return null; //validate month var months = tab[1]; if(months==null ||months.length!=2 || !isIntValue(months)) return null; //validate year var years = tab[2]; if(years==null ||years.length!=4 || !isIntValue(years)) return null; return isValidDate(years, months, days); } function isIntValue(value) { while(value.indexOf("0")==0) { value = value.substring(1,value.length); } if(!parseInt(value)) return false; return true; } //Receives a string dd-MM-yyyy and returns the Date object function getDate(dateStr) { if (dateStr.length != 10) return null; var day = dateStr.substring(0,2); var month = dateStr.substring(3,5); var year = dateStr.substring(6,10); var date = new Date(); date.setFullYear(year,(month-1),day); return date; } function formatDate(date){ var day = date.getDate()+""; var month = date.getMonth()+1+""; var year = date.getFullYear(); while (day.length < 2) day = "0" + day; while (month.length < 2) month = "0" + month; return (day + "-" + month + "-" + year); } function formatDateParam(date){ var day = date.getDate()+""; var month = date.getMonth()+1+""; var year = date.getFullYear(); while (day.length < 2) day = "0" + day; while (month.length < 2) month = "0" + month; return (year + month + day); }