﻿var gServReq = 'VendorRegistration_Req.aspx';
var gCompanyID = 0;
var downloadWindow = null;

function loadDropDowns(params, page){
    if (page == "Page1")
    {
	    $('divCompCountry').update(params.CountryDropDown);
	    $('divCompState').update(params.StateDropDown);
	}
	else if (page == "Page2")
    {
	    $('divPrimaryCountry').update(params.PrimaryCountryDropDown);
	    $('divPrimaryState').update(params.PrimaryStateDropDown);
	    $('divSecondaryCountry').update(params.SecondaryCountryDropDown);
	    $('divSecondaryState').update(params.SecondaryStateDropDown);
	}
	else if (page == "Page3")
    {
	    $('divProduct').update(params.ProductDropDown);
	    $('divNuclearSupply').update(params.NuclearSupplyDropDown);
	    $('divASMESect3').update(params.ASMESect3DropDown);
	    $('divASMESect10').update(params.ASMESect10DropDown);
	    $('divASMESect11').update(params.ASMESect11DropDown);
	    $('divISO').update(params.ISODropDown);
	    $('divQAManual').update(params.QAManualDropDown);
	    $('div10CFR21').update(params.CFR21DropDown);
	    $('div10CFR50').update(params.CFR50DropDown);
	    $('divNQA1').update(params.NQA1DropDown);
	}
}

function setCompany(companyID)
{
    gCompanyID = companyID;
    $('tbCompName').disabled = true;
}

function changePage(source, dest, check)
{
    var location;
    if (dest == "Page1")
    {	
        location = './Registration_Page1.aspx';
    }
	else if (dest == "Page2")
    {	
        location = './Registration_Page2.aspx';
    }
	else if (dest == "Page3")
    {	
        location = './Registration_Page3.aspx';
    }
    
    var data = goodData(source);
    if (data == "true" || check == "no")
    {
        saveSession(source, location);
    }
    else
    {
        alert(data);
    }   
}

function goodData(page)
{
    var message;
    
    if (page == "Page1")
    {
        if ($F('tbCompName') != "" && $F('tbCompAddr1') != "" && $F('tbCompCity') != "" && $F('selCompState') != "" && $F('selCompCountry') != "" && $F('tbCompZip') != "" && $F('tbCompPhone') != "")
        {
            message = "true";
        }
        else if ($('selCompCountry')[$('selCompCountry').selectedIndex].text != "USA" && $F('tbCompName') != "" && $F('tbCompAddr1') != "" && $F('tbCompCity') != "" && $F('selCompCountry') != "" && $F('tbCompZip') != "" && $F('tbCompPhone') != "")
        {
            message = "true";
        }
        else
        {
            message = "Please fill in values for the following fields before continuing:\n\n";
            if ($F('tbCompName') == "")
            {
                message = message + "Company Name\n";
            }
            if ($F('tbCompAddr1') == "")
            {
                message = message + "Address Line 1\n";
            }
            if ($F('tbCompCity') == "")
            {
                message = message + "City\n";
            }
            if ($F('selCompCountry') == "")
            {
                message = message + "Country\n";
            }
            if ($F('selCompState') == "" && $('selCompCountry')[$('selCompCountry').selectedIndex].text == "USA")
            {
                message = message + "State\n";
            }
            if ($F('tbCompZip') == "")
            {
                message = message + "Postal Code\n";
            }
            if ($F('tbCompPhone') == "")
            {
                message = message + "Phone Number\n";
            }    
        }
    } 
    
    else if (page == "Page2")
    {
        if ($F('tbPrimaryFirst') != "" && $F('tbPrimaryLast') != "" && $F('tbPrimaryAddr1') != "" && $F('tbPrimaryCity') != "" && $F('selPrimaryState') != "" && $F('selPrimaryCountry') != "" && $F('tbPrimaryZip') != "" && $F('tbPrimaryPhone') != "" && $F('tbPrimaryEmail') != "")
        {
            message = "true";
        }
        else if ($('selPrimaryCountry')[$('selPrimaryCountry').selectedIndex].text != "USA" && $F('tbPrimaryFirst') != "" && $F('tbPrimaryLast') != "" && $F('tbPrimaryAddr1') != "" && $F('tbPrimaryCity') != "" && $F('selPrimaryCountry') != "" && $F('tbPrimaryZip') != "" && $F('tbPrimaryPhone') != "" && $F('tbPrimaryEmail') != "")
        {
            message = "true";
        }
        else
        {
            message = "Please fill in values for the following fields for the Primary contact before continuing:\n\n";
            if ($F('tbPrimaryFirst') == "")
            {
                message = message + "First Name\n";
            }
            if ($F('tbPrimaryLast') == "")
            {
                message = message + "Last Name\n";
            }
            if ($F('tbPrimaryAddr1') == "")
            {
                message = message + "Address Line 1\n";
            }
            if ($F('tbPrimaryCity') == "")
            {
                message = message + "City\n";
            }
            if ($F('selPrimaryCountry') == "")
            {
                message = message + "Country\n";
            }
            if ($F('selPrimaryState') == "" && $('selPrimaryCountry')[$('selPrimaryCountry').selectedIndex].text == "USA")
            {
                message = message + "State\n";
            }
            if ($F('tbPrimaryZip') == "")
            {
                message = message + "Postal Code\n";
            }
            if ($F('tbPrimaryPhone') == "")
            {
                message = message + "Phone Number\n";
            }   
            if ($F('tbPrimaryEmail') == "")
            {
                message = message + "Email\n";
            }     
        }
    } 
    
    else if (page == "Page3")
    {
        if ($('selAssocProduct').length > 0 && $F('taServices') != "" && $F('taIndustries') != "" && $F('selNuclearSupply') != "" && $F('selASMESect3') != "" && $F('selASMESect10') != "" && $F('selASMESect11') != "" && $F('selISO') != "" && $F('selQAManual') != "" && $F('sel10CFR21') != "" && $F('sel10CFR50') != "")
        {
            message = "true"
        }
        else
        {
            message = "Please fill in values for the following fields before continuing:\n\n";
            if ($('selAssocProduct').length == 0)
            {
                message = message + "Products Manufactured or Designed\n";
            }
            if ($F('taServices') == "")
            {
                message = message + "Services Provided\n";
            }
            if ($F('taIndustries') == "")
            {
                message = message + "Industries Served\n";
            }
            if ($F('selNuclearSupply') == "")
            {
                message = message + "Nuclear Supply Experience\n";
            }
            if ($F('selASMESect3') == "")
            {
                message = message + "ASME Section 3 Certification\n";
            }
            if ($F('selASMESect10') == "")
            {
                message = message + "ASME Section 10 Certification\n";
            }
            if ($F('selASMESect11') == "")
            {
                message = message + "ASME Section 11 Certification\n";
            }
            if ($F('selISO') == "")
            {
                message = message + "ISO Certification\n";
            }
            if ($F('selQAManual') == "")
            {
                message = message + "QA Manual\n";
            }
            if ($F('sel10CFR21') == "")
            {
                message = message + "10CFR21 Compliant\n";
            }
            if ($F('sel10CFR50') == "")
            {
                message = message + "10CFR50, Appendix B Compliant\n";
            }   
        }
    }   
    
    return message; 
}

function restorePage1(params){
	$('tbCompName').value = params.compName;
	$('tbCompAddr1').value = params.compAddr1;
	$('tbCompAddr2').value = params.compAddr2;
	$('tbCompCity').value = params.compCity;
	$('selCompState').value = params.compState;
	$('selCompCountry').value = params.compCountry;
	$('tbCompZip').value = params.compZip;
	$('tbCompPhone').value = params.compPhone;
	$('tbCompFax').value = params.compFax;
	$('tbCompWebsite').value = params.compWebsite;
	if ($('selCompCountry')[$('selCompCountry').selectedIndex].text == "USA")
    {
        $('trCompState').style.display = '';
    }
    else
    {
        $('trCompState').style.display = 'none';
    }   
}

function restorePage2(params){
	$('tbPrimaryFirst').value = params.primaryFirst;
	$('tbPrimaryMiddle').value = params.primaryMiddle;
	$('tbPrimaryLast').value = params.primaryLast;
	$('tbPrimaryTitle').value = params.primaryTitle;
	$('tbPrimaryAddr1').value = params.primaryAddr1;
	$('tbPrimaryAddr2').value = params.primaryAddr2;
	$('tbPrimaryCity').value = params.primaryCity;
	$('selPrimaryState').value = params.primaryState;
	$('selPrimaryCountry').value = params.primaryCountry;
	$('tbPrimaryZip').value = params.primaryZip;
	$('tbPrimaryPhone').value = params.primaryPhone;
	$('tbPrimaryFax').value = params.primaryFax;
	$('tbPrimaryEmail').value = params.primaryEmail;
	if ($('selPrimaryCountry')[$('selPrimaryCountry').selectedIndex].text == "USA")
    {
        $('trPrimaryState').style.display = '';
    }
    else
    {
        $('trPrimaryState').style.display = 'none';
    }   
    
	$('tbSecondaryFirst').value = params.secondaryFirst;
	$('tbSecondaryMiddle').value = params.secondaryMiddle;
	$('tbSecondaryLast').value = params.secondaryLast;
	$('tbSecondaryTitle').value = params.secondaryTitle;
	$('tbSecondaryAddr1').value = params.secondaryAddr1;
	$('tbSecondaryAddr2').value = params.secondaryAddr2;
	$('tbSecondaryCity').value = params.secondaryCity;
	$('selSecondaryState').value = params.secondaryState;
	$('selSecondaryCountry').value = params.secondaryCountry;
	$('tbSecondaryZip').value = params.secondaryZip;
	$('tbSecondaryPhone').value = params.secondaryPhone;
	$('tbSecondaryFax').value = params.secondaryFax;
	$('tbSecondaryEmail').value = params.secondaryEmail;
	if ($('selSecondaryCountry')[$('selSecondaryCountry').selectedIndex].text == "USA")
    {
        $('trSecondaryState').style.display = '';
    }
    else
    {
        $('trSecondaryState').style.display = 'none';
    }   
}

function restorePage3(params){
    var arrValue = new Array();
    var arrText = new Array();
    var SEL = $('selAssocProduct');
    
    arrValue = params.productValue.split(',');
    arrText = params.productText.split(',');
    
    for (var i = 0; i<arrValue.length; i++) 
    {
        var newEle = document.createElement('option');
        newEle.text = arrText[i];
        newEle.value = arrValue[i];
        
        try {
            SEL.add(newEle, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
            SEL.add(newEle); // IE only
        }
    }

	$('taServices').value = params.services.split('||__||').join('\n');;
	$('taIndustries').value = params.industries.split('||__||').join('\n');;
	$('selNuclearSupply').value = params.nuclearSupply;
	$('taComments').value = params.comments.split('||__||').join('\n');;
	$('selASMESect3').value = params.asmeSect3;
	$('selASMESect10').value = params.asmeSect10;
	$('selASMESect11').value = params.asmeSect11;
	$('selISO').value = params.iso;
	$('taOtherCerts').value = params.otherCerts.split('||__||').join('\n');;
	$('selQAManual').value = params.qaManual;
	$('sel10CFR21').value = params.cfr21;
	$('sel10CFR50').value = params.cfr50;
	$('divAttachedFiles').innerHTML = params.Files;
	$('divLinks').innerHTML = params.Links;
}

function saveSession(page, location)
{
    if (page == "Page1")
    {
        if (gCompanyID == 0)
        {
            createNewCompany(location);
        }
        else
        {
            savePage1Session(location);
        }
    }
    else if (page == "Page2")
    {
        savePage2Session(location);
    }
    else if (page == "Page3")
    {
        savePage3Session(location);
    }
}

function createNewCompany(location)
{
    new Ajax.Request(
	    gServReq,
	    { 
		    method:'post', 
		    onCreate: function(){
		    //Turn Load Indicator On
		    }, 
		    onComplete: function(){
		    //Turn Load Indicator Off
		    },
		    onSuccess: function(transport){
		    //Handle Successful Response
			     if (transport.responseText == 0)
			     {
			        alert('The company "' + $F('tbCompName') + '" has already been registered.');
			     }
			     else
			     {
			        gCompanyID = transport.responseText;
			        savePage1Session(location);
			     }
		    },
		    parameters: {
			    requestType:"createCompany",
			    compName:$F('tbCompName')
		    },
		    onException: function(transport,ex){
		    //Turn Error Indicator On
		    //Javascript Exception
		    },
		    on406: function(transport){
		    //Turn Error Indicator On
		    //Server Error
		    }
	    }
    );
}

function savePage1Session(location)
{
    new Ajax.Request(
	    gServReq,
	    { 
		    method:'post', 
		    onCreate: function(){
		    //Turn Load Indicator On
		    }, 
		    onComplete: function(){
		    //Turn Load Indicator Off
		    },
		    onSuccess: function(transport){
		    //Handle Successful Response
			     window.location = location + '?CompanyID=' + gCompanyID;
		    },
		    parameters: {
			    requestType:"savePage1Session",
			    companyID:gCompanyID,
			    compName:$F('tbCompName'),
			    compAddr1:$F('tbCompAddr1'),
			    compAddr2:$F('tbCompAddr2'),
			    compCity:$F('tbCompCity'),
			    compState:$F('selCompState'),
			    compCountry:$F('selCompCountry'),
			    compZip:$F('tbCompZip'),
			    compPhone:$F('tbCompPhone'),
			    compFax:$F('tbCompFax'),
			    compWebsite:$F('tbCompWebsite')
		    },
		    onException: function(transport,ex){
		    //Turn Error Indicator On
		    //Javascript Exception
		    },
		    on406: function(transport){
		    //Turn Error Indicator On
		    //Server Error
		    }
	    }
    );
}

function savePage2Session(location)
{
    new Ajax.Request(
	    gServReq,
	    { 
		    method:'post', 
		    onCreate: function(){
		    //Turn Load Indicator On
		    }, 
		    onComplete: function(){
		    //Turn Load Indicator Off
		    },
		    onSuccess: function(transport){
		    //Handle Successful Response
			     window.location = location + '?CompanyID=' + gCompanyID;
		    },
		    parameters: {
			    requestType:"savePage2Session",
			    companyID:gCompanyID,
			    primaryFirst:$F('tbPrimaryFirst'),
			    primaryMiddle:$F('tbPrimaryMiddle'),
			    primaryLast:$F('tbPrimaryLast'),
			    primaryTitle:$F('tbPrimaryTitle'),
			    primaryAddr1:$F('tbPrimaryAddr1'),
			    primaryAddr2:$F('tbPrimaryAddr2'),
			    primaryCity:$F('tbPrimaryCity'),
			    primaryState:$F('selPrimaryState'),
			    primaryCountry:$F('selPrimaryCountry'),
			    primaryZip:$F('tbPrimaryZip'),
			    primaryPhone:$F('tbPrimaryPhone'),
			    primaryFax:$F('tbPrimaryFax'),
			    primaryEmail:$F('tbPrimaryEmail'),
			    secondaryFirst:$F('tbSecondaryFirst'),
			    secondaryMiddle:$F('tbSecondaryMiddle'),
			    secondaryLast:$F('tbSecondaryLast'),
			    secondaryTitle:$F('tbSecondaryTitle'),
			    secondaryAddr1:$F('tbSecondaryAddr1'),
			    secondaryAddr2:$F('tbSecondaryAddr2'),
			    secondaryCity:$F('tbSecondaryCity'),
			    secondaryState:$F('selSecondaryState'),
			    secondaryCountry:$F('selSecondaryCountry'),
			    secondaryZip:$F('tbSecondaryZip'),
			    secondaryPhone:$F('tbSecondaryPhone'),
			    secondaryFax:$F('tbSecondaryFax'),
			    secondaryEmail:$F('tbSecondaryEmail')

		    },
		    onException: function(transport,ex){
		    //Turn Error Indicator On
		    //Javascript Exception
		    },
		    on406: function(transport){
		    //Turn Error Indicator On
		    //Server Error
		    }
	    }
    );
}

function savePage3Session(location)
{
    var arrProductValue = new Array(); 
    var arrProductText = new Array();
    
    for (var i = 0; i<$('selAssocProduct').length; i++) 
    {
        arrProductValue[i] = $('selAssocProduct')(i).value;
        arrProductText[i] = $('selAssocProduct')(i).text;
    }
    
    new Ajax.Request(
	    gServReq,
	    { 
		    method:'post', 
		    onCreate: function(){
		    //Turn Load Indicator On
		    }, 
		    onComplete: function(){
		    //Turn Load Indicator Off
		    },
		    onSuccess: function(transport){
		    //Handle Successful Response
                if (location == "submit")
                {
                    submitInformation();
                }
                else if (location == "file")
                {
                    __doPostBack('FileUpload',gCompanyID);
                }
			    else
			    {
			 
			        window.location = location + '?CompanyID=' + gCompanyID;
			    }
		    },
		    parameters: {
			    requestType:"savePage3Session",
			    companyID:gCompanyID,
			    productValue:arrProductValue,
			    productText:arrProductText,
			    services:$F('taServices'),
			    industries:$F('taIndustries'),
			    nuclearSupply:$F('selNuclearSupply'),
			    comments:$F('taComments'),
			    asmeSect3:$F('selASMESect3'),
			    asmeSect10:$F('selASMESect10'),
			    asmeSect11:$F('selASMESect11'),
			    iso:$F('selISO'),
			    otherCerts:$F('taOtherCerts'),
			    qaManual:$F('selQAManual'),
			    cfr21:$F('sel10CFR21'),
			    cfr50:$F('sel10CFR50'),
			    nqa1:$F('selNQA1')
		    },
		    onException: function(transport,ex){
		    //Turn Error Indicator On
		    //Javascript Exception
		    },
		    on406: function(transport){
		    //Turn Error Indicator On
		    //Server Error
		    }
	    }
    );
}

function submitClick()
{
    var data = goodData('Page3');
    if (data == "true")
    {
        savePage3Session("submit");
    }
    else
    {
        alert(data);
    }  
    //savePage3Session("submit");
}

function submitInformation()
{	            
	new Ajax.Request(
        gServReq,
        { 
	        method:'post', 
	        onCreate: function(){
	        //Turn Load Indicator On
	        }, 
	        onComplete: function(){
	        //Turn Load Indicator Off
	        },
	        onSuccess: function(transport){
	        //Handle Successful Response
	            var obj = transport.responseText.evalJSON();
	            if (obj.Redirect == 1)
	            {
	                window.location = "./Registration_Complete.aspx"
	            }
	            else if (obj.Redirect == 2)
	            {
	               
	                window.location = "./../System/Inbox/Inbox.aspx"
	            }
	            else
	            {
	                 window.location = "./../System/Admin/Admin.aspx"
	            }
	        },
	        parameters: {
		        requestType:"submitInformation",
		        companyID:gCompanyID
	        },
	        onException: function(transport,ex){
	        //Turn Error Indicator On
	        //Javascript Exception
	        },
	        on406: function(transport){
	        //Turn Error Indicator On
	        //Server Error
	        }
        }
    );
}

function MoveProductRight()
{
    var SourceSEL = $('selProduct');
    var DestSEL = $('selAssocProduct');
    
    var len=SourceSEL.length;
    
    for (var i=0; i<len; i++)
    {
        var Exists = 0;
        var NewItemID = SourceSEL.item(i).value;
        var isSelected = SourceSEL.item(i).selected;

        if(isSelected == false) 
        {
            Exists = 1;
        }
        else
        {
            for(var j=0; j<DestSEL.length; j++)
            {
                if (NewItemID == DestSEL.item(j).value)
                {
                    Exists = 1;
                }
            }
        }
        
        if (Exists == 0 )
        {
            var newEle = document.createElement('option');
            newEle.text = SourceSEL.item(i).text;
            newEle.value = SourceSEL.item(i).value;

            try {
                DestSEL.add(newEle, null); // standards compliant; doesn't work in IE
            }
            catch(ex) {
                DestSEL.add(newEle); // IE only
            }
        }      
    }
}

function MoveProductRightAll()
{
    var SourceSEL = $('selProduct');
    var DestSEL = $('selAssocProduct');
    
    var len=SourceSEL.length;
        
    for (var i=0; i<len; i++)
    {
        var Exists = 0;
        var NewItemID = SourceSEL.item(i).value;
        
        for(var j=0; j<DestSEL.length; j++)
        {
            if (NewItemID == DestSEL.item(j).value)
            {
                Exists = 1;
            }
        }
        
        if (Exists == 0)
        {
            var newEle = document.createElement('option');
            newEle.text = SourceSEL.item(i).text;
            newEle.value = SourceSEL.item(i).value;

            try {
                DestSEL.add(newEle, null); // standards compliant; doesn't work in IE
            }
            catch(ex) {
                DestSEL.add(newEle); // IE only
            }
            //DestSEL.insert(SourceSEL.item(0));
        }
        else
        {
            //SourceSEL.remove(SourceSEL.item(0));
        }
    }
}

function MoveProductLeft()
{
    var SourceSEL = $('selAssocProduct');
    
    var len=SourceSEL.length;
    
    for (var i=0; i<len; i++)
    {
        var isSelected = SourceSEL.item(i).selected;

        if(isSelected == true) 
        {
            SourceSEL.remove(i)
            len = len-1;
            i = i-1;
        }
    }
}

function MoveProductLeftAll()
{
    var SourceSEL = $('selAssocProduct');
    
    var len=SourceSEL.length;
    
    for (var i=0; i<len; i++)
    {
        SourceSEL.remove(i)
        len = len-1;
        i = i-1;
    }
}

function countryChange(field)
{
    if ($('sel'+field+'Country')($('sel'+field+'Country').selectedIndex).text == "USA")
    {
        $('tr'+field+'State').style.display = '';
        $('sel'+field+'State').value = "";
    }
    else
    {
        $('tr'+field+'State').style.display = 'none';
        $('sel'+field+'State').value = "";
    }   
}

function attachFile()
{
    savePage3Session('file');
}

function addLink()
{
    new Ajax.Request(
        gServReq,
        { 
	        method:'post', 
	        onCreate: function(){
	        //Turn Load Indicator On
	        }, 
	        onComplete: function(){
	        //Turn Load Indicator Off
	        },
	        onSuccess: function(transport){
	        //Handle Successful Response
	            $('divLinks').innerHTML = transport.responseText;
	            //var newLink;
                //newLink = '<a href="' + $F('tbNewLinkPath') + '" target="_blank">';
                //newLink = newLink + $F('tbNewLinkPath') + '</a>';
                //newLink = newLink + '</a>';
                //$('divLinks').innerHTML = $('divLinks').innerHTML + newLink;
            },
	        parameters: {
		        requestType:"addNewLink",
		        companyID:gCompanyID,
		        linkPath:$F('tbNewLinkPath')
	        },
	        onException: function(transport,ex){
	        //Turn Error Indicator On
	        //Javascript Exception
	        },
	        on406: function(transport){
	        //Turn Error Indicator On
	        //Server Error
	        }
        }
    );
}

function removeFile(ID)
{
    new Ajax.Request(
        gServReq,
        { 
	        method:'post', 
	        onCreate: function(){
	        //Turn Load Indicator On
	        }, 
	        onComplete: function(){
	        //Turn Load Indicator Off
	        },
	        onSuccess: function(transport){
	        //Handle Successful Response
	            $('divAttachedFiles').innerHTML = transport.responseText;
            },
	        parameters: {
		        requestType:"removeFile",
		        ID:ID
	        },
	        onException: function(transport,ex){
	        //Turn Error Indicator On
	        //Javascript Exception
	        },
	        on406: function(transport){
	        //Turn Error Indicator On
	        //Server Error
	        }
        }
    );
}

function removeLink(ID)
{
    new Ajax.Request(
        gServReq,
        { 
	        method:'post', 
	        onCreate: function(){
	        //Turn Load Indicator On
	        }, 
	        onComplete: function(){
	        //Turn Load Indicator Off
	        },
	        onSuccess: function(transport){
	        //Handle Successful Response
	            $('divLinks').innerHTML = transport.responseText;
            },
	        parameters: {
		        requestType:"removeLink",
		        ID:ID
	        },
	        onException: function(transport,ex){
	        //Turn Error Indicator On
	        //Javascript Exception
	        },
	        on406: function(transport){
	        //Turn Error Indicator On
	        //Server Error
	        }
        }
    );
}

function viewCompanyFile(companyFileID)
{
    if (downloadWindow)
    {
        downloadWindow.close();
    }
    
    downloadWindow = window.open('./../System/Files/download.aspx?companyFileID=' + companyFileID, 'ViewSpec', 'Width=10px,Height=10px,Top=15px,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
}

function openVendorRegistHelp()
{
    window.open("./../PDFs/MNES_VENDOR_REGISTRATION.pdf")
}