// JavaScript Document
function OpenWindow(wintype, turl, wd, ht) {
       var windowFeatures =  '';
	   if(wintype == 'tablename') {
                window_width = 450;
                window_height = 300;
                window_top = (screen.availHeight-window_height)/2
                window_left = (screen.availWidth-window_width)/2
                windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
                windowFeatures += window_top
                windowFeatures += ",left="
                windowFeatures += window_left
                windowFeatures += ',status=1'
                windowFeatures += ',scrollbars=yes'
        }
        else if (wintype == 'general') {
          window_width = wd;
          window_height = ht;
          window_top = (screen.availHeight-window_height)/2
          window_left = (screen.availWidth-window_width)/2
          windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
          windowFeatures += window_top
          windowFeatures += ",left="
          windowFeatures += window_left
          windowFeatures += ',scrollbars=no'
       }
        else {
          window_width = wd;
          window_height = ht;
          window_top = (screen.availHeight-window_height) - 30
          window_left = (screen.availWidth-window_width) - 30
          windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
          windowFeatures += window_top
          windowFeatures += ",left="
          windowFeatures += window_left
          windowFeatures += ',scrollbars=no'
        }
window.open(turl,wintype,windowFeatures);
}

function isEmpty(formname,fieldname,displayname){
        itemvalue = eval("document." + formname + "." + fieldname + ".value");
        itemvalue = itemvalue.replace(/ /g,"");
        if(!itemvalue){
                alert("Please enter \"" + displayname + "\"");
                eval("document." + formname + "." + fieldname + ".focus()");
                return false;
        }
        else{
                return true;
        }
}

function checkEmail(emailString) {
	splitVal = emailString.split('@');
	
	if(splitVal.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
		alert("Please enter a valid email address");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	return true;
}

function trimSpace(frmElement)
{
		var stringToTrim = frmElement.value;
		var len = stringToTrim.length;

        var front;
        var back;
        for(front = 0; front < len && (stringToTrim.charAt(front) == ' ' || stringToTrim.charAt(front) == '\n' || stringToTrim.charAt(front) == '\r' || stringToTrim.charAt(front) == '\t'); front++);
        for(back = len; back > 0 && back > front && (stringToTrim.charAt(back - 1) == ' ' || stringToTrim.charAt(back - 1) == '\n' || stringToTrim.charAt(back - 1) == '\r' || stringToTrim.charAt(back - 1) == '\t'); back--);

        frmElement.value = stringToTrim.substring(front, back);
}

function checkNewsletter()
{
	trimSpace(document.newsletter.firstName);
	if (!isEmpty('newsletter','firstName','First Name')) {
			return false;
	}
	trimSpace(document.newsletter.lastName);
	if (!isEmpty('newsletter','lastName','Last Name')) {
			return false;
	}		
	trimSpace(document.newsletter.email);
	if (!isEmpty('newsletter','email','E-mail')) {
			return false;
	}	
	if (!checkEmail(document.newsletter.email.value))
	{
		document.newsletter.email.focus();
		return false;
	}
	document.newsletter.fAction.value = "signup";
	return true;
}

function checkAppointment()
{
	trimSpace(document.appointment.firstName);
	if (!isEmpty('appointment','firstName','First Name')) {
			return false;
	}
	trimSpace(document.appointment.lastName);
	if (!isEmpty('appointment','lastName','Last Name')) {
			return false;
	}		
	trimSpace(document.appointment.email);
	if (!isEmpty('appointment','email','E-mail')) {
			return false;
	}	
	if (!checkEmail(document.appointment.email.value))
	{
		document.appointment.email.focus();
		return false;
	}
	trimSpace(document.appointment.phone);
	if (!isEmpty('appointment','phone','Telephone Number')) {
			return false;
	}	
	document.appointment.fAction.value = "send";
	return true;
}
