// JavaScript Document

/*
	This file was largely created by Tinus Kotze
*/

// This function will load a stylesheet which will include a background image of the appropriate size for the user's desktop size
function setBackgroundSheet() {
//	document.mainform.screenWidth.value
	var shurl = 'background_'+document.mainform.screenWidth.value+'x'+document.mainform.screenHeight.value+'.css';
	var objHead = document.getElementsByTagName('head');
	var objCSS = objHead[0].appendChild(document.createElement('link'));
	void(objCSS.rel = 'stylesheet');
	void(objCSS.href = shurl);
	void(objCSS.type = 'text/css');
	void(document.body.appendChild(objCSS));
};

/*
	Validate field. Return true if field does contain data and is at least the minimum length
*/
function valField(fieldDesc,field,minLength) {
	with (field) {
		if (value==null || value=="") {
			alert(fieldDesc+" is a required field. Please fill it in.");
			focus();
			return false
		} else if ((typeof minLength != "undefined") &&
					(minLength>0 && value.length<minLength)) {
			alert(fieldDesc+" is an incorrect length.");
			focus();
			return false
		} else {
			return true
		}
	}
};

function valFieldLength(fieldDesc,field,maxLength)
{
	if (maxLength > 0) {
            if (field.value.length > maxLength) {
    			alert(fieldDesc+" is too long. Please correct it and try again.");
    			field.focus();
    			return false;
    		} else {
    			return true;
    		}
	}
	alert("maxlength <= 0");
    return true;
};

function valFieldLengthMinMax(fieldDesc,field,minLength,maxLength)
{
	if ((maxLength > 0) && (minLength >= 0)) {
            if ((field.value.length < minLength) || (field.value.length > maxLength)) {
                if (minLength != maxLength) {
    			    alert(fieldDesc+" must have between "+minLength+" and "+maxLength+" characters/digits. Please correct it and try again.");
    			}
    			else {
    			    alert(fieldDesc+" must have "+minLength+" characters/digits. Please correct it and try again.");
    			}
    			field.focus();
    			return false;
    		} else {
    			return true;
    		}
	}
	alert("maxlength <= 0");
    return true;
};

function valFieldSilent(field) {
	with (field) {
		if (value==null || value=="") {
			//alert(fieldDesc+" is a required field. Please fill it in.");
			focus();
			return false
		} else {
			return true
		}
	}
};


/*
	Used to verify if password entry is accepted.
*/
function valPasswordField(passwordField,verifyField) {
	var result = false;

	if (passwordField.value==null ||
		passwordField.value=="" ||
		passwordField.value!=verifyField.value)
	{
		alert("Password mismatch. Please enter passwords again.");
		passwordField.value = "";
		verifyField.value = "";
		passwordField.focus();
	}
	else if (passwordField.value.length<6) {
		alert("Password to short. Please enter a longer password.");
		passwordField.value = "";
		verifyField.value = "";
		passwordField.focus();
	}
	else
		result = true;

	return result;
};

function getResponseXml(xmlUrl) {
	var xmlHttp = new XMLHttpRequest();
	try {
		xmlHttp.open("GET", xmlUrl, false);
		xmlHttp.send(null);
	}
	catch (err) {
		alert('Failed to retrieve data from the server: '+err.description);
	}
	return xmlHttp;
};

function updateTitle(titleroot) {
	with (titleroot) {
		if (frames['subtitle']) {
			tb = frames['subtitle'].document.getElementById('activePatient');
			//alert(tb.innerHTML);
			data = getResponseXml('consultation_title.php?nameonly');
			if (tb) tb.innerHTML = data.responseText;
		}
		else{
			//alert('No subframe');
		}
		return true;
	}
};

/*
This function will scan the given string and will return false if any of the characters are not either a digit or
a full-stop (decimal indicator).
*/
function IsNumeric(sText) {
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

  for (i = 0; i < sText.length && IsNumber == true; i++) {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1) {IsNumber = false;};
  };

  return IsNumber;

};
// Validate a numeric field ensuring acceptable data
function valNumField(fieldDesc,field) {
	with (field) {
		if (value==null || value=="") {
			alert(fieldDesc+" is a required field. Please fill it in.");
			focus();
			return false;
		}
		if (!IsNumeric(value)) {
			alert(fieldDesc+" is not a valid number. Letters, spaces and commas are not accepted. Use the full stop character as the decimal point.");
			focus();
			return false;
		}
		return true;
/*		if (value==null || value=="" || !IsNumeric(value)) {
			alert(fieldDesc+" is not a valid number. Letters, spaces and commas are not accepted. Use the full stop character as the decimal point.");
			focus();
			return false;
		} else {
			return true;
		}*/
	}
};
// Validate a date field (or day, month and year fields) ensuring a valid date can be built from them
function valDateOld(fieldDesc,day,month,year) {
	/* new Date("Month dd, yyyy") */
	try {

		var dt,cdt;

		cdt=new Date();
		dt=new Date(month.value+" "+day.value+", "+year.value);
		/* Check if date is a valid date */
		valid = (day.value == dt.getDate());

		/* Check if date is older than current date */
		if (valid) {
			valid = (cdt.getYear()>dt.getYear()) ||
			        (cdt.getYear()==dt.getYear() && cdt.getMonth()>dt.getMonth()) ||
			        (cdt.getYear()==dt.getYear() && cdt.getMonth()==dt.getMonth() && cdt.getDate()>=dt.getDate());
			if (!valid) {
				alert(fieldDesc+" is set to a future date. Please correct the problem.");
				day.focus();

			}
		}
		else{
			alert(fieldDesc+" is not a valid date. Please correct the problem.");
			day.focus();
		}
	}
	catch (e) {
		alert('Date validation error');
		valid = false;
	}
	return valid;

};
function valDateOldQuiet(day,month,year) {
    // J Vos 2009/02/23 (copied from valDateOld)
	/* new Date("Month dd, yyyy") */
	try {

		var dt,cur_dt;

		cdt=new Date();
		dt=new Date(month.value+" "+day.value+", "+year.value);
		/* Check if date is a valid date */
		valid = (day.value == dt.getDate());

		/* Check if date is older than current date */
		if (valid) {
			valid = (cdt.getYear()>dt.getYear()) ||
			        (cdt.getYear()==dt.getYear() && cdt.getMonth()>dt.getMonth()) ||
			        (cdt.getYear()==dt.getYear() && cdt.getMonth()==dt.getMonth() && cdt.getDate()>=dt.getDate());
			if (!valid) {
				//alert(fieldDesc+" is set to a future date. Please correct the problem.");
				//day.focus();

			}
		}
		else{
			//alert(fieldDesc+" is not a valid date. Please correct the problem.");
			//day.focus();
		}
	}
	catch (e) {
		alert('Date validation error');
		valid = false;
	}
	return valid;

};

function valDate1AfterDate2Quiet(day1,month1,year1, day2,month2,year2) {
    // J Vos 2009/02/23

    d1 = new Date(month1.value+" "+day1.value+", "+year1.value);
    d2 = new Date(month2.value+" "+day2.value+", "+year2.value);

    valid = ((day1.value == d1.getDate()) && (day2.value == d2.getDate()));
	/* Check if date is a valid date */
	//valid = ((day1.value == d1.getDate()) && (month1.value == d1.getMonth()) && (year1.value == d1.getFullYear()));
	//valid = valid && ((day2.value == d2.getDate()) && (month2.value == d2.getMonth()) && (year2.value == d2.getFullYear()));
    //s = '';

    if (valid) {
        t1=d1.getFullYear();
        t2=d2.getFullYear();
        if (d2.getFullYear() > d1.getFullYear()) {
            valid = false;
            //s = 'Year1='+d1.getFullYear()+', Year2='+d2.getFullYear();
        } else {
            if (d2.getFullYear() == d1.getFullYear()) {
                t1=d1.getMonth();
                t2=d2.getMonth();
                if (t2 > t1) {
                    valid = false;
                    //s = 'Month1='+d1.getMonth()+', Month2='+d2.getMonth();
                } else {
                    if (t2 == t1) {
                        t1=d1.getDate();
                        t2=d2.getDate();
                        if (t2 > t1) {
                            valid = false;
                            //s = 'Day1='+d1.getDate()+', Day2='+d2.getDate();
                        }
                    }
                }
            }
        }
    }
    //if (!valid) {
    //    alert(s);
    //}
    return valid;
};



// Validate hour and minute fields to ensure that a valid time can be built from them
function valTime(fieldDesc,hour,minute) {
	/* Check if time is valid*/
	valid = (hour.value != '') && IsNumeric(hour.value) && (hour.value>=0) && (hour.value<=23);
	if (!valid) {l
		alert(fieldDesc+' contains an invalid hour. Hours can only be a number from 0 to 23.');
		hour.focus();
	}
	else {
		valid = (minute.value != '') && IsNumeric(minute.value) && (minute.value>=0) && (minute.value<=23);
		if (!valid) {
			alert(fieldDesc+' contains an invalid minute value. Minutes can only be a number from 0 to 59.');
			minute.focus();
		}
	}
	return valid;
};
// This function will cause the browser to print the selected frame (typically used to print the contents of an <iframe> HTML object)
function PrintIFrame(frameName) {
	window.frames[frameName].focus();
	window.frames[frameName].print();
	return false;
};
// Returns true if the text field contains no data
function IsEmpty(aTextField) {
	if ((aTextField.value.length==0) || (aTextField.value==null)) {
		return true;
	} else {
		return false;
	}
};

function showPopup()
{
    t_frm = parent.subcontent;
    t_frm.popup("popUpDiv");
}

function hidePopup()
{
    parent.content.document.getElementById('blanket').style.display = 'none';
    parent.content.document.getElementById('popUpDiv').style.display = 'none';
}

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {el.style.display = 'block';}
	else {el.style.display = 'none';}
}

function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-250;
	popUpDiv.style.top = popUpDiv_height + 'px';
}

function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-400;
	popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);
}
