function validate_email(email_txt) // validates a string as a email id
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@([\\w].+)\.[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(email_txt);
}

function validate_integer(int_str) // validates a string as an integer (i.e. no decimal points)
{
	regExpr = new RegExp(/^\d*$/);
	var regex = new RegExp(regExpr);
	return regex.test(int_str);
}

function validate_real_nos(real_nos_str) // validates a string as a Real Number (i.e. with decimal points)
{
	regExpr = new RegExp(/^-?\d*(\.\d{1,2})?$/);
	var regex = new RegExp(regExpr);
	return regex.test(real_nos_str);
}

function RoundOff(number,n) // rounds up given no to 'n' number of places
{
	str='';
	number=parseFloat(number);
	if(!isNaN(number))
	{
		power=Math.pow(10,n);
		number=Math.round(number*power)/power;
		return number;
	}
}

function str_trim(str) // strips of leading and following whitespaces from a string
{	
	if(str.length > 0)
		while(str.charAt(0)==' ')
			str = str.substr(1);
		
	if(str.length > 0)
		while(str.charAt((str.length - 1))==' ')
			str = str.substring(0, str.length-1);
	
	return str;
}

function str_implode(arr, delim) // Join array elements with a string
{
	var ret_str = "";
	var arr_len = 0;

	arr_len = arr.length;
	delim_len = delim.length;
	
	if(arr_len > 0)
	{
		for(var i=0; i < arr_len; i++)
			ret_str += delim + arr[i];

		if(ret_str.substr(0, delim_len))
			ret_str = ret_str.substr(delim_len);
	}
	
	return ret_str;
}

function str_explode(arr, delim)
{
	var ret_str = "";
	var arr_len = 0;

	arr_len = arr.length;
	delim_len = delim.length;
	
	if(arr_len > 0)
	{
		for(var i=0; i < arr_len; i++)
			ret_str += delim + arr[i];

		if(ret_str.substr(0, delim_len))
			ret_str = ret_str.substr(delim_len);
	}
	
	return ret_str;
}

function checkNum()
{
	var carCode = event.keyCode;
	
	if ((carCode < 48) || (carCode > 57))
	{
		alert('Please enter only numeric values.');
		event.cancelBubble = true;
		event.returnValue = false;
	}
} 

function CheckNum(obj) // validates a form ctrl for an integer
{
	regExpr = new RegExp(/^\d*$/);

	if(str_trim(obj.value)=="" || !regExpr.test(obj.value))
		obj.value="0";		
}

function CheckNum2(obj) // validates a form ctrl for an integer
{
	regExpr = new RegExp(/^\d*$/);

	if(!regExpr.test(obj.value))
		obj.value="0";		
}

function CheckRealNum(obj) // validates a form ctrl for a real number
{
	regExpr=new RegExp(/^-?\d*(\.\d{1,2})?$/);
	
	if(str_trim(obj.value)=="" || !regExpr.test(obj.value))
		obj.value="0";		
}

// NAVIGATION BASED
/*function AddAnother()
{
	if(Validate())
	{
		document.forms[0].add_mode.value = "Y";
		document.forms[0].submit();
	}
	else
		document.forms[0].add_mode.value = "N";
} // */

function GoToPage(page)
{
	window.document.location.href=page;
}

function ConfirmDelete(txt, page)
{
	var msg = "You Are About To Delete this " + txt + "! Continue?";

	if(confirm(msg))
		window.document.location.href=page;
}

function SubmitToThisPage(frm_nm, ctrl_nm, page_name)
{
	var page_no = document.getElementById(ctrl_nm).value;	
	var frm = eval("document."+frm_nm);
	
	frm.action = page_name+"?page="+page_no;
	frm.submit();
}

function SubmitPage(frm_nm, page)
{
	var frm = eval("document."+frm_nm);	
	frm.action = page;
	frm.submit();
}

function DefaultFocus(ctrl_nm)
{
	if(document.getElementById(ctrl_nm))
	{
		obj = document.getElementById(ctrl_nm);
		obj.focus();
	}
}

function SetFocus(obj)
{
	obj.focus();
	obj.select();
}

function UploadMailerPics()
{
	info="upload_images.php";
	window.open(info,"","menubars=0,width=500,height=550,scrollbars=1,resizable=1");
}

function ChangeRank(mode, fld, fld_val)
{
	var str = "";

	if(fld != "" && fld_val != "")
		str = "&fld=" + fld + "&fld_val=" + fld_val;

	var file = "rank-update.php?mode="+mode+str;
	win = window.open(file,'ChangeRank','width=900,height=600,scrollbars=1,resizable=1');
	WindowPosition(900,600,win);
}

function ChangeImageRank(mode, fld, fld_val)
{
	var str = "";

	if((!fld || fld == "undefined") && (!fld_val || fld_val == "undefined")) {}
	else
		str = "&fld=" + fld + "&fld_val=" + fld_val;

	var file = "uploaded_image_disp.php?mode="+mode+str; //	var file = "image-rank-update.php?mode="+mode+str;
	win = window.open(file,'ChangeRank','width=900,height=600,scrollbars=1,resizable=1');
	WindowPosition(900,600,win);
}

function WindowPosition(widthX,heightX,windowName)
{
	var width = (screen.width);
	var height = (screen.height - 25);
	var centerleft = 0;
	var centertop = 0;
	var centerleft = (width/2) - (widthX/2);
	var centertop = (height/2) - (heightX/2);
	var width=widthX;
	var height=heightX;
	windowName.moveTo(centerleft,centertop);
	windowName.resizeTo(width, height);
	windowName.focus();
}

function WindowPosition2(widthX,heightX,windowName) //same as above, only it takes %age vals as params for width n height
{
	var width = (screen.width);
	var height = (screen.height - 25);
		
	var wt = (width * widthX) / 100;
	var ht = (height * heightX) / 100;

	var centerleft = (width/2) - (wt/2);
	var centertop = (height/2) - (ht/2);

	windowName.resizeTo(wt, ht);
	windowName.moveTo(centerleft,centertop);
	windowName.focus();
}

function FillFromList(list_obj)
{
	var cmb = list_obj;

	str = "";
		
	for(i=0; i < cmb.length; i++)
		if(cmb.options[i])
			str += "~" + cmb.options[i].value;
	
	return str.substr(1);
}

function ConvertFromYMDtoDMY(ymd_date)
{
	var dt_arr = ymd_date.split('-');
	var dmy_date = dt_arr[2] + "-" + dt_arr[1] + "-" + dt_arr[0];
	return dmy_date;
}

function ConvertFromDMYtoYMD(dmy_date)
{
	var dt_arr = dmy_date.split('-');
	var ymd_date = dt_arr[2] + "-" + dt_arr[1] + "-" + dt_arr[0];
	return ymd_date;
}

function GetRadioValue(rd_obj)
{
	for(var i=0; i < rd_obj.length; i++)
	{
		if(rd_obj[i].checked)
			return rd_obj[i].value;
	}

	return false;
}

function ChangeRankX(mode, cond_str)
{
	var str = "";

	if(cond_str && cond_str != "")
		str = "&cond_str=" + cond_str;

	var file = "rank-updatex.php?mode="+mode+str;
	win = window.open(file,'ChangeRank','width=900,height=600,scrollbars=1,resizable=1');
	WindowPosition(900,600,win);
}

function SetIFrameSrc(iframe_name, url)
{
	if(!document.getElementById(iframe_name))
		return false;
	
	document.getElementById(iframe_name).src = url;
}

var win='';
function OpenPopup(file)
{
	if (win.location && !win.closed) 
	{
		// win.location.href = url;
		win.focus(); 
	}
	else 
	{
		win = window.open(file,'Popup','width=600,height=450,scrollbars=1,resizable=1');
		WindowPosition(700,650,win);
	}
}


function PicExists()
{
	alert("Picture already exists..Please delete the existing picture to add a new one!!!");
	return false;
}

function DateDiff(dtfrom,dto) // works with d-m-Y formatted dates
{
	var ret_val = false;
	
	fday = dtfrom.substr(0,2);
	tday = dto.substr(0,2);
	fmonth = dtfrom.substr(3,2);
	tmonth = dto.substr(3,2);
	fyear = dtfrom.substr(6,4);
	tyear = dto.substr(6,4);
	
	dtFdate = new Date(fyear,fmonth,fday);
	dtTdate = new Date(tyear,tmonth,tday);
	
	var sOneDay = 1000*60*60*24;
	var iDiff = dtTdate-dtFdate;
	var diff = (iDiff/sOneDay);
	
	return diff;
}

function UploadImages(dir)
{
	info="editor_upload_images.php?dir="+dir;
	win = window.open(info,'editor','menubars=0,width=500,height=550,scrollbars=1,resizable=1');
	WindowPosition(1050,450,win);
}

function VerifyDate(str_dt1, str_dt2) //(1st date[from], 2nd date[to])
{	
	var arr_dt1 = str_dt1.split("-");
	dt1 = new Date(arr_dt1[1] + "/" + arr_dt1[0] + "/" + arr_dt1[2]);

	var arr_dt2 = str_dt2.split("-");
	dt2 = new Date(arr_dt2[1] + "/" + arr_dt2[0] + "/" + arr_dt2[2]);

	difference = dt2 - dt1;
	years = difference/(1000*60*60*24*365);

	if(difference < 0)
		return false;

	return true;
}

function in_array(str, arr)
{
	var ret_flag = false;
	var arr_len = 0;

	arr_len = arr.length;

	if(arr_len > 0)
	{
		for(var i=0; i < arr_len; i++)	
			if(arr[i] == str)
			{
				ret_flag = true;
				break;
			}
	}

	return ret_flag;
}

function ArrayIndex(arr, str)
{
	var ret_flag = false;
	var arr_len = 0;

	arr_len = arr.length;

	if(arr_len > 0)
	{
		for(var i=0; i < arr_len; i++)	
			if(arr[i] == str)
			{
				ret_val = i;
				break;
			}
	}

	return ret_val;
}

function ToggleOptions(obj_id)
{
	if(!document.getElementById(obj_id))
		return false;
	
	obj = document.getElementById(obj_id);
	var disp_flag = obj.style.display;
	disp_cmb_flag = true;
	
	if(disp_flag == "none")
	{
		disp_cmb_flag = false;
		var div_arr = document.getElementsByTagName("div");
		var div_len = div_arr.length;
		
		for(var i=0; i < div_len; i++)
		{
			var div_nm_arr = div_arr[i].id.split("_");
			
			// if any other sliders are open...
			if(div_nm_arr[0] == "slide" && div_arr[i].style.display != "none")
				div_arr[i].style.display = "none";
		}

		obj.style.display = "block";
	}
	else
		obj.style.display = "none";

	ToggleComboBoxVisibility(disp_cmb_flag);
}

function SetSelectedValues(form_name, chk_obj, txt_nm, lbl_nm)
{
	var chk_val = chk_obj.value.split('~');
	var chk_id = chk_val[0];
	var chk_nm = chk_val[1];

	var txt_obj = eval("document."+form_name+"."+txt_nm);	
	var txt_arr = txt_obj.value.split(',');
	var txt_cnt = txt_arr.length;

	var lbl_obj = document.getElementById(lbl_nm);
	var lbl_arr = lbl_obj.innerHTML.split(',');
	var lbl_cnt = lbl_arr.length;

	if(chk_obj.checked) // add to list
	{
		if(txt_cnt > 0 && txt_obj.value!='')
		{
			if(!in_array(chk_id, txt_arr))
			{
				txt_obj.value += ","+chk_id;
				lbl_obj.innerHTML += ", " + chk_nm;
			}
		}
		else
		{
			txt_obj.value = chk_id;
			lbl_obj.innerHTML = chk_nm;
		}
	}
	else // remove from list
	{
		var rmv_index = -1; // index of value to be removed...
		
		if(txt_cnt > 0 && txt_obj.value!='')
		{
			rmv_index = ArrayIndex(txt_arr, chk_id);

			if(rmv_index > -1)
			{
				for(var i=rmv_index; i < txt_arr.length; i++)
				{
					if(i != (txt_arr.length - 1)) // not the last element in the array
					{
						txt_arr[i] = txt_arr[i+1];
						lbl_arr[i] = lbl_arr[i+1];
					}
				}
				
				txt_arr.length = (txt_cnt - 1); // pop off the last array item
				lbl_arr.length = (lbl_cnt - 1);
				txt_obj.value = txt_arr.join(",");
				lbl_obj.innerHTML = lbl_arr.join(",");
			}
		}
		
		if(txt_obj.value == "")
			lbl_obj.innerHTML = "None Selected";
	}
} 

function ToggleComboBoxVisibility(flag)
{
	visibility_mode = (flag)? "visible": "hidden";

	for(var i = 0; i < document.forms.length; i++) 
		for(var j=0; j < document.forms[i].elements.length; j++)
			if(document.forms[i].elements[j].type == 'select-one')
				document.forms[i].elements[j].style.visibility = visibility_mode;
}

function ToggleFilterVisibiltity(frm)
{
	var tbl_obj = document.getElementById(frm);
	if(!tbl_obj)
		return false;
	
	tbl_obj.style.display = (tbl_obj.style.display=='none')? '': 'none';
}

function ToggleVisibility(obj_id)
{
	var obj = document.getElementById(obj_id);
	if(!obj)
		return false;
	
	obj.style.display = (obj.style.display=='none')? '': 'none';
}

function divX(div_width) // width
{
	var screen_width = (screen.width);
	var width_offset = posLeft();
	var x = (width_offset + (screen_width/2) - (div_width/2));
	return x;
}

function divY(div_height) // height
{
	var screen_height = (screen.height - 100);
	var height_offset = posTop();
	var y = height_offset + (screen_height/2) - (div_height/2);
	return y;
}

function posTop() 
{
	return (typeof window.pageYOffset != 'undefined') 
			? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) 
									? document.documentElement.scrollTop: document.body.scrollTop;
}

function posLeft() 
{
	return (typeof window.pageXOffset != 'undefined') 
			? window.pageXOffset : (document.documentElement && document.documentElement.scrollLeft) 
									? document.documentElement.scrollLeft: document.body.scrollLeft;
}

function EnterThisPage(url) // only for pagination...
{
	if(window.event.keyCode == 13)
		window.document.location.href=url;
}

function AddAnother(frm)
{
	frm.add_mode.value = "Y";
	
	if(Validate(frm))
		frm.submit();
}

function DumpProperties(obj, parent) 
{
   for (var i in obj) // Go through all the properties of the passed-in object
   {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
//      if (typeof obj[i] == "object") {
 //        if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
//		 dumpProps(obj[i], i);
 //     }
   }
}



// **BEGIN GENERIC VALIDATION FUNCTIONS**
// general purpose function to see if an input value 
// has been entered at all
function isEmpty(inputStr) {
  if (inputStr == "" || inputStr == null) {
    return true
  }
  return false
}

// function to determine if value is in acceptable range 
//for this application
function inRange(inputStr, lo, hi) {
  var num = parseInt(inputStr, 10)
  if (num < lo || num > hi) {
    return false
  }
  return true
}
// **END GENERIC VALIDATION FUNCTIONS**

function validateMonth(field, bypassUpdate) {
  var input = field.value
  if (isEmpty(input)) {
    alert("Be sure to enter a month value.")
    select(field)
    return false
  } else {
    input = parseInt(field.value, 10)
    if (isNaN(input)) {
      alert("Entries must be numbers only.")
      select(field)
      return false
    } else {
      if (!inRange(input,1,12)) {
         alert("Enter a month between 1 (January) and 12 (December).")
         select(field)
         return false
      }
    }
  }
  if (!bypassUpdate) {
      calcDate()
  }
  return true
}

function validateDate(field) {
  var input = field.value
  if (isEmpty(input)) {
    alert("Be sure to enter a date value.")
    select(field)
    return false
  } else {
    input = parseInt(field.value, 10)
    if (isNaN(input)) {
      alert("Entries must be numbers only.")
      select(field)
      return false
    } else {
      var monthField = document.frmMember.month
      if (!validateMonth(monthField, true)) return false
      var monthVal = parseInt(monthField.value, 10)
      var monthMax = new Array(31,31,29,31,30,31,30,31,
                               31,30,31,30,31)
      var top = monthMax[monthVal]
	  
////////////////////////////////////  DAVE  ////////////////////////////////////////	  
	  if(document.frmMember.month.value==2 && document.frmMember.date.value>=29)
	  {
	  	
	  	 if(!checkYear(document.frmMember.year.value))
		 {
		 alert("Enter a date between 1 and 28");
		 select(document.frmMember.date)
		 return false;
		 }
		 else
		 {
		 	if(document.frmMember.date.value>29)
			{
			 alert("Enter a date between 1 and 29");
			 select(document.frmMember.date)
			 return false;
	  		}
			else
			return true;
	 	}
	  }
////////////////////////////////////  DAVE  ////////////////////////////////////////
	  else
      if (!inRange(input,1,top)) {
        alert("Enter a date between 1 and " + top + ".")
        select(field)
        return false
      }
    }
  }
  calcDate()
  return true
}

function validateYear(field) {
  var input = field.value
  if (isEmpty(input)) {
    alert("Be sure to enter a year value.")
    select(field)
    return false
  } else {
    input = parseInt(field.value, 10)
    if (isNaN(input)) {
      alert("Entries must be numbers only.")
      select(field)
      return false
    } else {
      if (!inRange(input,1900,2009)) {
        alert("Enter a year between 1900 and 2009.")
        select(field)
        return false
      }
    }
  }
  calcDate()
  return true
}


function checkYear(year) { 
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
}

function select(field) {
  field.focus()
  field.select()
}

// **END GENERIC VALIDATION FUNCTIONS**

function checkForm(form) {
  if (validateMonth(form.month)) {
    if (validateDate(form.date)) {
      if (validateYear(form.year))
	   {
	  	return true
      }
    }
  }
  return false
}

function calcDate()
{
  var mm = document.frmMember.month.value
  var dd = document.frmMember.date.value
  var yy = document.frmMember.year.value
  
  if(mm.length==1)
  mm="0"+mm
  
  if(dd.length==1)
  dd="0"+dd
  
  document.frmMember.txtDOB.value = yy + "-" + mm + "-" + dd
}

function ShowContent(file_name)
{	

	if(document.getElementById('div_window'))
	{
		var dobj = document.getElementById('div_window');
		
		if(dobj.style.display == "none")
		{
			dobj.style.display = "block";
			//ToggleComboBoxVisibility(false);
		}
		
		div_ht = 270;
		div_wt = 496;
		
		dobj.style.top = divY(div_ht);
		dobj.style.left = divX(div_wt);
		
		var iobj = document.getElementById("iframe_content");
		iobj.src = file_name;
	}
}

function HideContent()
{
	if(document.getElementById('div_window'))
	{
		var dobj = document.getElementById('div_window');
		dobj.style.display = "none";
	}
}

function divX(div_width) // width
{
	var screen_width = (screen.width);
	var width_offset = posLeft();
	var x = (width_offset + (screen_width/2) - (div_width/2));
	return x;
}

function divY(div_height) // height
{
	var screen_height = (screen.height - 100);
	var height_offset = posTop();
	var y = height_offset + 100; // + screen_height; // (screen_height/2) - (div_height/2);
	return y;
}

function Confirmscript(txt, page)
{
	var msg = "You Are About To Execute  " + txt + " script! Continue?";

	if(confirm(msg))
		window.document.location.href=page;
}

function ToggleDefaultText(obj, default_text, flag)
{		
	if(flag=='clear' && obj.value==default_text) { obj.value=''; obj.focus(); }
	else if(flag=='set' && str_trim(obj.value)=='') { obj.value = default_text; }
}

function SwitchCtrls(obj1, obj2, focus_flag)
{
	obj1.style.display = 'none';
	obj2.style.display = '';
	if(focus_flag) obj2.focus();
}

function Passwordify(obj, pass_obj)
{
	pass_obj.value = obj.value;
	
	var hidden_str = '';

	for(i=obj.value.length; i >	0; i--)
		hidden_str += '*';
	
	obj.value = hidden_str;
}

function ResetSrnos(count)
{
	var td_arr = document.getElementsByTagName('td');
	var td_len = td_arr.length;
	
	for(i=1, ctr=0; i <= td_len; i++)
	{
		if(!td_arr[i] || !td_arr[i].id || td_arr[i].id.indexOf("srno_")<0)
			continue;
		
		td_arr[i].innerHTML = (++ctr) + '.';
	}
}

function setgwanswer(url,qid,aid,mid) // url = xyz.php?mode=
{
	var url_str = url+"?mode=GW&";
	var myRandom=parseInt(Math.random()*99999999);  // cache buster

	//alert(url_str+"status="+status+"&id="+id+"&rand=" + myRandom);
	http.open("GET", url_str +"qid="+qid+"&aid="+aid+"&mid="+mid+"&rand=" + myRandom, true);
	http.onreadystatechange = handleGWAnsResponse;
	http.send(null);
}

function handleGWAnsResponse()	// return type flag~id~display string
{
	if (http.readyState == 4)
	{
		/*if(http.responseText)
			alert("Your Answer For This Question has been Successfully Saved");
		else
			alert("Failed To Save Answer");*/
  	}
}

function setanstoques(url,qid,aid) // url = xyz.php?mode=
{
	var url_str = url+"?mode=GW&";
	var myRandom=parseInt(Math.random()*99999999);  // cache buster

	//alert(url_str+"status="+status+"&id="+id+"&rand=" + myRandom);
	http.open("GET", url_str +"qid="+qid+"&aid="+aid+"&rand=" + myRandom, true);
	http.onreadystatechange = handleAnsToQuesResponse;
	http.send(null);
}

function handleAnsToQuesResponse()	// return type flag~id~display string
{
	if (http.readyState == 4)
	{
		if(http.responseText)
			alert("Updated Successfully");
		else
			alert("Failed To Save Answer");
  	}
}