///////////////////////////////////////////
//
//see quoteCookies.php in /phpIncludes
//
///////////////////////////////////////////

function addComments(e)
{
	var elId = getElemID(e) == 'error' ? e : getElemID(e);
	if (elId == 'comments') 
	{
		var quoteComments = $('comments').value;
		createCookie('quoteBinComments', quoteComments, 1000);
	}
	else
	{
		var idNum = elId.substr(elId.length - 1);
		var cookieName = $('quotePart'+idNum).value;
		var partComments = $(elId).value;
		createCookie(cookieName,partComments,1000);
	}
}

function partRemove(e)
{
    ////////////controls the textboxes in the quoteForm
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    var idNum = elId.substr(elId.length - 1);
    elementsLen = document.forms[0].elements.length;
    quoteVal = new Array();
    z = 0;
    checkQuoteBin('remove', quotePart[idNum].value,elId);
    if (!addPart[idNum].checked) 
    {
        for (x = 0; x < elementsLen - 1; x++) 
        {
            if (document.forms[0].elements[x].id.indexOf('QUOTE') > -1) 
            {
                quoteVal[z] = document.forms[0].elements[x].value;                                
                
                if (quotePart[idNum].value == quoteVal[z]) 
                {					
                    document.forms[0].elements['QUOTE' + z + 'a'].checked = false;
                }
                
                z++;
            }
        }
        
        $$('#allCGparts li').each(function(s)
        {
            var liId = s.id;
            var liIdNum = s.id.substr(6);
            
            if (quotePart[idNum].value == $('cgMainPN' + liIdNum).innerHTML) 
            {
                $('cgPartBox' + liIdNum).checked = false;
            }
        })
        
        quotePart[idNum].value = '';
        quoteQty[idNum].value = '';
    }
}

function openQuoteBin(e)
{
	var elId = getElemID(e) == 'error' ? e : getElemID(e);
    var args = arguments.callee;
    args.hasParts = '';
    $$('input.quoteInput').each(function(s)
    {
        var inputId = s.id;
        if ($(inputId).value != '') 
        {
            args.hasParts += 'yes';
        }
        else 
        {
            args.hasParts += 'no';
        }
    })
    if (args.hasParts.indexOf('yes') > -1) 
    {
        showQuote();
    }
    else 
    {
        alert('The quote bin is empty.\r\n\r\nTo add a part to the quote bin:\r\n\r\nStandard parts: check the box in the "quote" column on the main parts table.\r\nCustom parts: check the box next to "add to quote" on the custom gear form.\r\n');
    }
}

function showQuote(e)
{
	var quoteControlBox = browserName == 'msie' ? $('quoteControlsIE') : $('quoteControls');
	quoteControlBox.style.right = '360px';
	quoteControlBox.style.top = 150 + parseInt(findScrollY($('quoteBin'))) + 'px';
    quoteControlBox.toggle();
}

function addToQuoteFile(boxId, partToQuote)
{
	var partChecked = false;
	checkQuoteBin('add',partToQuote,boxId);
	quoteControlBox = browserName == 'msie' ? $('quoteControlsIE') : $('quoteControls');
	
    //////////////controls the checkboxes in the part table
	if ($('sendRFQbtn1')) 
	{
		$('sendRFQbtn1').disabled = false;
	}

    quoteControlBox.style.right = '360px';
	quoteControlBox.style.top = 150 + parseInt(findScrollY($('quoteBin'))) + 'px';
	
    if (!quoteControlBox.visible()) 
    {
		quoteControlBox.show();
		
        if ($('customGearDiv').visible()) 
        {
        
            var cgdX = findPosX($('customGearDiv'));
            var cgdY = findPosY($('customGearDiv'));
            
            if (browserName == 'msie') 
            {
                findScrollY($('customGearDiv')) + 'px';
            }            
        }            
        
        $('closeMsg').hide();
    }
    else 
    {
        if (quoteControlBox.style.display == 'none') 
        {
            quoteControlBox.show();
        }
        else 
        {
            if (!$('customGearDiv').visible()) 
            {
                quoteControlBox.show();
            }
        }
    }
    
    if (boxId != 'fromCookie') 
    {
        partQBox = document.forms[0].elements[boxId];
        if (partQBox.checked == true) 
        {
            partChecked = true;
        }
    }
    
    
    if (partChecked == true || boxId == 'fromCookie') 
    {
        for (x = 1; x < 10; x++) 
        {
            if (quotePart[x].value.replaceWS().length < 1) 
            {
                quotePart[x].value = partToQuote;
				quoteQty[x].value = !getCookie(partToQuote) ? '' : getCookie(partToQuote);
                addPart[x].checked = true;
                break;
            }
        }
    }
    else 
    {
        for (x = 1; x < 10; x++) 
        {
            if (quotePart[x].value.replaceWS() == partToQuote) 
            {
                quotePart[x].value = '';
                addPart[x].checked = false;
                break;
            }
        }
    }	
}

function addQuoteFile(e)
{
    //////////controls the add external files portion of the RFQ
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    
    ///////since there are a max of 9 parts just get the last character
    var idNum = elId.substr(elId.length - 1);
    
    var thisElement = '';
    var qHeight = '';
    var checkedElement = $(elId);
    thisElement = $('fileRow' + idNum);
    
    if (!$(thisElement.visible())) 
    {
        $(thisElement).show();
    }
    else 
    {
        $(thisElement).hide();
    }
}

function clearAddFiles()
{
    for (x = 1; x < 10; x++) 
    {
        addFile[x].checked = false;
        addPart[x].checked = false;
    }
    
    return;
}

function quoteControl(e)
{
    checkEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var elId = '';
    var tempQty = '';
    
    if (e != 'none') 
    {
    
        elId = getElemID(e) == 'error' ? e : getElemID(e);
    }
    
    
    if (elId == 'sendRFQbtn1') 
    {
        ////check part boxes to see if there are any parts to quote
        var quotePartStr = '';
        
        var qWarn = '';
        var quantityWarn = 'Please provide the quantities for each of the following parts:\r\n\r\n';
        for (x = 1; x < 10; x++) 
        {
            quotePartStr += quotePart[x].value.length > 0 ? quotePart[x].value + ',' : '';
            tempQty = $(quoteQty[x]).value.replaceWS();
            
            if (tempQty.length == 0 && quotePart[x].value.length > 0) 
            {
                qWarn = true
                quantityWarn += quotePart[x].value + '\r\n';
                
            }
        }
                        
        if (quotePartStr.length < 1) 
        {
            alert('No parts have been selected');
        }
        else 
        {
            email1.value = email1.value.toLowerCase();
            ////check to see if the email exists in the databse
            var emailTest1 = checkEmail.test(email1.value);
            if (qWarn) 
            {
                if (!emailTest1) 
                {
                    quantityWarn += '\r\n Please enter a valid email address';
                }
                quoteSendMsg.innerHTML = 'Provide Quantities'
                alert(quantityWarn);
                return;
            }
            if (emailTest1) 
            {
                createCookie('cgID', email1.value, 60);
                sendRFQbtn1.value = "Sending";
				sendRFQbtn1.disabled = true;
                quoteSendMsg.innerHTML = 'Sending...';
                getPage('Tech_Tools/' + folder + '/emailCheck.php?email=' + email1.value, 'processEmail');
            }
            else 
            {
                var emailLen = email1.value.length;
                if (emailLen > 1) 
                {
                    alert('Please enter a valid email address');
                }
                else 
                {
                    regTable.style.display = '';
                    $('quoteTable').setOpacity(.8);
                    
                    if (browserName == 'msie') 
                    {
                        //quoteT.filters.alpha.opacity = 80;
                    }
                    else 
                    {
                        //quoteT.style.MozOpacity = .8;
                    }
                }
            }
        }
    }
    else 
    {
        warnMsg = '';
        var emailTest2 = checkEmail.test(email2.value);
        emailTest2 == true ? warnMsg = warnMsg + '' : warnMsg = warnMsg + 'Enter a valid email address\r\n';
        fname.value.length < 2 ? warnMsg = warnMsg + 'Enter your first name.\r\n' : warnMsg = warnMsg + '';
        lname.value.length < 2 ? warnMsg = warnMsg + 'Enter your last name.\r\n' : warnMsg = warnMsg + '';
        company.value.length < 2 ? warnMsg = warnMsg + 'Enter your company name.\r\n' : warnMsg = warnMsg + '';
        street.value.length < 2 ? warnMsg = warnMsg + 'Enter your street.\r\n' : warnMsg = warnMsg + '';
        city.value.length < 2 ? warnMsg = warnMsg + 'Enter your city.\r\n' : warnMsg = warnMsg + '';
        zip.value.length < 3 ? warnMsg = warnMsg + 'Enter your zip or postal code. \r\n' : warnMsg = warnMsg + '';
        phone.value.length < 10 ? warnMsg = warnMsg + 'Enter your phone number.\r\n' : warnMsg = warnMsg + '';
        country.value == '--' ? warnMsg = warnMsg + 'Select your country.\r\n' : warnMsg = warnMsg + '';
        
        prov.value != '--' ? stateInfo = prov.value : stateInfo = state.value;
        statesAlt.value != '' ? stateInfo = statesAlt.value : '';
        stateInfo == '--' ? warnMsg = warnMsg + 'Enter your State or Province.' : warnMsg = warnMsg + '';
        
        if (emailIt.checked == true) 
        {
            emailOrFaxIt = 'Email';
        }
        
        if (faxIt.checked == true) 
        {
            emailOrFaxIt = 'fax';
        }
        
        if (faxIt.checked == true && emailIt.checked == true) 
        {
            emailOrFaxIt = 'Email and Fax';
        }
        
        if (warnMsg.length > 1) 
        {
            alert(warnMsg);
			$('registerQuoteTable').show();
        }
        else 
        {
            sendRFQbtn1.value = "Sending";
            if (e == 'none') 
            {
                quoteSendMsg.style.display = '';
                quoteSendMsg.innerHTML = 'Sending...';
                //quoteSendMeter.style.display = '';
            }
            else 
            {
                regSendMsg.style.display = '';
                //regSendMeter.style.display = '';
            }
            
            email1.value.length != email2.value.length ? email1.value = email2.value : '';
            // xmlString = '?email='+email2.value+'&fname='+fname.value+'&lname='+lname.value+'&company='+company.value;
            //xmlString += '&street='+street.value+'&city='+city.value+'&state='+stateInfo+'&zip='+zip.value;
            //xmlString += '&phone='+phone.value+'&fax='+fax.value+'&country='+country.value;
            
            xmlString = '?userInfo=|fname=' + fname.value + '|lname=' + lname.value + '|company=' + company.value + '|street=' + street.value + '|city=' + city.value;
            xmlString += '|state=' + stateInfo + '|zip=' + zip.value + '|phone=' + phone.value + '|fax=' + fax.value + '|email=' + email2.value + '|comments=' + comments.value;
            xmlString += '|emailOrFaxIt=' + emailOrFaxIt + '|country=' + country.value + '|sessid=' + sessid;
            
            for (x = 1; x < 10; x++) 
            {
                //quotePart[x].value.length > 0 ? xmlString += '&quotePart'+x+'='+quotePart[x].value+'&quoteQty'+x+'='+quoteQty[x].value : '';
                quotePart[x].value.length > 0 ? xmlString += '|quotePart' + x + '=' + quotePart[x].value + '|quoteQty' + x + '=' + quoteQty[x].value : '';
            }
            quoteCounter++;
            
            if (quoteCounter > 9) 
            {
                sendRFQbtn1.disabled = true;
                alert('You\'ve sent 9 quotes.  You need to refresh the page in order to send another. ');
            }
            else 
            {
                replaceThese = new Array("!", "#", "$", "%", "^", "&", "*", "(", ")", "+", "/", "<", ">", "?", "\\", "[", "{", "}", "`", "~", ",", ";", ":");
                xmlString = xmlString.replaceWith(replaceThese, "_");
                getPage('Tech_Tools/' + folder + '/processQuote.php?userInfo=' + xmlString, 'processQuote');
            }
        }
    }
    
    return;
}

function cleanQuote(e)
{
    var elId = getElemID(e) == 'error' ? e : getElemID(e);
    
    if (elId == 'closeQuote') 
    {
        if (regTable.visible()) 
        {
            regTable.style.display = 'none';
            $('quoteTable').setOpacity(1);
            if (browserName == 'msie') 
            {
                //quoteT.filters.alpha.opacity = 100;
            }
            else 
            {
                //quoteT.style.MozOpacity = 1;
            }
        }
        sendRFQbtn1.value = "Send";
        quoteSendMsg.style.display = '';
        if (lastRefNumber) 
        {
            quoteSendMsg.innerHTML = 'Last reference#<br />' + lastRefNumber;
        }
        else 
        {
            quoteSendMsg.innerHTML = '';
        }
        
        sendCell.style.background = "#343c63";
        
        for (x = 1; x < 10; x++) 
        {
            quotePart[x].value = '';
            quoteQty[x].value = '';
            addPart[x].checked = false;
            addFile[x].checked = false;
            fileRow[x].style.display = 'none';
        }
        
        comments.value = '';
        
        $$('#allCGparts li').each(function(s)
        {
            var liId = s.id;
            var liIdNum = s.id.substr(6);
            
            $('cgPartBox' + liIdNum).checked = false;
            
            
        })
        
        for (x = 0; x < 25; x++) 
        {
            if (document.forms[0].elements['QUOTE' + x + 'a']) 
            {
                document.forms[0].elements['QUOTE' + x + 'a'].checked = false;
            }
        }
    }
}

function minimizeQC()
{
    /*$(quoteT).visualEffect('fold', 
     {
     afterFinish: function()
     {
     //setTimeout("quoteControlMaxObj.show()", 500);
     
     quoteControlMaxObj.show();
     }
     });*/
    $(quoteT).hide();
	
	if (browserName == 'msie') 
	{
		$('quoteBlockIframe').hide();
	}
    //$(quoteControlClass).style.width = '250px';
    
    //quoteControlColObj.show();
    $('quoteControlMax').show();
    //$('quoteControlMax').visualEffect('blindDown');
	$('quoteControlMax').style.top = 158 + findScrollY($(window)) + 'px';
	$('quoteControlMax').style.right = '5px';
}

function maximizeQC()
{
    $(quoteT).visualEffect('Grow', 
    {
        direction: 'top-left',
        afterSetup: function()
        {
            $('quoteControlMax').hide();
            //quoteControlColObj.hide();
        }
    });
}
