/** Copyright Notice =========================================================*
  *
  * This file contains proprietary information.
  * Copying or reproduction without prior written approval is prohibited.
  * All rights reserved.
  *
  * Document Information =====================================================*
  *
  * @filename       mi.main.js
  * @version        1.0
  * @description    Middleware Interceptor
  * @date           2004/05/21
  * @author         Rick Farmer
  *
  * @revised        2004/05/26
  * @author         Rick Farmer
  * @revision       Added MI functional code
  *
  * @revised        2004/06/07
  * @author         Rick Farmer
  * @revision       Added AF functional code
  *
  * Copyright (c) 2004 =======================================================*/

    function oMI(){  // MI object definition

        this.DD = new Array();  // the dropdown object array
        
        // method --> add a Dropdown object to the hierarchy
        this.addDD = function(title, oID, jsObj, DD, custfield, article){
            this.DD.push(new oDD(title, oID, jsObj, DD, custfield, article));
            }
            
        // child Objects --> methods & properties available to the oMI parent object
        function oDD(title, oID, jsObj, DD, custfield, article) { // An Object which contains the dropdowns

                            this.title                      = new String(title);
                            this.oID                        = new String(oID);
                            this.DD                         = new Array();
                            this.custfield                  = new oCustField();
                            this.article                    = new oArticle();
                            this.jsObj                      = new String(jsObj);

                            // Public methods()
                            this.addDD = function(title, oID, jsObj, DD, custfield, article){
                                this.DD.push(new oDD(title, oID, jsObj, DD, custfield, article));
                            }

                            this.addCustfield = function(name, label){
                                this.custfield.name         = name;
                                this.custfield.label        = label;
                            }

                            this.addArticle = function(id,route_email,route_chat,nomap,chatOnlySuccessMsgTo){
                                this.article.id                     = id;
                                this.article.route_email            = route_email;
                                this.article.route_chat             = route_chat;
                                this.article.nomap                  = nomap;
                                this.article.chatOnlySuccessMsgTo   = chatOnlySuccessMsgTo;
                            }
        }

        function oCustField(name, label) { // An Object which contains the custom fields
                            this.name                       = new String(name);
                            this.label                      = new String(label);
        }

        function oArticle(id,route_email,route_chat,nomap,AFs,chatOnlySuccessMsgTo) { // An Object which contains the Article data
                            this.id                         = new String(id);
                            this.route_email                = new String(route_email);
                            this.route_chat                 = new String(route_chat);
                            this.nomap                      = new String(nomap);
                            this.AFs                        = new Array();
                            this.chatOnlySuccessMsgTo       = new String(chatOnlySuccessMsgTo);

                            this.addAFs = function(grp,item){
                                this.AFs.push(new oAFs(grp, item));
                            }
        }

        function oAFs(grp, item) { // The DD version of the Object which contains the Additional fields
                            this.group  = new String(grp);
                            this.item   = new String(item);
        }

        // The Addtional Fields object code - which is linked at runtime to the DD AFs

        this.AF         = new Array()

        this.addAFgroup = function(grp, item, text){
            this.AF[grp] = new Array();
            this.AF[grp][item].push(new oAF(text));
        }

        function oAF(text){  // the Additional Fields object array
            this.text = text;
            //this.item[item] = text;  // the item object array
        }
    }

    function validate(){
        var atChar = "@";
        var dotChar = ".";
        var email = this.document.theForm.KR_Email.value;
        var altemail = this.document.theForm.KR_AlternativeEmail.value;
            if (this.document.theForm.KR_Name.value == "" && this.document.theForm.KR_Email.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Name and Your EarthLink Email Address");
                this.document.theForm.KR_Name.focus();
                return false;
            }
            if (this.document.theForm.KR_Name.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Name");
                this.document.theForm.KR_Name.focus();
                return false;
            }
            if (this.document.theForm.KR_Email.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Your EarthLink Email Address");
                this.document.theForm.KR_Email.focus();
                return false;
            } else {
                var atIndex = email.indexOf(atChar);
                var dotIndex = email.indexOf(dotChar);
                var atIndex2 = altemail.indexOf(atChar);
                var dotIndex2 = altemail.indexOf(dotChar);
                if(atIndex == -1 || atIndex == 0){
                    alert("Please enter a valid email address for example johndoe@earthlink.net.");
                    this.document.theForm.KR_Email.focus();
                    return false;
                }
                if(dotIndex == -1 || dotIndex == 0){
                    alert("Please enter a valid email address for example johndoe@earthlink.net.");
                    this.document.theForm.KR_Email.focus();
                    return false;
                }
                if(document.theForm.KR_AlternativeEmail.value.length > 0 ){
                    if(atIndex2 == -1 || atIndex2 == 0){
                        alert("Please enter a valid email address for example johndoe@earthlink.net.");
                        this.document.theForm.KR_AlternativeEmail.focus();
                        return false;
                    }
                    if(dotIndex2 == -1 || dotIndex2 == 0){
                        alert("Please enter a valid email address for example johndoe@earthlink.net.");
                        this.document.theForm.KR_AlternativeEmail.focus();
                        return false;
                    }
                }
            }
        miSubmit();
        return true;

    }

    function miSubmit(){
              
        var url = new String();
        var isNoMap = (this.document.theForm.isNoMap.value == "1")?true:false;
        //debug(isNoMap);
        var bypassThrottle = (this.document.theForm.bypassThrottle.value == "1")?true:false;
        //debug(bypassThrottle);
        var isPhone    = (this.document.theForm.switchTo.value == "phone")?true:false;
        //debug(isPhone);
        if((!isNoMap && isThrottled) || (bypassThrottle) || (isPhone)){

                url = "mi.case.asp";

        } else {

                url = "mi.mail.asp";

                if(this.document.theForm.route.value=="chat") {

                    wNewWindow = window.open('','chat','width=472,height=320,status=no,scrollbars=no,toolbar=no');
                    //wNewWindow = window.open('','chat','width=472,height=320,status=no,resizable=no,scrollbars=no,location=no,toolbar=no');
                    wNewWindow.document.open();
                    wNewWindow.document.write("<html><head></head><body><strong>Hello!  Starting chat session... Please wait.</strong></body></html>");
                    wNewWindow.document.close();
                   
                }
        }

        	this.document.theForm.action=url;
        	this.document.theForm.method='POST';
    	    this.document.theForm.submit();
    }
    
    function validateFraud(){

        var atChar = "@";
        var dotChar = ".";
        var email = this.document.theForm.KR_Email.value;
            if (this.document.theForm.KR_Name.value == "" && this.document.theForm.KR_Email.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Name and Your EarthLink Email Address");
                this.document.theForm.KR_Name.focus();
                return false;
            }
            if (this.document.theForm.KR_Name.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Name");
                this.document.theForm.KR_Name.focus();
                return false;
            }
            if (this.document.theForm.KR_Email.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Your EarthLink Email Address");
                this.document.theForm.KR_Email.focus();
                return false;
            } else {
                var atIndex = email.indexOf(atChar);
                var dotIndex = email.indexOf(dotChar)
                if(atIndex == -1 || atIndex == 0){
                    alert("Please enter a valid email address for example johndoe@earthlink.net.");
                    this.document.theForm.KR_Email.focus();
                    return false;
                }
                if(dotIndex == -1 || dotIndex == 0){
                    alert("Please enter a valid email address for example johndoe@earthlink.net.");
                    this.document.theForm.KR_Email.focus();
                    return false;
                }
            }
        fraudmiSubmit();
        return true;

    }

    function fraudmiSubmit(){
        
        var url = new String();
        var isNoMap = (this.document.theForm.isNoMap.value == "1")?true:false;
        //debug(isNoMap);
        var bypassThrottle = (this.document.theForm.bypassThrottle.value == "1")?true:false;
        //debug(bypassThrottle);
        var isPhone    = (this.document.theForm.switchTo.value == "phone")?true:false;
        //debug(isPhone);
        if((!isNoMap && isThrottled) || (bypassThrottle) || (isPhone)){

                url = "fraudmi.case.asp";

        } else {

                url = "mi.mail.asp";

                if(this.document.theForm.route.value=="chat") {

                    wNewWindow = window.open('','chat','width=472,height=320,status=no,scrollbars=no,toolbar=no');
                    //wNewWindow = window.open('','chat','width=472,height=320,status=no,resizable=no,scrollbars=no,location=no,toolbar=no');
                    wNewWindow.document.open();
                    wNewWindow.document.write("<html><head></head><body><strong>Starting chat session... Please wait.</strong></body></html>");
                    wNewWindow.document.close();
                }
        }

        	this.document.theForm.action=url;
        	this.document.theForm.method='POST';
    	    this.document.theForm.submit();
    }

  
    function validateMac(){
        var atChar = "@";
        var dotChar = ".";
        var email = this.document.theForm2.KR_Email.value;
            if (this.document.theForm2.KR_Name.value == "" && this.document.theForm2.KR_Email.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Name and Your EarthLink Email Address");
                this.document.theForm2.KR_Name.focus();
                return false;
            }
            if (this.document.theForm2.KR_Name.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Name");
                this.document.theForm2.KR_Name.focus();
                return false;
            }
            if (this.document.theForm2.KR_Email.value == ""){
                alert("The following required fields were left blank. Please enter the requested information to continue." + " Your EarthLink Email Address");
                this.document.theForm2.KR_Email.focus();
                return false;
            } else {
                var atIndex = email.indexOf(atChar);
                var dotIndex = email.indexOf(dotChar)
                if(atIndex == -1 || atIndex == 0){
                    alert("Please enter a valid email address for example johndoe@earthlink.net.");
                    this.document.theForm2.KR_Email.focus();
                    return false;
                }
                if(dotIndex == -1 || dotIndex == 0){
                    alert("Please enter a valid email address for example johndoe@earthlink.net.");
                    this.document.theForm2.KR_Email.focus();
                    return false;
                }
            }
        miSubmitMac();
        return true;

    }

    function miSubmitMac(){
              
        var url = new String();
        url = "fraudmi.case.asp";

        	this.document.theForm2.action=url;
        	this.document.theForm2.method='POST';
    	    this.document.theForm2.submit();
    }


    function convertEntities(CDATA)
/**==========================================================================*
  * @purpose
  *     Converts CDATA content to XML entities while preserving HTML entities
  *
  * @dependancies
  *     none
  *
  * @param      CDATA - the XML CDATA section to convert to XML entities
  *
  * @return     A String containing CDATA converted to XML entities
  *
  * @author     Rick Farmer
  * @date       9/30/2003
  *==========================================================================*/
{
    // convert the existing content entities to real characters
	CDATA = CDATA.replace(/&amp;/gi,"&");  // replace all & with &amp;
	CDATA = CDATA.replace(/&lt;/gi,"<");  // replace all < with &lt;
	CDATA = CDATA.replace(/&gt;/gi,">");  // replace all > with &gt;
	CDATA = CDATA.replace(/&apos;/gi,"\'");  // replace all ' with &apos;
	CDATA = CDATA.replace(/&quot;/gi,"\"");  // replace all ' with &quot;

    // There are five pre-defined XML entities, because the & indicates the start of an entity
    // we must replace it first.
	CDATA = CDATA.replace(/&/gi,"&amp;");  // replace all & with &amp;
	CDATA = CDATA.replace(/</gi,"&lt;");  // replace all < with &lt;
	CDATA = CDATA.replace(/>/gi,"&gt;");  // replace all > with &gt;
	CDATA = CDATA.replace(/\'/gi,"&apos;");  // replace all ' with &apos;
	CDATA = CDATA.replace(/\"/gi,"&quot;");  // replace all ' with &quot;

	return(CDATA);
}

