// JavaScript Document
/**
 @@
 @@ This sets the display styles for the pop up window
 @@
 @@ @param: none
 @@
 */
//bgred is for part type (transmitter, receiver, etc)
bgred ="<font size='3' color='#000000' face='Arial, Helvetica, sans-serif'><b>" 

//bgblue is for header
bgblue ="<font size='3' color='#555555' face='Arial, Helvetica, sans-serif'><b>"

//rgred is generated data.
rgred="<font size='2' color='#000000' face='Arial, Helvetica, sans-serif'>"

//rgblue is row titles.
rgblue="<font size='2' color='#555555' face='Arial, Helvetica, sans-serif'>"

bgoff = "</b></font><p>"

tomail = "n" 
displayed = "n"


/**
 @@
 @@ Generates an array to assemble the part numbers AND checks to make sure an option
 @@ has been selected for each dropdown otherwise generates error message.
 @@
 @@ @param:
 @@
 */
function check_selects(form1)
{
	// set the default error message
    emptyerr = "Please select =-> ";  //default message to be appended to
    formObj = document.form1;
      
// create array of names and their descriptions and check
    names = new Array(9)
	names[0] = "fiber*No. of Fibers"
	names[1] = "casestyle*Case Style"
	names[2] = "opmode*Operating Mode"
	names[3] = "connector*Connector Type"
	names[4] = "video*Video Option"
	names[5] = "audio*Audio Option"
	names[6] = "audiost*Audio Strapping"
	names[7] = "data1*Data Option1"
	names[8] = "data2*Data Option2"
		
// split the array and make sure user has selected all options from dropdowns otherwise throw error
	 for (i=0; i<9; i++) 
	 { 
     parts = names[i].split("*")              //split names into parts array
     curpart = parts[0]                       //store current parts info
	 curdescrip = parts[1]                    //store part description
  	 selnum = formObj[curpart].selectedIndex  //store the selected index     
     if (selnum == 0)                         //if it's still default, give error
	 {alert(emptyerr + " " + curdescrip + "  <-=      ")
     formObj[curpart].focus();                // focus on errored field
     return false;}
	}
	
	return true;
}
/**
 @@
 @@ Creates initial 3 character part numbers
 @@
 @@ @param: Pass it Rx, Tx or Tc
 @@
 */
function inipartnums() 
{
	 switch( p_type )
	{
		case 'Rx' :	
		    partnum = "F1R-";
    		part_type = "Receiver";
			comppart = "F1T-";
			break;
		
		case 'Tx' :
		    partnum = "F1T-";
    		part_type = "Transmitter";
			comppart = "F1R-";
			break;
		
		case 'Tc' :
		    partnum = "F1X-";
    		part_type = "Transceiver";
			comppart = "F1X-";
			break;
	}
}

/**
 @@
 @@ Begin building the actual part number
 @@ This works for rcvr, tmit and tcvr)
 @@ @param: 'direction'  either 'forward" or 'reverse'
 @@
 */

function makepartnumF() 
{
    partlist = bgblue + "Pure Digital Fiberlink Flex System " + bgred + part_type + bgoff

    names = new Array(9)
	names[0] = "fiber*No. of Fibers"
	names[1] = "casestyle*Case Style"
	names[2] = "opmode*Operating Mode"
	names[3] = "connector*Connector Type"
	names[4] = "video*Video Option"
	names[5] = "audio*Audio Option"
	names[6] = "audiost*Audio Strapping"
	names[7] = "data1*Data Option1"
	names[8] = "data2*Data Option2"

	// split the array
 	for (i=0; i<9; i++) 
	{
	    parts = names[i].split("*")
	    curpart = parts[0]
	    curdescrip = parts[1]
	  	opnum = formObj[curpart].selectedIndex     //set the selected index number
	    partnum += formObj[curpart][formObj[curpart].selectedIndex].value.charAt(0);    // add to the part number
	    partlist += rgblue+"<b>" + curdescrip + ":</b> " + rgred + formObj[curpart][formObj[curpart].selectedIndex].value.substring(2,40) + "<br>";

		if (curpart == "connector" | curpart == "audiost")  //insert dashes at right place
		{ partnum += "-" };
 	}

	switch( direction )
	{
		case 'reverse' :
		//store COMP variables
	    compfiber = formObj.fiber[formObj.fiber.selectedIndex].value.charAt(0);  //same fiber type
	    compcase = formObj.compcase[formObj.compcase.selectedIndex].value.charAt(0);  //case sytle
	    compopmode1 = formObj.opmode[formObj.opmode.selectedIndex].value.charAt(0);  //same opmode
	    compconn = formObj.connector[formObj.connector.selectedIndex].value.charAt(0);  //same connector
	    compaud1 = formObj.audio[formObj.audio.selectedIndex].value.charAt(0);    //same audio
	    compvid1 = formObj.video[formObj.video.selectedIndex].value.charAt(0);    //same video
	    compaudst = formObj.compaudst[formObj.compaudst.selectedIndex].value.charAt(0);
	    compdata1 = formObj.compdata1[formObj.compdata1.selectedIndex].value.charAt(0);
	    compdata2 = formObj.compdata2[formObj.compdata2.selectedIndex].value.charAt(0);
		break;
		
		case 'forward' :

		//store COMP variables
	    compfiber = formObj.fiber[formObj.fiber.selectedIndex].value.charAt(0);  	//same fiber type
	    compopmode = formObj.opmode[formObj.opmode.selectedIndex].value.charAt(0);  //same opmode
	    compaud = formObj.audio[formObj.audio.selectedIndex].value.charAt(0);    	//same audio
	    compvid = formObj.video[formObj.video.selectedIndex].value.charAt(0);    	//same video
	    compaudst = formObj.compaudst[formObj.compaudst.selectedIndex].value.charAt(0);
	    compdata1 = formObj.compdata1[formObj.compdata1.selectedIndex].value.charAt(0);
	    compdata2 = formObj.compdata2[formObj.compdata2.selectedIndex].value.charAt(0);
	

	}
}

/**
 @@
 @@ Begin building the actual part number
 @@ This works for rcvr and tmit REVERSE only)
 @@ @param: 
 @@
 */
function makepartnumR() 
{
    partlist = bgblue + "Flex " + bgred + part_type + bgoff

 	for (i=0; i<9; i++) { 
     parts = names[i].split("*")
     curpart = parts[0]
     curdescrip = parts[1]
  	 opnum = formObj[curpart].selectedIndex     //set the selected index number
     partnum += formObj[curpart][formObj[curpart].selectedIndex].value.charAt(0);    // add to the part number
     partlist += rgblue+"<b>" + curdescrip + ":</b> " + rgred + formObj[curpart][formObj[curpart].selectedIndex].value.substring(2,40) + "<br>";

	// insert dashes
	 if (curpart == "connector" | curpart == "audiost")
	 {
	 	partnum += "-"};
 	 }

	//store COMP variables
    compfiber = formObj.fiber[formObj.fiber.selectedIndex].value.charAt(0);  //same fiber type
    compcase = formObj.compcase[formObj.compcase.selectedIndex].value.charAt(0);  //case sytle
    compopmode = formObj.opmode[formObj.opmode.selectedIndex].value.charAt(0);  //same opmode
    compconn = formObj.connector[formObj.connector.selectedIndex].value.charAt(0);  //same connector
    compaud = formObj.audio[formObj.audio.selectedIndex].value.charAt(0);    //same audio
    compvid = formObj.video[formObj.video.selectedIndex].value.charAt(0);    //same video
    compaudst = formObj.compaudst[formObj.compaudst.selectedIndex].value.charAt(0);
    compdata1 = formObj.compdata1[formObj.compdata1.selectedIndex].value.charAt(0);
    compdata2 = formObj.compdata2[formObj.compdata2.selectedIndex].value.charAt(0);
	
	// check for "Same as Main" selection
	//if "same as main is selected, use it otherwise get new selection
    if (compaudst == "X")
    {compaudst = formObj.audiost[formObj.audiost.selectedIndex].value.charAt(0);}

	if (compdata1 == "X")
	{compdata1 = formObj.data1[formObj.data1.selectedIndex].value.charAt(0);}

	if (compdata2 == "X")
    {compdata2 = formObj.data2[formObj.data2.selectedIndex].value.charAt(0);}

}

/**
 @@
 @@ Begin building the actual part number
 @@ This works for tcvr REVERSE only
 @@ @param: 
 @@
 */
function makepartnumTC() 
{
    partlist = bgblue + "Flex " + bgred + part_type + bgoff

 	for (i=0; i<9; i++) { 
     parts = names[i].split("*")
     curpart = parts[0]
     curdescrip = parts[1]
  	 opnum = formObj[curpart].selectedIndex     //set the selected index number
     partnum += formObj[curpart][formObj[curpart].selectedIndex].value.charAt(0);    // add to the part number
     partlist += rgblue+"<b>" + curdescrip + ":</b> " + rgred + formObj[curpart][formObj[curpart].selectedIndex].value.substring(2,40) + "<br>";

	// insert dashes
	 if (curpart == "connector" | curpart == "audiost")
	 {partnum += "-"};
 	 }

	//store COMP variables
    compfiber = formObj.fiber[formObj.fiber.selectedIndex].value.charAt(0);  //same fiber type
    compcase = formObj.compcase[formObj.compcase.selectedIndex].value.charAt(0);  //case sytle
    compopmode1 = formObj.opmode[formObj.opmode.selectedIndex].value.charAt(0);  //same opmode
    compconn = formObj.connector[formObj.connector.selectedIndex].value.charAt(0);  //same connector
    compaud1 = formObj.audio[formObj.audio.selectedIndex].value.charAt(0);    //same audio
    compvid1 = formObj.video[formObj.video.selectedIndex].value.charAt(0);    //same video
    compaudst = formObj.compaudst[formObj.compaudst.selectedIndex].value.charAt(0);
    compdata1 = formObj.compdata1[formObj.compdata1.selectedIndex].value.charAt(0);
    compdata2 = formObj.compdata2[formObj.compdata2.selectedIndex].value.charAt(0);
	
	// check for "Same as Main" selection
	//if "same as main is selected, use it otherwise get new selection
    if (compaudst == "X")
    {compaudst = formObj.audiost[formObj.audiost.selectedIndex].value.charAt(0);}

	if (compdata1 == "X")
	{compdata1 = formObj.data1[formObj.data1.selectedIndex].value.charAt(0);}

	if (compdata2 == "X")
    {compdata2 = formObj.data2[formObj.data2.selectedIndex].value.charAt(0);}

}

/**
 @@
 @@ Check for "Same as Main" selection and use it otherwise get new selection
 @@ This module seems to be the same for FORWARD apps
 @@ @param: 
 @@
 */
 
function build_comp()
{
    if (compaudst == "x")
    {compaudst = formObj.audiost[formObj.audiost.selectedIndex].value.charAt(0);}
    else
    {compaudst = formObj.compaudst[formObj.compaudst.selectedIndex].value.charAt(0);}

	if (compdata1 == "x")
    {compdata1 = formObj.data1[formObj.data1.selectedIndex].value.charAt(0);}
    else
    {compdata1 = formObj.compdata1[formObj.compdata1.selectedIndex].value.charAt(0);}

	if (compdata2 == "x")
    {compdata2 = formObj.data2[formObj.data2.selectedIndex].value.charAt(0);}
    else
    {compdata2 = formObj.compdata2[formObj.compdata2.selectedIndex].value.charAt(0);}
}

/**
 @@
 @@ Do compliment validation
 @@
 @@ @param:	var p_type (Tx, Rx, Tc)
 @@
 */
function check_comps()
{
// check audio options
    if (formObj.audio.selectedIndex == 1 & formObj.audiost.selectedIndex != 1)
    {
		alert("INVALID OPTION: AUDIO has been set to NONE therefore AUDIO STRAPPING must be NONE");
	    formObj.audiost.focus();
		return false;
	}

    if (formObj.audio.selectedIndex > 1 & formObj.audiost.selectedIndex == 1)
    {
		alert("INVALID OPTION: AUDIO has been specified therefore an option must be chosen for AUDIO STRAPPING");
	    formObj.audiost.focus();
		return false;
	}

    if (formObj.audio.selectedIndex == 1 & formObj.compaudst.selectedIndex > 0)
    {
		alert("INVALID OPTION: AUDIO was set to NONE therefore COMPLIMENTARY AUDIO STRAPPING must be set to SAME AS MAIN");
	    formObj.compaudst.focus();
		return false;
	}
	
// check data options
//if "Contact Closure" is selected, make sure it's selected on comp number
    if (formObj.data1.selectedIndex == 6 & formObj.compdata1.selectedIndex != 5)
    {
	    if (formObj.compdata1.selectedIndex != 0)  //AND if SAME AS MAIN is not selected show error
		{
	    alert("INVALID OPTION: CONTACT CLOSURE for DATA1 must be the same for both the MAIN & COMPLIMENTARY parts");
	    formObj.compdata1.focus();
		return false;
		}	
	}

    if (formObj.data1.selectedIndex != 6 & formObj.compdata1.selectedIndex == 5)
    {
	    if (formObj.data1.selectedIndex != 1)
		{
	    alert("INVALID OPTION: CONTACT CLOSURE for DATA1 must be the same for both the MAIN & COMPLIMENTARY parts");
	    formObj.compdata1.focus();
		return false;
		}	
	}

    if (formObj.data2.selectedIndex == 6 & formObj.compdata2.selectedIndex != 5) //if Contact Closure is selected on MAIN but not on COMP
	{
    	if (formObj.compdata2.selectedIndex != 0)  //AND if SAME AS MAIN is not selected show error
		{
	    alert("INVALID OPTION: CONTACT CLOSURE for DATA2 must be the same for both the MAIN & COMPLIMENTARY parts");
	    formObj.compdata2.focus();
		return false;
		}	
	}

    if (formObj.data2.selectedIndex != 6 & formObj.compdata2.selectedIndex == 5)
    {
	    if (formObj.data2.selectedIndex != 1)
	    {
		alert("INVALID OPTION: CONTACT CLOSURE for DATA2 must be the same for both the MAIN & COMPLIMENTARY parts");
	    formObj.compdata2.focus();
		return false;
		}
	}

    if (formObj.data1.selectedIndex == 1 & formObj.compdata1.selectedIndex > 0)
    {
		alert("INVALID OPTION: DATA1 on the master part was set to NONE therefore DATA1 on the COMPLEMENTARY must be set to SAME AS MAIN");
	    formObj.compdata1.focus();
		return false;
	}

    if (formObj.data2.selectedIndex == 1 & formObj.compdata2.selectedIndex > 0)
    {
		alert("INVALID OPTION: DATA2 on the master part was set to NONE therefore DATA2 on the COMPLEMENTARY must be set to SAME AS MAIN");
	    formObj.compdata2.focus();
		return false;
	}

	return true;
}


/**
 @@
 @@ Generates the display window that shows configurator output
 @@
 @@ @param: mode=pricing or mode=null
 @@
 */
function display() 
{
	displayed='y'
	
	switch( mode )
	{
		case 'pricing' :

			var newDocumentString = '<HTML><HEAD><TITLE>Part Number</TITLE></HEAD><BODY>' + rgred + partlist + '</font><p>' + rgblue + '....Part Number: <b>'  + rgred + partnum + rgblue + ' -- List $ ' + rgred + partprice + '</b></font><br>';
			if (compcase != "N" && mode)
			{
				newDocumentString += rgblue + 'Comp. Number: <b>'  + rgred + comppart  + rgblue + ' -- List $ ' + rgred + comppartprice + '</b>'
			}
			break;
	
		default :
			
			var newDocumentString = '<HTML><HEAD><TITLE>Part Number</TITLE></HEAD><BODY>' + rgred + partlist + '</font><p>' + rgblue + 'Your Part Number Is: <b>'  + rgred + partnum + '</b></font><br>'
			if (compcase != "N")
			{
				newDocumentString += rgblue + 'Comp. Part Number: <b>'  + rgred + comppart +'</b>'
			}		
	}
	newDocumentString += "</UL><br>";
	newDocumentString += "<form name='form2'>";
	newDocumentString += "<INPUT TYPE='BUTTON' VALUE='Print this Configuration' onClick='window.print();'>";
	newDocumentString += "<p>" + "<INPUT TYPE='BUTTON' VALUE='Close this window' onClick='window.close();'>";
	newDocumentString += "</FORM></body>";
	
	DispWin = window.open("","PartWin", "toolbar=no,status=no,width=340,height=400");
	DispWin.document.write(newDocumentString);
	DispWin.document.close();
}

/**
 @@
 @@ Generates the client side email
 @@
 @@ @param: Already created in original JS
 @@
 */
        function update_message_body ()
        {
        		//ensure they have displayed part number which controls building the part #
                if (displayed == "n")
                {alert("Please use the DISPLAY button to verify your configuration first.")
                form1.company.focus();		
                return false;}
                
                var company = document . form1 . company . value
                var sendname = document . form1 . sendname . value
                var emailadd = document . form1 . emailadd . value
                var phone = document . form1 . phone . value
                var qty = document . form1 . qty . value
                var comments = document . form1 . comments . value
                
                if (company == "Company")
                {alert("Please enter your company name.")
                form1.company.focus()
                form1.company.select()
                return false;}
        
                if (sendname == "Name")
                {alert("Please include a contact name.")
                form1.sendname.focus()
                form1.sendname.select()
                return false;}
                        
                tomail = "y"
                valform1()
        //        tomail = "n"
        //        return false;
        
                document . proxy_form . message_body . value =
                
               "\t" + "Pure Digital Fiberlink Flex " + part_type + " Part Number from   \n"
                +       "\n"
                +       "\t" + "        COMPANY: " + company + " \n"
                +       "\t" + "           NAME: " + sendname + " \n"
                +       "\t" + "          EMAIL: " + emailadd + " \n"
                +       "\t" + "      TELEPHONE: " + phone + " \n"
                +       "\t" + "            QTY: " + qty + " \n"
                +       "\n"
                +       "\t" + "     NO. FIBERS: " + formObj.fiber[formObj.fiber.selectedIndex].value.substring(2,20) + "\n"		
                +       "\t" + "     CASE STYLE: " + formObj.casestyle[formObj.casestyle.selectedIndex].value.substring(2,20) + "\n"		
                +       "\t" + " OPERATING MODE: " + formObj.opmode[formObj.opmode.selectedIndex].value.substring(2,30) + "\n"
                +       "\t" + " CONNECTOR TYPE: " + formObj.connector[formObj.connector.selectedIndex].value.substring(2,30) + "\n"
                +       "\t" + "   VIDEO OPTION: " + formObj.video[formObj.video.selectedIndex].value.substring(2,40) + "\n"
                +       "\t" + "   AUDIO OPTION: " + formObj.audio[formObj.audio.selectedIndex].value.substring(2,30) + "\n"
                +       "\t" + "AUDIO STRAPPING: " + formObj.audiost[formObj.audiost.selectedIndex].value.substring(2,30) + "\n"
                +       "\t" + "          DATA1: " + formObj.data1[formObj.data1.selectedIndex].value.substring(2,30) + "\n"
                +       "\t" + "          DATA2: " + formObj.data2[formObj.data2.selectedIndex].value.substring(2,20) + "\n"
                +       "\t" + "\n"		
                +       "\t" + "\n"
                +       "\t" + "PART # " + partnum + "\n"		
                +       "\t" + "COMP # " + comppart + "\n"
                +       "\t" + "\n"
                +       "\t" + "COMMENTS: \n"
                +       "\t" +  comments + " \n"
                return true;
        }
		

