// JavaScript Document

function setgencats(catno,catval,stype) { //shopcat = the parentid of next set; ; curshpcat= level in hierachy
	catnames = catval.split(' > ');
	catids = catno.split('-');
	cat = catids.length;
	switch (stype) {
	case 1:	dir = "/shopping"; break;
	case 2:	dir = "/auctions"; break;
	case 3:	dir = "/charities"; break;
	default: dir = "";
	}
	setCookie("curshpcat", cat, 0, "/", ".itestify.reachoutnigeria.org", 0);
	setCookie("shopcat", catno, 0, "/", ".itestify.reachoutnigeria.org", 0);
	setCookie('shopshelf', catval, 0, "/", '.itestify.reachoutnigeria.org', 0);
	setCookie('shelfid', catno, 0, "/", '.itestify.reachoutnigeria.org', 0);
	while (cat>0) {
		catname = "catshp" + cat;
		catcookie = catids[cat-1] + "###" + catnames[cat-1] + "%%%";
		setCookie(catname, catcookie, 0, "/", ".itestify.reachoutnigeria.org", 0);
		cat--;
	}
	parent.location.href = dir+"/index.php";
}

function timepast(dt1,dt2,sh) {
	lastactive = "";
	dtstr = dt1.replace(/\D/g,",");
	dt = dtstr.split(',');
	actd1 = new Date(dt[0],dt[1],dt[2],dt[3],dt[4],dt[5]);
	dtstr = dt2.replace(/\D/g,",");
	ds = dtstr.split(',');
	actd2 = new Date(ds[0],ds[1],ds[2],ds[3],ds[4],ds[5]);
	sec1 = parseInt(actd1.valueOf() / (1000), 10);
	sec2 = parseInt(actd2.valueOf() / (1000), 10);
	timegap = (sec1 - sec2);
	daygap  = (sec1 - sec2) / (60 * 60 * 24);
	daygap  = parseInt(daygap, 10);
	weekgap = (sec1 - sec2) / (60 * 60 * 24 * 7);
	weekgap  = parseInt(weekgap, 10);
	mthgap  = (sec1 - sec2) / (60 * 60 * 24 * 30);
	mthgap  = parseInt(mthgap, 10);
	yeargap = (sec1 - sec2) / (60 * 60 * 24 * 365);
	yeargap  = parseInt(yeargap, 10);
	if (timegap<1440) {
		if (timegap<2) {
			lastactive = timegap + " second ago";
			} else if (timegap<60) {
				lastactive = timegap + " seconds ago";
				} else if (timegap<120) {
							lastactive = "1 minute ago";
					} else if (timegap<3600) {
								lastactive = parseInt(timegap/60) + " minutes ago";
						} else if (timegap<7200) {
							lastactive = "1 hour " + parseInt(timegap/60) +  " minutes ago";
							} else {
								lastactive = parseInt(timegap/(60 * 60)) + " hours " + parseInt(timegap/60) +  " minutes ago";
		} 
	} else if (daygap<2) {
			lastactive = "Yesterday";
		  } else if (daygap<7) {
				lastactive = daygap + " Days ago";
			} else if (daygap<14) {
					lastactive = "Over a Week ago";
			} else if (daygap<30) {
					lastactive = weekgap + " Weeks ago";
				} else if (daygap<31) {
						lastactive = "Over a Month ago";
				} else if (daygap<365) {
						lastactive = mthgap + " Months ago";
					} else if (daygap<730) {
							lastactive = "Over a Year ago";
					} else if (daygap>730) {
							lastactive = yeargap + " Years ago";
							}
	if (sh==true) {
		lastactive= lastactive + ' '+ds[3]+':'+ds[4]+':'+ds[5]+' '+ds[2]+'/'+ds[1]+'/'+ds[0]+'';}
	return lastactive
}

function hms(secs,t){
time=[0,0,secs];
for(var i=2;i>2-t;i--){
time[i-1]=Math.floor(time[i]/60);
time[i]=time[i]%/**/60;
if(time[i]<10)
time[i]='0'+time[i];
if ((t==1) && (time[i-1]==0))
break;
}
if (t==2)
return time.join(':')
else
return time[1]+':'+time[2];
}

function getUrlVars(){    
	var vars = [], hash;    
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');     
	for(var i = 0; i < hashes.length; i++)    {        
		hash = hashes[i].split('=');        
		vars.push(hash[0]);        
		vars[hash[0]] = hash[1];    
	}     
	return vars;
}

function setCookie(name, value, expdate, path, domain, secure) {
	var exp = new Date( );
	var expiretime = exp.getTime( ) + (expdate * 60 * 1000);
	exp.setTime(expiretime);
	var curCookie = name + "=" + escape(value) +
	((expdate==0) ? "" : "; expires=" + exp.toUTCString( )) +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure==1) ? "; secure" : "");
document.cookie = curCookie;
}

function getCookie(Name) { 
var search = Name + "=" 
if (document.cookie.length > 0) { // if there are any cookies  
 offset = document.cookie.indexOf(search) 
 if (offset != -1) { // if cookie exists  
offset += search.length  // set index of beginning of value 
end = document.cookie.indexOf(";", offset)  // set index of end of cookie value 
if (end == -1) 
 end = document.cookie.length 
 return unescape(document.cookie.substring(offset, end))  
 }}
 }

function deleteCookie(name, path, domain, secure) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1000);
	setCookie(name, '', exp, path, domain, secure);
}
//
function fixchar(obj) {
	var parv=/[^A-Za-z0-9_\@\ \.\-]/gi;
	var otr=obj;
	var str=obj.value;
	if (str.search(parv)>-1) {
	//otr.focus();
	alert('Do not use special characters like \(,\)\+,\\,\#,\$,\%,\^,\&,\*,\~,\!,\|,\",\',\:,\;,\<,\>,.,, in your seacrch.');
	}
}
//
function remcom(badstr) {
	newstr=badstr.replace(/[^A-Za-z0-9_\@\ \.\-]/gi,' '); 
	return newstr;
}
//
function setnum(txtin) {
	var txt=txtin.replace(/[^\.\d]/g,"");
	return txt;
}
//
function trimme(txtin) {
	var txt=txtin.replace(/^\s+|/,''); 
	var txt=txt.replace(/\s+$/,''); 
	return txt;
}
//
//
function setthous(txt) {
	i=txt.length;
	txtview="";
	x=0;
	parv=/\./g;
	if (txt.search(parv)>-1) {
		i=txt.search(parv);
		txtview=txt.substr(txt.search(parv));
	}
	while (i>0){
		x++;
		if (((x%3)==1)&&(x>1)) {txtview=","+txtview;}
		txtview=txt.charAt(i-1)+txtview;
		i--;
	}	
	return txtview;
}
//
function format_Num(objview,obj) {
	txt=setnum(objview.value);
	txtview=setthous(txt);
	obj.value=txt;
	objview.value=txtview;
}

function colchkval(chk,chkval) {
	txtval='';
	for (var i=0; i<chk.length; i++) {
		txtval +=(chk[i].checked==true)? '^'+(i+1)+'$|':'';
		}	
	chkval.value = (txtval.length==0)? '' : txtval.substr(0,txtval.length-1);
}

function colval(obj,objval) {
	txtval='';
	for (var i=0; i<obj.length; i++) {
		txtval +=(obj[i].value!='')? obj[i].value+'|':'';
		}	
	objval.value = (txtval.length==0)? '' : txtval.substr(0,txtval.length-1);
}

function fixNum(obj) {
	var parv=/[^0-9]/g;
	var otr=obj;
	var str=obj.value;
	if (str.search(parv)>-1) {
	//otr.focus();
	alert('Only Numbers are allowed here.');
	}
}

function formatdate(dbdate) {
	xdate = '';
	if (dbdate.length>0) {
	var splitdate = dbdate.split('-');
	xdate = splitdate[2] + '/' + splitdate[1] + '/' + splitdate[0]}
	return xdate;
}

function formatdatetime(dbdate) {
	xdate = '';
	var dt = dbdate.split(" ");
	return dt[1] + ' ' + formatdate(dt[0]);
}

function formatday(actdate) {
	retdate = "";
	refdate = actdate.replace(/\D/g,",");
	rdt = refdate.split(",");
	activsec = Date.UTC(rdt[0],rdt[1],rdt[2],rdt[3],rdt[4],rdt[5],0);
	newdate  = new Date(activsec);

	switch (newdate.getDay()) {
	case 0:	weekday = "Sun"; break;
	case 1:	weekday = "Mon"; break;
	case 2:	weekday = "Tue"; break;
	case 3:	weekday = "Wed"; break;
	case 4:	weekday = "Thu"; break;
	case 5:	weekday = "Fri"; break;
	case 6:	weekday = "Sat"; break;
	default: weekday = "";
	}
	switch (newdate.getMonth()) {
	case 0:	month = "Jan"; break;
	case 1:	month = "Feb"; break;
	case 2:	month = "Mar"; break;
	case 3:	month = "Apr"; break;
	case 4:	month = "May"; break;
	case 5:	month = "Jun"; break;
	case 6:	month = "Jul"; break;
	case 7:	month = "Aug"; break;
	case 8:	month = "Sep"; break;
	case 9:	month = "Oct"; break;
	case 10: month = "Nov"; break;
	case 11: month = "Dec"; break;
	default: month = "";
	}
	
	retdate = weekday + " " + newdate.getDate() + " " + month + " " + newdate.getYear() + " " + newdate.getHours() + ":" + newdate.getMinutes() + ":" + newdate.getSeconds();
return retdate;
}

function setcolor(color) {
	switch (color) {
	case 'Beige': clrval = "#F5F5DC"; break;
	case 'Black': clrval = "#000000"; break;
	case 'Blue': clrval = "#0000FF"; break;
	case 'Brown': clrval = "#A52A2A"; break;
	case 'Burgundy': clrval = "#"; break;
	case 'Champagne': clrval = "#"; break;
	case 'Charcoal': clrval = "#"; break;
	case 'Cream': clrval = "#"; break;
	case 'Gold': clrval = "#FFD700"; break;
	case 'Gray': clrval = "#808080"; break;
	case 'Green': clrval = "#008000"; break;
	case 'Maroon': clrval = "#800000"; break;
	case 'Off White': clrval = "#"; break;
	case 'Orange': clrval = "#FFA500"; break;
	case 'Pewter': clrval = "#"; break;
	case 'Pink': clrval = "#FFC0CB"; break;
	case 'Purple': clrval = "#800080"; break;
	case 'Red': clrval = "#FF0000"; break;
	case 'Silver': clrval = "#C0C0C0"; break;
	case 'Tan': clrval = "#D2B48C"; break;
	case 'Teal': clrval = "#008080"; break;
	case 'Titanium': clrval = "#"; break;
	case 'Turquoise': clrval = "#40E0D0"; break;
	case 'White': clrval = "#FFFFFF"; break;
	case 'Yellow': clrval = "#FFFF00"; break;
	case 'Other': clrval = "#"; break;
	default: clrval = "";
	}
return clrval;
}

function optval(opt) {
	optsel=opt.options[opt.selectedIndex].value;
	return optsel;
}

function chkuplow(txt1,txt2,val) {
	if ((parseInt(txt1.value)>parseInt(txt2.value))&&(txt1.value!='')&&(txt2.value!='')) {
		alert('Upper limit should be higher or equal to lower limit for '+val); 
		txt1.focus(); 
		return false;
		} else {
		return true;}
}

function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

function setposval(pos,len) {
retval = '';
for (var i=1; i<=len; i++) {
if (pos==i) 
{retval += '1-';}else{retval += '_-';}
}
retval = "'" + retval.slice(0,9) + "'";
return retval;
}

function hidshow(divtag){
thistag=document.getElementById(divtag);
if (thistag.style.display=='none'){
	thistag.style.display='block'}
else
	thistag.style.display='none';
}

function genblank(val) {
	retval = val.replace(/0/g,"_");
 return retval;
}
function tickinit() {
if (ie) {
if (l1 == 0 && t1 == 0) {
pos = document.all['tickpos'];
l1 = getLeft(pos);
t1 = getTop(pos);
}
ticktext.style.posTop = t1;
}
else {
if (l1 == 0 && t1 == 0) {
pos = document.anchors['tickpos'];
l1 = pos.x;
t1 = pos.y;
}
document.ticktext.pageY = t1;
}
l2 = l1 + w1;
l3 = l1 - l2;
l = l2;
setInterval('tick()',200);
}
function getLeft(ll) {
if (ll.offsetParent)
return (ll.offsetLeft + getLeft(ll.offsetParent));
else 
return (ll.offsetLeft);
}
function getTop(ll) {
if (ll.offsetParent)
return (ll.offsetTop + getTop(ll.offsetParent));
else
return (ll.offsetTop);
}
function tick() {
l = l - 15;
if (l < l3) l = l2;
cl = l1 - l;
cr = l2 - l;
if (ie) {
ticktext.style.posLeft = l;

ticktext.style.posTop = t1;
ticktext.style.clip = "rect(auto "+cr+"px auto "+cl+"px)";
if (first) ticktext.style.visibility = "visible";
}
else {
document.ticktext.pageX = l;
document.ticktext.clip.left = cl;
document.ticktext.clip.right = cr;
if (first) document.ticktext.visibility = "show";
}
first = false;
}

/**
* listbox redirection
*/
function goToUrl(selObj, goToLocation) {
eval("document.location.href = '" + goToLocation + "pos=" + selObj.options[selObj.selectedIndex].value + "'");
}

/**
 * getElement
 */
function getElement(e,f){
if(document.layers){
f=(f)?f:self;
if(f.document.layers[e]) {
return f.document.layers[e];
}
for(W=0;i<f.document.layers.length;W++) {
return(getElement(e,fdocument.layers[W]));
}
}
if(document.all) {
return document.all[e];
}
return document.getElementById(e);
}

function setSelectOptions(the_form, the_select, do_check)
{
var selectObject = document.forms[the_form].elements[the_select];
var selectCount  = selectObject.length;

for (var i = 0; i < selectCount; i++) {
selectObject.options[i].selected = do_check;
} // end for

return true;
} 

function setCheckboxColumn(theCheckbox){
if (document.getElementById(theCheckbox)) {
document.getElementById(theCheckbox).checked = (document.getElementById(theCheckbox).checked ? false : true);
if (document.getElementById(theCheckbox + 'r')) {
document.getElementById(theCheckbox + 'r').checked = document.getElementById(theCheckbox).checked;
}
} else {
if (document.getElementById(theCheckbox + 'r')) {
document.getElementById(theCheckbox + 'r').checked = (document.getElementById(theCheckbox +'r').checked ? false : true);
if (document.getElementById(theCheckbox)) {
document.getElementById(theCheckbox).checked = document.getElementById(theCheckbox + 'r').checked;
}
}
}
}

function copyCheckboxesRange(the_form, the_name, the_clicked)
{
if (typeof(document.forms[the_form].elements[the_name]) != 'undefined' && typeof(document.forms[the_form].elements[the_name + 'r']) != 'undefined') {
if (the_clicked !== 'r') {
if (document.forms[the_form].elements[the_name].checked == true) {
document.forms[the_form].elements[the_name + 'r'].checked = true;
}else {
document.forms[the_form].elements[the_name + 'r'].checked = false;
}
} else if (the_clicked == 'r') {
if (document.forms[the_form].elements[the_name + 'r'].checked == true) {
document.forms[the_form].elements[the_name].checked = true;
}else {
document.forms[the_form].elements[the_name].checked = false;
}
 }
}
}

/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;



function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
var theCells = null;

// 1. Pointer and mark feature are disabled or the browser can't get the
//  row -> exits
if ((thePointerColor == '' && theMarkColor == '')
|| typeof(theRow.style) == 'undefined') {
return false;
}

// 2. Gets the current row and exits if the browser can't get it
if (typeof(document.getElementsByTagName) != 'undefined') {
theCells = theRow.getElementsByTagName('td');
}
else if (typeof(theRow.cells) != 'undefined') {
theCells = theRow.cells;
}
else {
return false;
}

// 3. Gets the current color...
var rowCellsCnt  = theCells.length;
var domDetect  = null;
var currentColor = null;
var newColor = null;
// 3.1 ... with DOM compatible browsers except Opera that does not return
// valid values with "getAttribute"
if (typeof(window.opera) == 'undefined'
&& typeof(theCells[0].getAttribute) != 'undefined') {
currentColor = theCells[0].getAttribute('bgcolor');
domDetect  = true;
}
// 3.2 ... with other browsers
else {
currentColor = theCells[0].style.backgroundColor;
domDetect  = false;
} // end 3

// 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
if (currentColor.indexOf("rgb") >= 0)
{
var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
currentColor.indexOf(')'));
var rgbValues = rgbStr.split(",");
currentColor = "#";
var hexChars = "0123456789ABCDEF";
for (var i = 0; i < 3; i++)
{
var v = rgbValues[i].valueOf();
currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
}
}

// 4. Defines the new color
// 4.1 Current color is the default one
if (currentColor == ''
|| currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
if (theAction == 'over' && thePointerColor != '') {
newColor  = thePointerColor;
}
else if (theAction == 'click' && theMarkColor != '') {
newColor  = theMarkColor;
marked_row[theRowNum] = true;
}
}
// 4.1.2 Current color is the pointer one
else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
 && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
if (theAction == 'out') {
newColor  = theDefaultColor;
}
else if (theAction == 'click' && theMarkColor != '') {
newColor  = theMarkColor;
marked_row[theRowNum] = true;
// document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
}
}
// 4.1.3 Current color is the marker one
else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
if (theAction == 'click') {
newColor  = (thePointerColor != '')
 ? thePointerColor
 : theDefaultColor;
marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
 ? true
 : null;
// document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
}
} // end 4

// 5. Sets the new color...
if (newColor) {
var c = null;
// 5.1 ... with DOM compatible browsers except Opera
if (domDetect) {
for (c = 0; c < rowCellsCnt; c++) {
theCells[c].setAttribute('bgcolor', newColor, 0);
} // end for
}
// 5.2 ... with other browsers
else {
for (c = 0; c < rowCellsCnt; c++) {
theCells[c].style.backgroundColor = newColor;
}
}
} // end 5

return true;
} // end of the 'setPointer()' function


function setCheckboxesRange(the_form, do_check, basename, min, max)
{
for (var i = min; i < max; i++) {
if (typeof(document.forms[the_form].elements[basename + i]) != 'undefined') {
document.forms[the_form].elements[basename + i].checked = do_check;
}
if (typeof(document.forms[the_form].elements[basename + i + 'r']) != 'undefined') {
document.forms[the_form].elements[basename + i + 'r'].checked = do_check;
}
}

return true;
}
