function delete_record(id, table_name, return_url)
{
	response = confirm("Are you sure you want to delete this record?");
	if(response == true)
	{
		location.href = "record_delete.asp?id="+id+"&table_name="+table_name+"&return_url="+return_url;		
	}
}
function delete_record_extended(id, table_name, return_url, status)
{
	response = confirm("Are you sure you want to delete this record?");
	if(response == true)
	{
		location.href = "record_delete_extended.asp?id="+id+"&table_name="+table_name+"&return_url="+return_url+"&status="+status;		
	}
}
function purge_record(id, table_name, return_url)
{
	response = confirm("Are you sure you want to delete this record?");
	if(response == true)
	{
		location.href = "record_purge.asp?id="+id+"&table_name="+table_name+"&return_url="+return_url;		
	}
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(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;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(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;
}

function daysInFebruary (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 );
}
function DaysArray(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
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=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){
	if (pos1<1 || pos2<1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function Validate_Form() {
	var error = 0;
	var sMessage = "";
	//var sFieldName;
	//var sFieldValue;
	var i;
	
	for(i=0; i<document.forms[0].elements.length; i++)
	{
	   	//alert(document.forms[0].elements[i].name)
		//alert(document.form1.elements[i].name + ": " + document.form1.elements[i].value);
		
		//------------------------------------------------------------------------
		// will need to update code with different types of element at some stage
		//if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
			//alertText += "Element Value: " + theForm.elements[i].value + "\n"
		//}
		//else if(theForm.elements[i].type == "checkbox"){
			//alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
		//}
		//else if(theForm.elements[i].type == "select-one"){
			//alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
		//-------------------------------------------------------------------------
		if (document.forms[0].elements[i].className == "txt_required")
		{
			if (document.forms[0].elements[i].value == "")
			{
				sMessage = "'" + document.forms[0].elements[i].id + "' is a required field.";
				document.forms[0].elements[i].focus();
				error = 1;
				break;
			}
		}
		if (document.forms[0].elements[i].className == "required_date")
		{
			if (document.forms[0].elements[i].value == "")
			{
				sMessage = "'" + document.forms[0].elements[i].name + "' is a required date field.";
				document.forms[0].elements[i].focus();
				error = 1;
				break;
			}
		}
		if (document.forms[0].elements[i].className == "required_date")
		{
			if (document.forms[0].elements[i].value != "")
			{
				//sMessage = isDate(document.forms[0].elements[i].value);
				//error = 1;
				//break;
				if (isDate(document.forms[0].elements[i].value) == false)
				{
					sMessage = "'" + document.forms[0].elements[i].name + "' is not a valid date.";
					document.forms[0].elements[i].focus();
					error = 2;
					break;
				}
			}
		}		
	}
		
	if (error == 1)
	{
		alert(sMessage);
		return (false);
	}
	else if (error == 2)
	{
		return (false);
	}
	else
	{
		return (true);
	}
}

function checkArray(arrayName)
  {
    var retval = new Array();
    for(var i=0; i < document.forms[0].elements.length; i++) {
      var el = document.forms[0].elements[i];
      if(el.type == "checkbox" && el.name == arrayName && el.checked) {
        retval.push(el.value);
      }
    }
    return retval;
  }

function checkDistOption(array_name)
  {
	//alert("test");
    var itemsChecked = checkArray(array_name);
    //alert("You selected " + itemsChecked.length + " items");
    if(itemsChecked.length > 0) {
      //alert("The items selected were:\n\t" + itemsChecked);
	  //return true;
    }
	else
	{
		alert("Please select at least 1 type of distribution.");
    	return false;
	}
  }
