/* this function opens an htm file in a separate window. Parameters are the URL filename,
   window width and height in pixels, and window position from left and top of screen */

var newWindow = null;

function openWin(URL, wWidth, wHeight, wLeft, wTop) 
{ 
if(newWindow&&!newWindow.closed)
{
  closeWin();
}

//  alert("Wait a tick!");

  newWindow=window.open(URL,"XXX","toolbar=no,left="+wLeft+",top="+wTop+",width="+wWidth+",height="+wHeight+",status=no,resize=no,menubar=no");
}

/* this function closes an open window. It is necessary to resolve a problem with IE4 */

function closeWin() 
{ 
  newWindow.close();
}


function checkEmail(str)

// Validate e-mail address - called by ValidateForm function

{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
	   return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
	   return false
	}
	if (str.indexOf(" ")!=-1){
	   return false
	}
	return true					
}

function checkInvChars(str)

// Check for any invalid characters in field - called by ValidateForm function

{
	var checkInv = "<>";
	var i;
	for (i = 0; i < str.length; i++) {
		if (checkInv.indexOf(str.charAt(i))>=0) {
			return false		
		}
	}
	return true
}

function checkTelephone(checkStr)

// Check for a valid telephone number - called by ValidateForm function

{
	var checkOK="0123456789- ";
	var allValid=true;
	var spaces=0;
	var hyphens=0;
	var digits=0;
	for (i=0; i<checkStr.length; i++) {
		ch=checkStr.charAt(i);
		if (checkOK.indexOf(ch)==-1) {
			allValid=false;
			break;
		}
	if (ch=="-") {
		hyphens++ }
	if (ch==" ") {
		spaces++; }
	if (checkOK.indexOf(ch)<=9) {
		digits++; }
	}
	if (allValid==false || hyphens>2 || spaces>3 || digits<8) {
		return false }
	else {
		return true }
}

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 dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	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){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || 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 ValidateForm(checkForm)

// Validate form enquiry data. 

{
	if (checkForm.Name.value.length < 1 || checkInvChars(checkForm.Name.value)==false) {
		alert("Name not entered or contains invalid characters")	
		checkForm.Name.focus()
		return false
	}
	if (checkForm.Company.value.length > 0 && checkInvChars(checkForm.Company.value)==false) {
		alert("Company contains invalid characters")
		checkForm.Company.focus()
		return false
	}
	if (checkForm.Telephone.value.length > 0 && checkTelephone(checkForm.Telephone.value)==false) {
		alert("Telephone number invalid")	
		checkForm.Telephone.focus()
		return false
	}	
	if (checkForm.FAX.value.length > 0 && checkTelephone(checkForm.FAX.value)==false) {
		alert("FAX number invalid")	
		checkForm.FAX.focus()
		return false
	}
	if (checkForm.email.value.length > 0 && checkEmail(checkForm.email.value)==false) {
		alert("E-mail address invalid")
		checkForm.email.focus()
		return false
	}
	if (checkForm.c_addr1.value.length > 0 && checkInvChars(checkForm.c_addr1.value)==false) {
		alert("Address contains invalid characters")
		checkForm.c_addr1.focus()
		return false
	}	
	if (checkForm.c_addr2.value.length > 0 && checkInvChars(checkForm.c_addr2.value)==false) {
		alert("Address contains invalid characters")
		checkForm.c_addr2.focus()
		return false
	}	
	if (checkForm.c_addr3.value.length > 0 && checkInvChars(checkForm.c_addr3.value)==false) {
		alert("Address contains invalid characters")
		checkForm.c_addr3.focus()
		return false
	}	
	if (checkForm.c_postcode.value.length > 0 && checkInvChars(checkForm.c_postcode.value)==false) {
		alert("Postcode contains invalid characters")
		checkForm.c_postcode.focus()
		return false
	}
	if (checkForm.c_county.value.length > 0 && checkInvChars(checkForm.c_county.value)==false) {
		alert("County contains invalid characters")
		checkForm.c_county.focus()
		return false
	}
	if (checkForm.s_addr1.value.length > 0 && checkInvChars(checkForm.s_addr1.value)==false) {
		alert("Address contains invalid characters")
		checkForm.s_addr1.focus()
		return false
	}	
	if (checkForm.s_addr2.value.length > 0 && checkInvChars(checkForm.s_addr2.value)==false) {
		alert("Address contains invalid characters")
		checkForm.s_addr2.focus()
		return false
	}	
	if (checkForm.s_addr3.value.length > 0 && checkInvChars(checkForm.s_addr3.value)==false) {
		alert("Address contains invalid characters")
		checkForm.s_addr3.focus()
		return false
	}	
	if (checkForm.s_postcode.value.length > 0 && checkInvChars(checkForm.s_postcode.value)==false) {
		alert("Postcode contains invalid characters")
		checkForm.s_postcode.focus()
		return false
	}
	if (checkForm.s_county.value.length > 0 && checkInvChars(checkForm.s_county.value)==false) {
		alert("County contains invalid characters")
		checkForm.s_county.focus()
		return false
	}
	if (checkForm.further_info.value.length > 0 && checkInvChars(checkForm.further_info.value)==false) {
		alert("Further information contains invalid characters")
		checkForm.further_info.focus()
		return false
	}
	if (checkForm.email.value.length < 1 && checkForm.Telephone.value.length < 1) {	
		alert("Please enter an e-mail address or a telephone number")
		checkForm.email.focus()
		return false	
	}
	if (checkForm.email.value.length < 1 && checkForm.Preferred[0].checked==true) {
		alert("Please enter your e-mail address as your preferred method of contact is e-mail")
		checkForm.email.focus()
		return false	
	}
	if (checkForm.Telephone.value.length < 1 && checkForm.Preferred[1].checked==true) {
		alert("Please enter your telephone number as your preferred method of contact is telephone")
		checkForm.Telephone.focus()
		return false	
	}
	if ((checkForm.c_addr1.value.length < 1 || checkForm.c_postcode.value.length < 1) && checkForm.Preferred[2].checked==true) {
		alert("Please enter your company address as your preferred method of contact is mail (minimum first line and postcode)")
		if (checkForm.c_addr1.value.length < 1) {
			checkForm.c_addr1.focus()
		}
		else {
			checkForm.c_postcode.focus()
		}	
		return false	
	}
	return true
}
