
// - Left String function

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

// - Right String function

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


// //////////////////////////////////////////


// - this validates the form using trigger attributes

function validateForm()
{
	var x = document.forms[0].elements;
	for (var i=0;i<x.length;i++)
	{
// - set 'required=' and the message to display
		if (x[i].getAttribute('required') && !x[i].value)
		{ 	var msg=x[i].getAttribute('required')
			alert(msg)
			x[i].focus()
			return false;
		}

// - set 'email=true'
		if (x[i].getAttribute('email'))
		{ 	
			if ((x[i].value.indexOf('@') < 1) || 
    				(x[i].value.lastIndexOf('.') <= x[i].value.indexOf('@')+1) ||  
    				(x[i].value.lastIndexOf('.') == x[i].value.length - 1 ) ||  
    				(x[i].value.indexOf(' ')  != -1)) 
   			{  
      				alert('Please enter a valid e-mail address!')
				x[i].focus()
            			return false;
			}

		}

// - set 'confirm=' and the fieldname to match against
// - useful for confirmation passwords
// - could be adapted to compare numeric values of fields etc
		if (x[i].getAttribute('confirm'))
		{ 	var field=x[i].getAttribute('confirm')
			var ref=document.forms[0].elements[field].value
			if (ref!=x[i].value)
			{
				alert("Your password & confirmation don't match.  Please try again.")
				x[i].focus()
				return false;
			}
		}


// - set 'docupload=' and the message to display
// - checks the file extension of files being uploaded
		if (x[i].getAttribute('docupload') && x[i].value.length >0)
		{
			var fileext=Right(x[i].value,4)	

			if (fileext.toLowerCase() !=".doc" && fileext.toLowerCase() !=".pdf")
			{ 	var msg=x[i].getAttribute('docupload')
				alert(msg)
				x[i].focus()
				return false;
			}

 		}
 	}
}	


function openWindow() 
{
	if (!window.callwindow) 
	{
       // has not yet been defined
	   callwindow = window.open('callme.aspx','callwindow','width=360,height=280,titlebar=no,menubar=no,toolbar=no,status=no');
	}
	else 
	{
	    // has been defined
	    if (!callwindow.closed) 
	    {
			// still open
			callwindow.close();
	        callwindow = window.open('callme.aspx','callwindow','width=360,height=280,titlebar=no,menubar=no,toolbar=no,status=no');
	    }
	 }
}
	
 	
 	
