/*
	utilities.js
*/

/*
	for the Contact Info form:
	enable the Specify text field only for certain source values
*/
function setSourceSpecifyStatus() 
{
        //alert("SETSOURCESTATUS");
	switch(document.frmContactInfo.Contact_source.selectedIndex) 
        {

		case 2:
                case 5:
                case 8:
                case 10:
		case 13:
                case 15:
			document.frmContactInfo.Contact_source_specify.disabled = false;
			break;

		default:
			document.frmContactInfo.Contact_source_specify.disabled = true;
			document.frmContactInfo.Contact_source_specify.value = "";
			break;
	}
}

function setExistingCodeStatus() 
{
	if(document.frmContactInfo.Use_Existing_Code.checked) 
        {
			document.frmContactInfo.Previous_Code.disabled = false;
			document.frmContactInfo.Previous_Sale.disabled = false;
                        validatePreviousSale();
                        //document.frmContactInfo.Previous_Code.value = "xxxx";
	}
	else
	{			
	                document.frmContactInfo.Previous_Code.disabled = true;
			document.frmContactInfo.Previous_Sale.disabled = true;
	}


}

function setTest()
{
     //alert("SET:TEST:");
     document.frmContactInfo.Previous_Sale.disabled = false;
 
}

function validatePreviousSale()
{
    var invalidSale = false;
    var previousKidsEveryWear = false;
    var saleName=document.frmContactInfo.Previous_Sale.value;
    var code=document.frmContactInfo.Previous_Code.value;
    code = trim(code);
//alert("validate");
    saleName = trim(saleName);
    saleName = saleName.toUpperCase();
    
    if((saleName.length >= 1) && (code.length >= 1))
    {
            if((saleName == "KIDSEXCHANGE") ||  
                (saleName == "NEARLYNEW") ||
                (saleName == "KIDS EXCHANGE") ||
                (saleName == "KID'S EXCHANGE") ||
                (saleName == "NEARLY NEW CLOTHING EXCHANGE") ||
                (saleName == "NEARLY NEW"))  
             {
                 // alert("VALID sale name");
             }
             else if ((saleName == "KIDSEVERYWEAR") ||  
                      (saleName == "KIDS EVERYWEAR"))
             {
                    alert("Previous Kids EveryWear codes and tags will not be accepted. Only new barcoded tags are allowed!");
                    previousKidsEveryWear = true;
             }
             else
             {
                   invalidSale = true;  
             }
   }
   else if (code.length >= 1 )
   {
           alert ("A Sale Name must be specified before a code can be entered"); 
           document.frmContactInfo.Previous_Code.value = '';
   }
   if (invalidSale)
   {
           alert(saleName + " is not a recognized sale.");
           var code = document.frmContactInfo.Previous_Code.value;
           if (trim(code) == "")
               code = "________";
           if (saleName == "")
               saleName = "____________"
           window.location = "mailto:gail@kidseverywear.com?subject=Existing Code Request&body=I have an existing barcode from another sale. The sale is " + saleName + " and my code is " + code + ". What do I need to do to register\?";
    }       
    if (invalidSale || previousKidsEveryWear)
    {
           document.frmContactInfo.Use_Existing_Code.checked = false;
	   document.frmContactInfo.Previous_Code.disabled = true;
	   document.frmContactInfo.Previous_Sale.disabled= true;
	   document.frmContactInfo.Previous_Code.value = '';
	   document.frmContactInfo.Previous_Sale.value = '';
    } 
}

function trim(strText) 
{ 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

