//                            Crywild Javascript
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Window Open (wolfctr.shtml)
function openwindow0(){
    window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=500,height=420,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
    }
function openwindow1(){
    window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=500,height=420,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
    }
function openwindow2(){
    window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=500,height=420,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
    }
function openwindow3(){
    window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=500,height=351,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
    }
// Window Open (books.shtml, buybooks.shtml)
function openwindow4(){
    window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=500,height=550,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
    }
// Window Open (gallery.shtml)
function openwindow5(){
 	window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=400,height=330,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
	}
function openwindow6(){
 	window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=400,height=395,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
	}
function openwindow7(){
 	window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=400,height=352,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
	}
function openwindow8(){
 	window.open("","newwindow",'toolbar=0,location=0,scrollbar=0,width=400,height=451,leftmargin=0,topmargin=0,marginwidth=0,marginheight=0,resizable=0,left=50,top=50');
	}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Displays pictures of new books and message every X seconds (index.shtml) called Onload
function NewBooks(m){
    delay = 5000;		// Dealy in mS
    document.getElementById("bookmsg").innerHTML = '<h5>'+"Re-Released Books"+'</h5>';
    if (m == 1) {
        document.getElementById("booktext").innerHTML = '<A href="books.shtml#Cry Wild 05">Cry Wild</A>';
        document.images['bookimage'].src = "book-crywild05.jpg";
        setTimeout("NewBooks(2)",delay);	// Wait & call next
        }
    if (m == 2) {
        document.getElementById("booktext").innerHTML = '<A href="books.shtml#The Natural History of Canada 05">The Natural History of Canada</A>';
        document.images['bookimage'].src = "book-nathistcanada2.jpg";
        setTimeout("NewBooks(3)",delay);	// Wait & call next
        }
    if (m == 3) {
        document.getElementById("booktext").innerHTML = '<A href="books.shtml#The North Runner">The North Runner</A>';
        document.images['bookimage'].src = "book-northrunner.jpg";
        setTimeout("NewBooks(4)",delay);	// Wait & call next
        }
    if (m == 4) {
        document.getElementById("booktext").innerHTML = '<A href="books.shtml#The Place in the Forest">The Place in the Forest</A>';
        document.images['bookimage'].src = "book-intheforest.jpg";
        setTimeout("NewBooks(5)",delay);	// Wait & call next
        }
    if (m == 5) {
        document.getElementById("booktext").innerHTML = '<A href="books.shtml#Where the Water Lilies Grow">Where the Water Lilies Grow</A>';
        document.images['bookimage'].src = "book-wherelilliesgrow.jpg";
        setTimeout("NewBooks(6)",delay);	// Wait & call next
        }
    if (m == 6) {
        document.getElementById("booktext").innerHTML = '<A href="books.shtml#The Ghost Walker 09">The Ghost Walker</A>';
        document.images['bookimage'].src = "book-ghostwalker09.jpg";
        setTimeout("NewBooks(1)",delay);	// Wait & call first
        }
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Contact Form Validation (contactmain.htm)
function ValidateMailForm(){
    var ErrorMsg = "";
    var Name = document.MailForm.Name.value;
    var Email = document.MailForm.Email.value;
    var Body = document.MailForm.Body.value;
    var Subject = GetSelectedListboxItem("MailForm","Subject");
    ErrorMsg=ValidateField(Name,"Name","Mandatory","Bad","|¦<>\\/",ErrorMsg);
    ErrorMsg=ValidateField(Email,"Email","Mandatory","Bad","|¦<>\\/",ErrorMsg);
    ErrorMsg=ValidateField(Body,"Message","Mandatory","Bad","|¦<>\\/",ErrorMsg);
    if (Email != ""){
        ErrorMsg=EmailCheck(Email,ErrorMsg);
        }
    // Display Error messages and return
    ErrorMsg=ErrorMsg.replace(/<br>/gi,"\n");
    if (ErrorMsg != "") {
        alert(ErrorMsg);
        return false;
        }
    else {
        document.MailForm.Valid.value = "2";
        return true;
        }
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Add to Guestbook form validation (addguestmain.htm)
function ValidateAddGuest(){
    var ErrorMsg="";
    var Name = document.AddGuest.Name.value;
    var Email = document.AddGuest.Email.value;
    var Location = document.AddGuest.Location.value;
    var Msg = document.AddGuest.Body.value;

    ErrorMsg=ValidateField(Name,"Name","Mandatory","Bad","|¦<>\\/",ErrorMsg);
    ErrorMsg=ValidateField(Location,"Location","Mandatory","Bad","|¦<>\\/",ErrorMsg);
    ErrorMsg=ValidateField(Msg,"Message","Mandatory","Bad","|¦<>\\/",ErrorMsg);

    if (Email == "" || Email == "Optional"){
        document.AddGuest.Email.value = "Withheld";
        Email = "Withheld";
        }
    // Validate Email address, this will populate errormsg with an ErrorMsg if invalid
    if (Email != "Withheld"){
        ErrorMsg=EmailCheck(Email,ErrorMsg);
        }

    ErrorMsg=SpaceCheck(Msg,ErrorMsg);

    // Display Error messages and return
    ErrorMsg=ErrorMsg.replace(/<br>/gi,"\n");    
    if (ErrorMsg != "") {
        alert(ErrorMsg);
        document.AddGuest.Valid.value = "0";
        document.AddGuest.Email.value = "";
        return false;
        }
    else {
        var MsgTime = DateTime();
        document.AddGuest.DateTime.value = MsgTime;
        document.AddGuest.Valid.value = 2;
        return true;
        }
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Clears Email address field when user clicks in it (text=Optional in html) (addguestmain.htm)
function ClearAddr(){
    document.AddGuest.Email.value="";
    }



// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 				Called Functions
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Validate Specified field
// Param0=field, Param1=Field Name, Param2=Madatory or Optional
// Param3=chars to check, Param4=Errorstring (error gets appended)
function ValidateField(field,fieldtext,ftype,checks,chars,errorstr){
    var valerr="";
    //alert("Checking: Field="+field+" Field Text="+fieldtext+" Type of Test="+ftype+" Chars="+chars+" ErrorStr="+errorstr);
    if (ftype == "Mandatory"){
        if(field == ""){  // Field is mandaory
            var valerr=fieldtext+" Missing<br>";
            }
        else {
            if (checks=="Good"){
                var valerr=GoodCharChk(field,chars,fieldtext,errorstr);
                }
            else {
                var valerr=BadCharChk(field,chars,fieldtext,errorstr);
                }
            }
        }
    else {      // Field is Optional
        if(field != ""){
            if (checks=="Good"){
                var valerr=GoodCharChk(field,chars,fieldtext,errorstr);
                }
            else {
                var valerr=BadCharChk(field,chars,fieldtext,errorstr);
                }
            }
        }
    return errorstr+valerr;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Looks for wanted characters in a string. Returns Error
// Param0=String to check
// Param1=Valid characters that must only exist in String
// Param2=Field Name
function GoodCharChk(strtocheck,GoodChars,fieldname){
    var ErrorString="";
    var strlen=strtocheck.length;
    var goodlen=GoodChars.length;
    var i,s,x,g,m;
    for (i=0;i<strlen;i++) {
        s=strtocheck.charAt(i);
        m=false;
        for (x=0;x<goodlen;x++){
            g=GoodChars.charAt(x);
            if (s == g){
                // Char in string does matches one in GoodChars, breakout
                 m=true;
                 break;
                }
            }
        if (m==false){
            ErrorString="Invalid character found in "+fieldname+". Characters can only be "+GoodChars+"<br>";
            return ErrorString
            }
        }
    return ErrorString;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Looks for unwanted characters in a string. Returns Error
// Param0=String to check
// Param1=Invalid characters that must not exist in String
// Param2=Field Name
function BadCharChk(strtocheck,BadChars,field){
    var ErrorString="";
    var strlen=strtocheck.length;
    var badlen=BadChars.length;
    var i,s,x,b;
    for (i=0;i<strlen;i++) {
        s=strtocheck.charAt(i);
        for (x=0;x<badlen;x++){
            b=BadChars.charAt(x);
            if (s == b){
                // Char in string matches one in BadChars
                ErrorString="Invalid character found in "+field+" ("+BadChars+")<br>";
                return ErrorString;
                }
            }
        }
    return ErrorString;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Email validation function. Plagiarized from http://www.smartwebby.com/dhtml/
// Param0=Email Address to check. Param1=Errorstring (error gets appended)
function EmailCheck(str,errorstr) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        var errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        var errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        var errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    if (str.indexOf(at,(lat+1))!=-1){
        var errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        var errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    if (str.indexOf(dot,(lat+2))==-1){
        var errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    if (str.indexOf(" ")!=-1){
        errormsg = "Invalid EMail address<br>";
        errorstr=errorstr+errormsg;
        return errorstr;
        }
    return errorstr;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Generic Get Selected Listbox Item
// Param0=FormName, Param1=ListboxName
function GetSelectedListboxItem(FormName,ListBox) {
    var len = eval("document."+FormName+"."+ListBox+".length");
    var i = 0;
    var chosen = 0;
    for (i = 0; i < len; i++) {
        if (eval("document."+FormName+"."+ListBox+"[i].selected")) {
            chosen = eval("document."+FormName+"."+ListBox+"[i].value");
            }
        }
    return chosen;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Dynamic Version by: Nannette Thacker http://www.shiningstar.net
// Original by :  Ronnie T. Moore Web Site:  The JavaScript Source
// Slightly modified by the author of this website :-)
function TextCounter() {
    var field = document.AddGuest.Body;
    var maxlimit = 500;
    if (field.value.length > maxlimit){                     // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        }
    else {                                                  // otherwise, update 'characters left' counter
        document.getElementById("Counter").innerHTML = maxlimit-field.value.length;
        }
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Funtion to check for spaces in a string
// Param0=String to check, Param1=Error String
function SpaceCheck(strng,errormsg){
    var num_spaces=0;
    var len=strng.length;
    for (var s = 0; s < len; s++) {
        var eachchar=strng.charAt(s);
        var ret=eachchar.indexOf(" ");
        if (ret != -1){
            var num_spaces = num_spaces+1;
            }
        }
    var divlen = len/25;            // For every 25 chars there should be 1 space
    if ( num_spaces < divlen){
        errormsg=errormsg+"Not enough spaces in your Message\n";
        }
    return errormsg;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Date and time to display on screen
// Plagiarized from http://www.quirksmode.org/js/date.html
function DateTime(){
    var today=new Date();
    var year=TakeYear(today);
    var month=GetMonth(today.getMonth());
    var day=LeadingZero(today.getDate());
    var hours=LeadingZero(today.getHours());
    var mins=LeadingZero(today.getMinutes());
    var time4html=day+'-'+month+'-'+year+' '+hours+':'+mins;
    return time4html;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Finds out the correct year - Used with datetime()
function TakeYear(theDate){
	var x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Adds leading zeros to numbers less than 10 - Used with DateTime()
function LeadingZero(nr){
    if (nr < 10) nr = "0" + nr;
    return nr;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Adds leading zeros to numbers less than 10 - Used with DateTime()
function LeadingZero(nr){
    if (nr < 10) nr = "0" + nr;
    return nr;
    }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get Short Month Name - Used with DateTime()
// Param0=Number of month (0=Jan)
function GetMonth(m) {
    var month=new Array(12);
    month[0]="Jan";
    month[1]="Feb";
    month[2]="Mar";
    month[3]="Apr";
    month[4]="May";
    month[5]="Jun";
    month[6]="Jul";
    month[7]="Aug";
    month[8]="Sep";
    month[9]="Oct";
    month[10]="Nov";
    month[11]="Dec";
    return month[m];
    }
