﻿// Written:     13-OCT-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function load() {
    var nBrowserWidth = 960, nBrowserHeight = 800;

    sBrowser = navigator.appName;

    ShowDateTime();
    
    GetDataFromFileOnServer();

    try {
        GetScreenSize();
        var nBrowserLeft = (screen.width - nBrowserWidth) / 2;
        var nBrowserTop = (screen.height - nBrowserHeight) / 2;
        //window.moveTo(nBrowserLeft, nBrowserTop);
        window.resizeTo(nBrowserWidth, nBrowserHeight);
    }
    catch (e) {
    } 
}

// Written:     22-OCT-2009  Chris Doan
// Last Update: 26-NOV-2009          CD
var nBrowserWidth = 0; nBrowserHeight = 0;
function GetScreenSize() {
    var s = sBrowser.toLowerCase();
    var n = s.indexOf("explorer");
    bIE = n > 0 ? true : false;
    if (bIE) {
        nBrowserWidth = document.body.offsetWidth;
        nBrowserHeight = document.body.offsetHeight;
    }
    else {
        nBrowserWidth = window.innerWidth;
        nBrowserHeight = window.innerHeight;
        mwidth = Math.round(.061 * nBrowserWidth);
        //writeText(DefaultMessage)
    }
}

// Written:     13-OCT-2009  Chris Doan
// Last Update: 13-OCT-2009          CD
function GetDataFromFileOnServer() {
    var httpRequest;

    if (window.XMLHttpRequest) 		// Mozilla, Safari, ...
    {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
            // See note below about this line
        }
    }
    else if (window.ActiveXObject) 	// IE
    {
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
    httpRequest.open('GET', "videotodvd.xml", false); 			//open(mode, url, boolean); mode: type of request, GET or POST; url: the location of the file, with a path; boolean: true (asynchronous) / false (synchronous); optionally, a login and a password may be added to arguments
    httpRequest.send(''); 							//null for a GET command				
}

// Written:     13-OCT-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function alertContents(httpRequest) {
    if (httpRequest.readyState == 4) 		//0: not initialized; 1: connection established;  2: request received; 3: answer in process; 4: finished
    {
        if (httpRequest.status == 200)		//200 is OK
        {
            //alert(httpRequest.responseText);	//holds loaded data as a string of characters
            xmlDoc = httpRequest.responseXML; 	//holds an XML loaded file, DOM's method allows to extract data

            var x = xmlDoc.getElementsByTagName("SOURCE_TYPE");

//            var y = document.getElementById("SOURCE_TYPE");
//            var elemId = document.getElementById('<%=FindControl("SOURCE_TYPE").ClientID%>');

            var form_f1 = document.getElementById("f1");

            for (var i = 0; i < x.length; i++) {
                var option = document.createElement("option");
                ttype = (x[i].getElementsByTagName("TAPE")[0].childNodes[0].nodeValue);
                option.text = ttype;
                option.value = ttype;

                try {
                    if (form_f1 != null)
                        f1.source_type.add(option, null); 	//Standard
                }
                catch (error) {
                    if (form_f1 != null)
                        f1.source_type.add(option); 		// IE only
                }
            }

            var x2;
            if (form_f1 != null)
                x2 = xmlDoc.getElementsByTagName("TRANSFER_TYPE");
            else
                x2 = xmlDoc.getElementsByTagName("DISC_TYPE");
                
            for (var i2 = 0; i2 < x2.length; i2++) {
                var option2 = document.createElement("option");
                stype = (x2[i2].getElementsByTagName("DVD")[0].childNodes[0].nodeValue);
                option2.text = stype;
                option2.value = stype;

                try {
                    if (form_f1 != null)
                            f1.transfer_type.add(option2, null); //Standard
                        else
                            f2.disc_type.add(option2, null); //Standard
                    }
                catch (error) {
                    if (form_f1 != null)
                            f1.disc_type.add(option2); 		// IE only
                        else
                            f2.disc_type.add(option2); 		// IE only
                    }
            }
        }
        else {
            alert('There was a problem with the httpRequest.');
        }
    }
}

// Written:     10-OCT-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function validate_form(form) {
    with (form) {
        if (validate_name(name, "Please enter your name!") == false)
        { name.focus(); return false; }

        if (validate_phone(phone) == false)
        { phone.focus(); return false; }

        if (validate_email(email) == false)
        { email.focus(); return false; }

        var form_f1 = document.getElementById("f1");

        if (form_f1 != null) {
            len = source_type.length;
            var chosen;
            for (i = 0; i < len; i++) {
                if (source_type[i].selected)
                    chosen = source_type[i].value
            }

            if (chosen == "?") {
                alert("What's your tape type?");
                source_type.focus();
                return false;
            }

            len = transfer_type.length;
            for (i = 0; i < len; i++) {
                if (transfer_type[i].selected)
                    chosen = transfer_type[i].value
            }
            if (chosen == "?") {
                alert("What type of DVD transfer type do you want?");
                transfer_type.focus();
                return false;
            }
        }
        else 
        {
            if (validate_channel(channel,"What channel do you want to record?") == false)
            { channel.focus(); return false; }

            if (validate_starttime(start_time,"What time will the programming start?") == false)
            { start_time.focus(); return false; }

            if (validate_stoptime(stop_time, "What time will the programming stop?") == false)
            { stop_time.focus(); return false; }

            len = d_type.length;
            for (i = 0; i < len; i++) {
                if (d_type[i].selected)
                    chosen = d_type[i].value
            }
            if (chosen == "?") {
                alert("What type of disc type do you want?");
                d_type.focus();
                return false;
            }
        }
    }
}


// Written:     11-OCT-2009  Chris Doan
// Last Update: 19-OCT-2009          CD
function GetSelectedItem() {
    return;

    var sel = document.getElementById("s_type");
    for (var i = 0; i < sel.options.length; i++) {
        if (sel.options[i].selected)
            alert(sel.options[i].text);
    }
}

// Written:     10-OCT-2009  Chris Doan
// Last Update: 20-OCT-2009          CD
function validate_email(field) {
    with (field) {
        if (value == null || value == "")			//Check for blank e-mail address
        {
            alert("Please enter your E-mail address");
            return false;
        }

        apos = value.indexOf("@"); 			//Check for validated e-mail address
        dotpos = value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2) {
            alert("Not a valid e-mail address!");
            return false;
        }
        else {
            return true;
        }
    }
}

// Written:     13-DEC-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function validate_channel(field, alerttxt) {
    with (field) {
        if (value == null || value == "")			//Check for blank channel
        {
            alert(alerttxt);
            return false;
        }
    }
}

// Written:     13-DEC-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function validate_time(sTimeStr) {
    //var objRegExp = /^((\d)|(0\d)|(1\d)|(2[0-3]))\:((\d)|([0-5]\d))$/;   //hh:mm:ss
    var objRegExp = /^((\d)|(0\d)|(1\d)|(2[0-3]))\:((\d)|([0-5]\d))$/;     //hh:mm
    return objRegExp.test(sTimeStr);
}

// Written:     13-DEC-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function validate_starttime(field, alerttxt) {
    with (field) {
        if (value == null || value == "")			//Check for start time
        {
            alert(alerttxt);
            return false;
        }

        if (validate_time(value) == false) {
            alert("Please Enter a Valid Start Time")
            value = ""
            return false
        }
        return true        
    }
}

// Written:     13-DEC-2009  Chris Doan
// Last Update: 13-DEC-2009          CD
function validate_stoptime(field, alerttxt) {
    with (field) {
        if (value == null || value == "")			//Check for stop time
        {
            alert(alerttxt);
            return false;
        }

        if (validate_time(value) == false) {
            alert("Please Enter a Valid Stop Time")
            value = ""
            return false
        }
        return true        
    }
}

// Written:     10-OCT-2009  Chris Doan
// Last Update: 11-OCT-2009          CD
function validate_name(field, alerttxt) {
    with (field) {
        if (value == null || value == "")			//Check for blank name
        {
            alert(alerttxt);
            return false;
        }
    }
}

// Written:     10-OCT-2009  Chris Doan
// Last Update: 11-OCT-2009          CD
function validate_phone(field, alerttxt) {
    with (field) {
        if (value == null || value == "")			//Check for blank phone number
        {
            alert("Please Enter your Phone Number");
            return false;
        }

        if (checkInternationalPhone(value) == false) {
            alert("Please Enter a Valid Phone Number")
            value = ""
            return false
        }
        return true
    }
}

// DHTML phone number validation script. 

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

// Written:     10-OCT-2009  Chris Doan
// Last Update: 11-OCT-2009          CD
function checkInternationalPhone(strPhone) {
    var bracket = 3
    strPhone = trim(strPhone)
    if (strPhone.indexOf("+") > 1) return false
    if (strPhone.indexOf("-") != -1) bracket = bracket + 1
    if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) return false
    var brchr = strPhone.indexOf("(")
    if (strPhone.indexOf("(") != -1 && strPhone.charAt(brchr + 2) != ")") return false
    if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) return false
    s = stripCharsInBag(strPhone, validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


// Written:     10-OCT-2009  Chris Doan
// Last Update: 11-OCT-2009          CD
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;
}

// Written:     10-OCT-2009  Chris Doan
// Last Update: 11-OCT-2009          CD
function trim(s) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ")
            returnString += c;
    }
    return returnString;
}

// Written:     10-OCT-2009  Chris Doan
// Last Update: 11-OCT-2009          CD
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++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
// Written:     13-OCT-2009  Chris Doan
// Last Update: 22-OCT-2009          CD
function ShowDateTime() {
    if (!document.all && !document.getElementById)
        return;

    thelement = document.getElementById("datetime");
    var Digital = new Date();
    var year = Digital.getFullYear();
    var date = Digital.getDate();
    var hours = Digital.getHours();
    var minutes = Digital.getMinutes();
    var seconds = Digital.getSeconds();
    var dn = "PM";
    if (hours < 12)
        dn = "AM";
    if (hours > 12)
        hours = hours - 12;
    if (hours == 0)
        hours = 12
    if (minutes <= 9)
        minutes = "0" + minutes
    if (seconds <= 9)
        seconds = "0" + seconds;
    var ctime = hours + ":" + minutes + ":" + seconds + " " + dn;

    var month = new Array(12);
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";
    m = month[Digital.getMonth()];

    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    wd = weekday[Digital.getDay()];

    thelement.innerHTML = "<span style = 'font-size:14;color:navy;position:absolute;right:20px'>" + ctime + " on " + wd + ", " + m + " " + date + ", " + year + "</span>";
    setTimeout("ShowDateTime()", 1000);
}

