﻿//////////////////////////////////////////////////////////////////////////
// TITLE:		Contact Us page scripts  								//
// PURPOSE:		Query string used for Application Description input.    //
//              Cookie to save user values.                             //
//              Recognize query strings and modify:                     //
//                  Banner image                                        //
//                  Contact Form Heading                                //
//                  Button Text                                         //
//                  Destination Web page                                //
//																		//
// FUNCTIONS:	VerifyFormSave - calls VerifyForm and saves cookie		//
//				serCookie - generic function to set a cookie value		//
//				getCookie - generic function to get a particular cookie //
//				saveusercookie - saves the cookie values for ContactUs	//
//				getusercookie - retrieves cookie values for ContactUs	//
//				initpage - get and recognize query string values    	//
//																		//
// VERSION:		1.00													//
// DATE:		May 19, 2007											//
//																		//
//				© Copyright Measurlogic 2003							//
//																		//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// NAME:        queryparseinfo                                          //
// PURPOSE:     Object to store information for changes based on query  //
//																		//
// PARAMETERS:  Matchstring - string to match in query string           //
//              Bannerimageurl - url of the banner image to use         //
//              Contactformtext - text to head the contact form         //
//              Buttontext - text for the submit button                 //
//              Destinationurl - url after successful submission        //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     initpage, setpageinfo                                   //
//																		//
//////////////////////////////////////////////////////////////////////////
function queryparseinfo(Matchstring, Bannerimageurl, 
        Contactformtext, Buttontext, Destinationurl)
{
    this.matchstring = Matchstring;         // Query string to match
    this.bannerimageurl = Bannerimageurl;   // URL of banner image to use 
    this.contactformtext = Contactformtext; // Header text to use
    this.buttontext = Buttontext;           // Text to place on button
    this.destinationurl = Destinationurl;   // Destination after submission
}	

//////////////////////////////////////////////////////////////////////////
// Page Global variables                                                //
//////////////////////////////////////////////////////////////////////////

// pageinfo contains an array of queryparseinfo with all the matching
// query strings and customisation information
var pageinfo = new Array();

    pageinfo["DPI Selector"] = new queryparseinfo(
                                "DPI Selector",
                                "Resources/dpiinforequest.gif",
                                "DPI Information Request",
                                "Request DPI Information",
                                "Info/requestsent.html");

    pageinfo["PowerStudio Guided Tour Access"] = new queryparseinfo(
                                "PowerStudio Guided Tour",
                                "Resources/sendpowerstudioto.gif",
                                "PowerStudio Guided Tour Access",
                                "Request Access to Tour",
                                "Info/touraccess.html");

    pageinfo["PowerStudio Online Demo Access"] = new queryparseinfo(
                                "PowerStudio Online Demo Access",
                                "Resources/powerstudiodemo.gif",
                                "PowerStudio Demo Access",
                                "Request Access to Demo",
                                "Info/demoaccess.html");
                                
    pageinfo["Documentation Access Request"] = new queryparseinfo(
                                "Documentation Access Request",
                                "Resources/documentation.gif",
                                "Documentation Access Request:",
                                "Request Access",
                                "Info/docaccess.html");

//////////////////////////////////////////////////////////////////////////
// NAME:        setpageinfo                                             //
// PURPOSE:     Set up the page according to the myinfo parameter       //
//																		//
// PARAMETERS:  myinfo - contains the page setting information          //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     matchquerystring                                        //
//																		//
//////////////////////////////////////////////////////////////////////////
function setpageinfo(myinfo)
{
    //alert("in setpageinfo with " + myinfo.matchstring);
    
    NamedElements = window.document.getElementsByName("bannerimage");
    //alert (NamedElements[0].src);
    NamedElements[0].src = myinfo.bannerimageurl;
    
    contactheader = window.document.getElementById("headertext");
    //alert(contactheader.innerHTML);
    contactheader.innerHTML = myinfo.contactformtext;
    
    NamedElements = window.document.getElementsByName("destinationurl");
    //alert (NamedElements[0].value);
    NamedElements[0].value = myinfo.destinationurl;
    
    NamedElements = window.document.getElementsByName("submitbutton");
    //alert (NamedElements[0].value);
    NamedElements[0].value = myinfo.buttontext;
}

//////////////////////////////////////////////////////////////////////////
// NAME:        matchquerystring                                        //
// PURPOSE:     Try to match the query string against known queries     //
//              If there is a match, set up the page parameters         //
//																		//
// PARAMETERS:  none                                                    //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     initpage                                                //
//																		//
//////////////////////////////////////////////////////////////////////////
function matchquerystring()
{
    var qry, re, matched, pinfo, contactheader;
    
    // if there is a query string
	if (location.search.length != 0)
	{
	    qry = unescape(location.search.substr(1, location.search.length - 1));
	    matched = false;
	    
        // check all know query strings for a match
        for (qrymatch in pageinfo)
        {
            //alert("Check " + qrymatch + " in " + qry);
    	    re = new RegExp(pageinfo[qrymatch].matchstring);
            if(qry.match(re))
            {
                //alert("Matched " + qrymatch);
                pinfo = pageinfo[qrymatch];
                matched = true;
            }
            else
            {
                //alert("Not matched");
            }
        }
        
        if(matched)
        {
            //alert("set up page information for " + pinfo.matchstring);
            setpageinfo(pinfo);
        }
        
	    // get the Application Description form area (assuming it is the only element
	    // named Application Description).
		NamedElements = window.document.getElementsByName("Application Description");
		NamedElements[0].value = "";
		
		NamedElements[0].value = qry;
	}
}





//////////////////////////////////////////////////////////////////////////
// NAME:        VerifyFormSave                                          //
// PURPOSE:     It calls the existing VerifyForm function and if the    //
//              form verifies correctly, saves the user cookie before   //
//              returning the result of the verification result         //
//																		//
// PARAMETERS:  formName - form to verify                               //
//																		//
// RETURNS:     true if form verifies correctly else false              //
//																		//
// USED BY:     ContactUs form to verify form and save cookie           //
//																		//
//////////////////////////////////////////////////////////////////////////
function VerifyFormSave(formName)
{
    var result
  
    result = VerifyForm(formName);  // Call the original VerifyForm code
    if (result)
    {
        saveusercookie();   // Save all the user information to cookies
    }
    
    //NamedElements = window.document.getElementsByName("destinationurl");
    //alert (NamedElements[0].value);
   
    
    return result;
}

//////////////////////////////////////////////////////////////////////////
// NAME:        setCookie                                               //
// PURPOSE:     Generic function called to set a cookie value           //
//																		//
// PARAMETERS:  c_name - name of cookie to set                          //
//              value - value of cookie to save                         //
//              expiredays - number of days after which cookie expires  //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     saveusercookie function                                 //
//																		//
//////////////////////////////////////////////////////////////////////////
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

//////////////////////////////////////////////////////////////////////////
// NAME:        getCookie                                               //
// PURPOSE:     Generic function called to get a cookie value           //
//																		//
// PARAMETERS:  c_name - name of cookie to get                          //
//																		//
// RETURNS:     value of cookie if it exists or empty string            //
//																		//
// USED BY:     saveusercookie function                                 //
//																		//
//////////////////////////////////////////////////////////////////////////
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

//////////////////////////////////////////////////////////////////////////
// NAME:        saveusercookie                                          //
// PURPOSE:     save the contactus cookie information                   //
//																		//
// PARAMETERS:  none                                                    //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     VerifyFormSave function                                 //
//																		//
//////////////////////////////////////////////////////////////////////////
function saveusercookie()
{
	var
		NamedElements   // Array of HTML elements with name given

    // local values for the elements that will be saved to cookie values	
    var
		companyname, contactperson, titleindustry, telephone, fax,
		email, address1, address2, city, stateprov, zip, country

    // Get all HTML elements with names that match the required elements in the form
    // ASSUME that there is only one element with this name so t will be element 0 
    NamedElements = window.document.getElementsByName("Company Name");
    companyname = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Contact Person");
    contactperson = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Industry");
    titleindustry = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Telephone Number");
    telephone = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Fax Number");
    fax = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("email");
    email = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Address1");
    address1 = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Address2");
    address2 = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("City");
    city = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("State / Province");
    stateprov = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Zip / Postal Code");
    zip = NamedElements[0].value;
    NamedElements = window.document.getElementsByName("Country");
    country = NamedElements[0].value;
	
	// Set the various cookie values with 365 day expiry
	setCookie("CompanyName", companyname, 365);
	setCookie("ContactPerson", contactperson, 365);
	setCookie("TitleIndustry", titleindustry, 365);
	setCookie("Telephone", telephone, 365);
	setCookie("Fax", fax, 365);
	setCookie("email", email, 365);
	setCookie("Address1", address1, 365);
	setCookie("Address2", address2, 365);
	setCookie("City", city, 365);
	setCookie("StateProv", stateprov, 365);
	setCookie("Zip", zip, 365);
	setCookie("Country", country, 365);
	
}

//////////////////////////////////////////////////////////////////////////
// NAME:        getusercookie                                           //
// PURPOSE:     retrieve the contactus cookie information               //
//																		//
// PARAMETERS:  none                                                    //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     initpage function                                       //
//																		//
//////////////////////////////////////////////////////////////////////////
function getusercookie()
{
    var myCookie

    var
		companyname, contactperson, titleindustry, telephone, fax,
		email, address1, address2, city, stateprov, zip, country
		
    companyname = getCookie("CompanyName");
    contactperson = getCookie("ContactPerson");
    titleindustry = getCookie("TitleIndustry");
    telephone = getCookie("Telephone");
    fax = getCookie("Fax");
    email = getCookie("email");
    address1 = getCookie("Address1");
    address2 = getCookie("Address2");
    city = getCookie("City");
    stateprov = getCookie("StateProv");
    zip = getCookie("Zip");
    country = getCookie("Country");

    NamedElements = window.document.getElementsByName("Company Name");
    NamedElements[0].value = companyname;
    NamedElements = window.document.getElementsByName("Contact Person");
    NamedElements[0].value = contactperson;
    NamedElements = window.document.getElementsByName("Industry");
    NamedElements[0].value = titleindustry;
    NamedElements = window.document.getElementsByName("Telephone Number");
    NamedElements[0].value = telephone;
    NamedElements = window.document.getElementsByName("Fax Number");
    NamedElements[0].value = fax;
    NamedElements = window.document.getElementsByName("email");
    NamedElements[0].value = email;
    NamedElements = window.document.getElementsByName("Address1");
    NamedElements[0].value = address1;
    NamedElements = window.document.getElementsByName("Address2");
    NamedElements[0].value = address2;
    NamedElements = window.document.getElementsByName("City");
    NamedElements[0].value = city;
    NamedElements = window.document.getElementsByName("State / Province");
    NamedElements[0].value = stateprov;
    NamedElements = window.document.getElementsByName("Zip / Postal Code");
    NamedElements[0].value = zip;
    NamedElements = window.document.getElementsByName("Country");
    NamedElements[0].value = country;
}

//////////////////////////////////////////////////////////////////////////
// NAME:        initpage                                                //
// PURPOSE:     get the cookie inforamtion                              //
//              get the query string information                        //
//              set the Application Description information             //
//              recognize the query strings                             //
//              change banner image, contact form header, button text   //
//              and destination page depending on query string          //
//																		//
// PARAMETERS:  none                                                    //
//																		//
// RETURNS:     nothing                                                 //
//																		//
// USED BY:     initpage function                                       //
//																		//
//////////////////////////////////////////////////////////////////////////
function initpage()
{
    var qry, appdes, NamedElements;
    
    getusercookie();    // get cookie information for user
    
    matchquerystring();
    
    // if there is a query string
//	if (location.search.length != 0)
//	{
//	    // get the Application Description form area (assuming it is the only element
//	    // named Application Description).
//		NamedElements = window.document.getElementsByName("Application Description");
//		NamedElements[0].value = "";
//		
//		qry = location.search.substr(1, location.search.length - 1);
//		appdes = qry;
//		appdes = appdes.replace(/%20/gi, " ");
//		NamedElements[0].value = appdes;
//		//window.document.getElementById("Application Description").value = appdes;
//	}
}

//////////////////////////////////////////////////////////////////////////
//				© Copyright Measurlogic 2003, 2007						//
//////////////////////////////////////////////////////////////////////////
