PageSourceSearch

https://qe.com.qa/o/mainsitetheme/js/UtilityFunctions.js

js qe.com.qa collected 2026-10-02 10:15:21 UTC 17,291 bytes, 480 lines download raw bytes

1// Description: This is a Utility Functions JS File
2// Purpose: This file will have all the common functions that will be used across the entire portal
3// Created Date: 10th November, 2009
4// Created By: Techlogix Pvt Ltd -- Farah Noman
5
6
7function changeLanguage()
8{
9	 var currentWindowURL=window.location.href;
10	 var replacedURL=currentWindowURL.replace("english", "arabic");
11	 var replacedURLFormatted=replacedURL.replace("English", "arabic");	 	 
12	 window.location.href=replacedURLFormatted;
13	 var searchpage = "SearchPage";
14	 var currentURL = currentWindowURL.split('%20').join('');
15	 var currentURL1 = currentURL.split(' ').join('');
16	 var currentURLupper = currentURL1.toUpperCase();
17	 if (currentURLupper.indexOf(searchpage.toUpperCase()) != -1)
18	 { 
19	 	var position=replacedURLFormatted.indexOf("?");
20	 	if (position != -1)
21	 	{
22	 		window.location.href = replacedURLFormatted.substr(0,position);
23	 	}
24	 	else 
25	 	{
26	 		window.location.href = replacedURLFormatted;
27		}			
28	 }
29}
30
31
32
33/*
34* Shows any ID that is currently hidden
35*/
36function toggle(showHideDiv) 
37{
38	   var ele = document.getElementById(showHideDiv);
39      ele.style.display = "block";
40}
41/*
42* Hides any ID that is currently Visible
43*/
44function hide(showHideDiv)
45{
46	var ele = document.getElementById(showHideDiv);
47	ele.style.display = "none";
48}
49
50// Calendar Javascript Code
51
52// default settins
53var A_TCALDEF = {
54	'months' : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
55	'weekdays' : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
56	'yearscroll': true, // show year scroller
57	'weekstart': 0, // first day of week: 0-Su or 1-Mo
58	'centyear'  : 70, // 2 digit years less than 'centyear' are in 20xx, othewise in 19xx.
59	'imgpath' : '/pps/PortalTemplates/CalendarImages/' // directory with calendar images
60}
61// date parsing function
62function f_tcalParseDate (s_date) {
63
64	var re_date = /^\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*$/;
65	if (!re_date.exec(s_date))
66		return alert ("Invalid date: '" + s_date + "'.\nAccepted format is dd/mm/yyyy.")
67	var n_day = Number(RegExp.$1),
68		n_month = Number(RegExp.$2),
69		n_year = Number(RegExp.$3);
70	
71	if (n_year < 100)
72		n_year += (n_year < this.a_tpl.centyear ? 2000 : 1900);
73	if (n_month < 1 || n_month > 12)
74		return alert ("Invalid month value: '" + n_month + "'.\nAllowed range is 01-12.");
75	var d_numdays = new Date(n_year, n_month, 0);
76	if (n_day > d_numdays.getDate())
77		return alert("Invalid day of month value: '" + n_day + "'.\nAllowed range for selected month is 01 - " + d_numdays.getDate() + ".");
78
79	return new Date (n_year, n_month - 1, n_day);
80}
81// date generating function
82
83function f_tcalGenerDate (d_date) {
84	return (
85		(d_date.getDate() < 10 ? '0' : '') + d_date.getDate() + "/"
86		+ (d_date.getMonth() < 9 ? '0' : '') + (d_date.getMonth() + 1) + "/"
87		+ d_date.getFullYear()
88	);
89}
90
91// implementation
92function tcal (a_cfg, a_tpl) {
93
94	// apply default template if not specified
95	if (!a_tpl)
96		a_tpl = A_TCALDEF;
97
98	// register in global collections
99	if (!window.A_TCALS)
100		window.A_TCALS = [];
101	if (!window.A_TCALSIDX)
102		window.A_TCALSIDX = [];
103	
104	this.s_id = a_cfg.id ? a_cfg.id : A_TCALS.length;
105	window.A_TCALS[this.s_id] = this;
106	window.A_TCALSIDX[window.A_TCALSIDX.length] = this;
107	
108	// assign methods
109	this.f_show = f_tcalShow;
110	this.f_hide = f_tcalHide;
111	this.f_toggle = f_tcalToggle;
112	this.f_update = f_tcalUpdate;
113	this.f_relDate = f_tcalRelDate;
114	this.f_parseDate = f_tcalParseDate;
115	this.f_generDate = f_tcalGenerDate;
116	
117	// create calendar icon
118	this.s_iconId = 'tcalico_' + this.s_id;
119	this.e_icon = f_getElement(this.s_iconId);
120	if (!this.e_icon) {
121		document.write('<img src="' + a_tpl.imgpath + 'icon_calendar.gif" id="' + this.s_iconId + '" onclick="A_TCALS[\'' + this.s_id + '\'].f_toggle()" class="tcalIcon" alt="Open Calendar" width="26" height="20" />');
122		this.e_icon = f_getElement(this.s_iconId);
123	}
124	// save received parameters
125	this.a_cfg = a_cfg;
126	this.a_tpl = a_tpl;
127}
128
129function f_tcalShow (d_date) {
130
131	// find input field
132	if (!this.a_cfg.controlname)
133		throw("TC: control name is not specified");
134	if (this.a_cfg.formname) {
135		var e_form = document.forms[this.a_cfg.formname];
136		if (!e_form)
137			throw("TC: form '" + this.a_cfg.formname + "' can not be found");
138		this.e_input = e_form.elements[this.a_cfg.controlname];
139	}
140	else
141		this.e_input = f_getElement(this.a_cfg.controlname);
142
143	if (!this.e_input || !this.e_input.tagName || this.e_input.tagName != 'INPUT')
144		throw("TC: element '" + this.a_cfg.controlname + "' does not exist in "
145			+ (this.a_cfg.formname ? "form '" + this.a_cfg.controlname + "'" : 'this document'));
146
147	// dynamically create HTML elements if needed
148	this.e_div = f_getElement('tcal');
149	if (!this.e_div) {
150		this.e_div = document.createElement("DIV");
151		this.e_div.id = 'tcal';
152		document.body.appendChild(this.e_div);
153	}
154	this.e_shade = f_getElement('tcalShade');
155	if (!this.e_shade) {
156		this.e_shade = document.createElement("DIV");
157		this.e_shade.id = 'tcalShade';
158		document.body.appendChild(this.e_shade);
159	}
160	this.e_iframe =  f_getElement('tcalIF')
161	if (b_ieFix && !this.e_iframe) {
162		this.e_iframe = document.createElement("IFRAME");
163		this.e_iframe.style.filter = 'alpha(opacity=0)';
164		this.e_iframe.id = 'tcalIF';
165		this.e_iframe.src = this.a_tpl.imgpath + 'pixel.gif';
166		document.body.appendChild(this.e_iframe);
167	}
168	
169	// hide all calendars
170	f_tcalHideAll();
171
172	// generate HTML and show calendar
173	this.e_icon = f_getElement(this.s_iconId);
174	if (!this.f_update())
175		return;
176
177	this.e_div.style.visibility = 'visible';
178	this.e_shade.style.visibility = 'visible';
179	if (this.e_iframe)
180		this.e_iframe.style.visibility = 'visible';
181
182	// change icon and status
183	//this.e_icon.src = this.a_tpl.imgpath + 'no_cal.gif';
184	this.e_icon.title = 'Close Calendar';
185	this.b_visible = true;
186}
187
188function f_tcalHide (n_date) {
189	if (n_date)
190		this.e_input.value = this.f_generDate(new Date(n_date));
191
192	// no action if not visible
193	if (!this.b_visible)
194		return;
195
196	// hide elements
197	if (this.e_iframe)
198		this.e_iframe.style.visibility = 'hidden';
199	if (this.e_shade)
200		this.e_shade.style.visibility = 'hidden';
201	this.e_div.style.visibility = 'hidden';
202	
203	// change icon and status
204	this.e_icon = f_getElement(this.s_iconId);
205	this.e_icon.src = this.a_tpl.imgpath + 'icon_calendar.gif';
206	this.e_icon.title = 'Open Calendar';
207	this.b_visible = false;
208}
209
210function f_tcalToggle () {
211	return this.b_visible ? this.f_hide() : this.f_show();
212}
213
214function f_tcalUpdate (d_date) {
215	
216	var d_today = this.a_cfg.today ? this.f_parseDate(this.a_cfg.today) : f_tcalResetTime(new Date());
217	var d_selected = this.e_input.value == ''
218		? (this.a_cfg.selected ? this.f_parseDate(this.a_cfg.selected) : d_today)
219		: this.f_parseDate(this.e_input.value);
220
221	// figure out date to display
222	if (!d_date)
223		// selected by default
224		d_date = d_selected;
225	else if (typeof(d_date) == 'number')
226		// get from number
227		d_date = f_tcalResetTime(new Date(d_date));
228	else if (typeof(d_date) == 'string')
229		// parse from string
230		this.f_parseDate(d_date);
231		
232	if (!d_date) return false;
233
234	// first date to display
235	var d_firstday = new Date(d_date);
236	d_firstday.setDate(1);
237	d_firstday.setDate(1 - (7 + d_firstday.getDay() - this.a_tpl.weekstart) % 7);
238	
239	var a_class, s_html = '<table class="ctrl"><tbody><tr>'
240		+ (this.a_tpl.yearscroll ? '<td' + this.f_relDate(d_date, -1, 'y') + ' title="Previous Year"><img src="' + this.a_tpl.imgpath + 'prev_year.gif" /></td>' : '')
241		+ '<td' + this.f_relDate(d_date, -1) + ' title="Previous Month"><img src="' + this.a_tpl.imgpath + 'prev_mon.gif" /></td><th>'
242		+ this.a_tpl.months[d_date.getMonth()] + ' ' + d_date.getFullYear()
243			+ '</th><td' + this.f_relDate(d_date, 1) + ' title="Next Month"><img src="' + this.a_tpl.imgpath + 'next_mon.gif" /></td>'
244		+ (this.a_tpl.yearscroll ? '<td' + this.f_relDate(d_date, 1, 'y') + ' title="Next Year"><img src="' + this.a_tpl.imgpath + 'next_year.gif" /></td></td>' : '')
245		+ '</tr></tbody></table><table><tbody><tr class="wd">';
246
247	// print weekdays titles
248	for (var i = 0; i < 7; i++)
249		s_html += '<th>' + this.a_tpl.weekdays[(this.a_tpl.weekstart + i) % 7] + '</th>';
250	s_html += '</tr>' ;
251
252	// print calendar table
253	var n_date, n_month, d_current = new Date(d_firstday);
254	while (d_current.getMonth() == d_date.getMonth() ||
255		d_current.getMonth() == d_firstday.getMonth()) {
256	
257		// print row heder
258		s_html +='<tr>';
259		for (var n_wday = 0; n_wday < 7; n_wday++) {
260
261			a_class = [];
262			n_date  = d_current.getDate();
263			n_month = d_current.getMonth();
264
265			// other month
266			if (d_current.getMonth() != d_date.getMonth())
267				a_class[a_class.length] = 'othermonth';
268			// weekend
269			if (d_current.getDay() == 5 || d_current.getDay() == 6)
270				a_class[a_class.length] = 'weekend';
271			// today
272			if (d_current.valueOf() == d_today.valueOf())
273				a_class[a_class.length] = 'today';
274			// selected
275			if (d_current.valueOf() == d_selected.valueOf())
276				a_class[a_class.length] = 'selected';
277
278			s_html += '<td onclick="A_TCALS[\'' + this.s_id + '\'].f_hide(' + d_current.valueOf() + ')"' + (a_class.length ? ' class="' + a_class.join(' ') + '">' : '>') + n_date + '</td>'
279
280			d_current.setDate(++n_date);
281			while (d_current.getDate() != n_date && d_current.getMonth() == n_month) {
282				d_current.setHours(d_current.getHours + 1);
283				d_current = f_tcalResetTime(d_current);
284			}
285		}
286		// print row footer
287		s_html +='</tr>';
288	}
289	s_html +='</tbody></table>';
290	
291	// update HTML, positions and sizes
292	this.e_div.innerHTML = s_html;
293
294	var n_width  = this.e_div.offsetWidth;
295	var n_height = this.e_div.offsetHeight;
296	var n_top  = f_getPosition (this.e_icon, 'Top') + this.e_icon.offsetHeight;
297	var n_left = f_getPosition (this.e_icon, 'Left') - n_width + this.e_icon.offsetWidth;
298	if (n_left < 0) n_left = 0;
299	
300	this.e_div.style.left = n_left + 'px';
301	this.e_div.style.top  = n_top + 'px';
302
303	this.e_shade.style.width = (n_width + 8) + 'px';
304	this.e_shade.style.left = (n_left - 1) + 'px';
305	this.e_shade.style.top = (n_top - 1) + 'px';
306	this.e_shade.innerHTML = b_ieFix
307		? '<table><tbody><tr><td rowspan="2" colspan="2" width="6"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td width="7" height="7" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_tr.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td height="' + (n_height - 7) + '" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_mr.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td width="7" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_bl.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_bm.png\', sizingMethod=\'scale\');" height="7" align="left"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_br.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tbody></table>'
308		: '<table><tbody><tr><td rowspan="2" width="6"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td rowspan="2"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td width="7" height="7"><img src="' + this.a_tpl.imgpath + 'shade_tr.png"></td></tr><tr><td background="' + this.a_tpl.imgpath + 'shade_mr.png" height="' + (n_height - 7) + '"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td><img src="' + this.a_tpl.imgpath + 'shade_bl.png"></td><td background="' + this.a_tpl.imgpath + 'shade_bm.png" height="7" align="left"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td><img src="' + this.a_tpl.imgpath + 'shade_br.png"></td></tr><tbody></table>';
309	
310	if (this.e_iframe) {
311		this.e_iframe.style.left = n_left + 'px';
312		this.e_iframe.style.top  = n_top + 'px';
313		this.e_iframe.style.width = (n_width + 6) + 'px';
314		this.e_iframe.style.height = (n_height + 6) +'px';
315	}
316	return true;
317}
318
319function f_getPosition (e_elemRef, s_coord) {
320	var n_pos = 0, n_offset,
321		e_elem = e_elemRef;
322
323	while (e_elem) {
324		n_offset = e_elem["offset" + s_coord];
325		n_pos += n_offset;
326		e_elem = e_elem.offsetParent;
327	}
328	// margin correction in some browsers
329	if (b_ieMac)
330		n_pos += parseInt(document.body[s_coord.toLowerCase() + 'Margin']);
331	else if (b_safari)
332		n_pos -= n_offset;
333	
334	e_elem = e_elemRef;
335	while (e_elem != document.body) {
336		n_offset = e_elem["scroll" + s_coord];
337		if (n_offset && e_elem.style.overflow == 'scroll')
338			n_pos -= n_offset;
339		e_elem = e_elem.parentNode;
340	}
341	return n_pos;
342}
343
344function f_tcalRelDate (d_date, d_diff, s_units) {
345	var s_units = (s_units == 'y' ? 'FullYear' : 'Month');
346	var d_result = new Date(d_date);
347	d_result['set' + s_units](d_date['get' + s_units]() + d_diff);
348	if (d_result.getDate() != d_date.getDate())
349		d_result.setDate(0);
350	return ' onclick="A_TCALS[\'' + this.s_id + '\'].f_update(' + d_result.valueOf() + ')"';
351}
352
353function f_tcalHideAll () {
354	if(typeof window.A_TCALSIDX != "undefined")
355	for (var i = 0; i < window.A_TCALSIDX.length; i++)
356		window.A_TCALSIDX[i].f_hide();
357}
358
359function f_tcalResetTime (d_date) {
360	d_date.setHours(0);
361	d_date.setMinutes(0);
362	d_date.setSeconds(0);
363	d_date.setMilliseconds(0);
364	return d_date;
365}
366
367f_getElement = document.all ?
368	function (s_id) { return document.all[s_id] } :
369	function (s_id) { return document.getElementById(s_id) };
370
371if (document.addEventListener)
372	window.addEventListener('scroll', f_tcalHideAll, false);
373if (window.attachEvent)
374	window.attachEvent('onscroll', f_tcalHideAll);
375	
376// global variables
377var s_userAgent = navigator.userAgent.toLowerCase(),
378	re_webkit = /WebKit\/(\d+)/i;
379var b_mac = s_userAgent.indexOf('mac') != -1,
380	b_ie5 = s_userAgent.indexOf('msie 5') != -1,
381	b_ie6 = s_userAgent.indexOf('msie 6') != -1 && s_userAgent.indexOf('opera') == -1;
382var b_ieFix = b_ie5 || b_ie6,
383	b_ieMac  = b_mac && b_ie5,
384	b_safari = b_mac && re_webkit.exec(s_userAgent) && Number(RegExp.$1) < 500;
385	
386
387	//---------------------  DATE VALIDATION -------------------//
388var dtCh= "/";
389var minYear = 1000;
390var maxYear = 9999;
391
392function isInteger(s){
393	var i;
394    for (i = 0; i < s.length; i++){   
395        // Check that current character is number.
396        var c = s.charAt(i);
397        if (((c < "0") || (c > "9"))) return false;
398    }
399    // All characters are numbers.
400    return true;
401}
402
403function stripCharsInBag(s, bag){
404	var i;
405    var returnString = "";
406    // Search through string's characters one by one.
407    // If character is not in bag, append to returnString.
408    for (i = 0; i < s.length; i++){   
409        var c = s.charAt(i);
410        if (bag.indexOf(c) == -1) returnString += c;
411    }
412    return returnString;
413}
414
415function daysInFebruary (year){
416	// February has 29 days in any year evenly divisible by four,
417    // EXCEPT for centurial years which are not also divisible by 400.
418    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
419}
420function DaysArray(n) {
421	var array = [12];
422	for (var i = 1; i <= n; i++) {
423		array[i] = 31
424		if (i==4 || i==6 || i==9 || i==11) {array[i] = 30}
425		if (i==2) {array[i] = 29}
426   } 
427   return array;
428}
429
430
431function isDate(dtStr){
432	var daysInMonth_UtilityFun = DaysArray(12)
433	var pos1=dtStr.indexOf(dtCh)
434	var pos2=dtStr.indexOf(dtCh,pos1+1)
435	var strDay=dtStr.substring(0,pos1)
436	var strMonth=dtStr.substring(pos1+1,pos2)
437	var strYear=dtStr.substring(pos2+1)
438	strYr=strYear
439	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
440	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
441	for (var i = 1; i <= 3; i++) {
442		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
443	}
444	month=parseInt(strMonth)
445	day=parseInt(strDay)
446	year=parseInt(strYr)
447	if (pos1==-1 || pos2==-1) //The date format should be : dd/mm/yyyy
448	{
449		return false
450	}
451	if (strMonth.length<1 || month<1 || month>12)  //Please enter a valid month
452	{
453		return false
454	}
455	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth_UtilityFun[month])  //Please enter a valid day
456	{
457		return false
458	}
459	if (strYr.length != 4 || year==0 || strYr<minYear || strYr>maxYear) //Please enter a valid 4 digit year
460	{
461		return false
462	}
463	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)  //Please enter a valid date
464	{
465		return false
466	}
467return true
468}
469function ValidateDate(txtDate)
470{
471	var dt=document.getElementById(txtDate);
472	if (isDate(dt.value)==false)
473	{
474		dt.focus()
475		return false
476	}
477    return true
478 }
479
480	//---------------------  DATE VALIDATION -------------------//

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.