﻿// JScript File
//*******************************************************************

function checkstock(sku)
    {
        if(sku == '') 
            {
                alert('You must enter a SKU!');
                return false;
            }
        if(sku.length != 5)
            {
                alert('SKU must be 5 digits!');
                return false;
            }
            
        if(!IsNumeric(sku))
            {
                alert('SKU must be 5 digits (numeric)!');
                return false;
            }

        var url = 'http://www.nourison.com/ajaxcode/default.aspx?T=checkstock&sku=' + sku;
        var ctl = 'irequest';
        ajaxFunction(url, ctl, true);
    }
    

function IsNumeric(sText)
    {
        var ValidChars = "0123456789";
        var IsNumber=true;
        var Char;
        for (i = 0; i < sText.length && IsNumber == true; i++) 
            { 
                Char = sText.charAt(i); 
                if (ValidChars.indexOf(Char) == -1) 
                    {
                        IsNumber = false;
                    }
            }
       return IsNumber;
    }

//***************************************************************************************************************************************
// use below for hard to find search
    function runquery()
        {
            var s = document.getElementById('shapecode').value;
            if(s != '' && s != 'null' && s != null)
                {
                    runshapequery(s);
                }
            else
                {
                    var w = document.getElementById('width').value;
                    var l = document.getElementById('length').value;
                    var url = 'http://www.nourison.com/ajaxcode/default.aspx?T=htfsearch&w=' + w + '&l=' + l;
		    htfbreadcrumbs(l,w, '');
                    var ctl = 'qresults';
                    ajaxFunction(url, ctl, true);
                }

        }
    function runshapequery(s)
        {
            var w = document.getElementById('width').value;
            var l = document.getElementById('length').value;
            //document.getElementById('shapecode').value = s;
	    htfbreadcrumbs(l,w, s);
            var url = 'http://www.nourison.com/ajaxcode/default.aspx?T=htfsearchbyshape&s=' + s + '&w=' + w + '&l=' + l;
            var ctl = 'qresults';
            ajaxFunction(url, ctl, true);
        }
    function clearquery()
        {
            document.getElementById('width').value='';
            document.getElementById('length').value='';
            document.getElementById('qresults').innerHTML='';
	    document.getElementById('shapecode').value = '';
	    document.getElementById('searchbc').innerHTML = 'No Current Selection';
        }
    function passmainparms()
        {
            var w = document.getElementById('width').value;
            var l = document.getElementById('length').value;
            //if(w != '' || l != '')
	    if((w != '' && w != 'null' && w != null) || (l != '' && l != 'null' && l != null))
                {
                    var url = 'main.aspx?form=htfq&w=' + w + '&l=' + l;
                    document.location.replace(url);
                    
                }
            else
                alert("No Sizes Entered!");
        }
    function passshapemainparms(s)
        {
            var w = document.getElementById('width').value;
            var l = document.getElementById('length').value;
            var url = 'main.aspx?form=htfq&w=' + w + '&l=' + l + '&s=' + s;
            document.location.replace(url);
        }
    function checkparms(qry)
        {
            var w = getParameter(qry, 'w' );
            var l = getParameter(qry, 'l' );
            var s = getParameter(qry, 's' );
	
            if(s != '' && s != 'null' && s != null) 
                {
		    runshapequery(s);
                }
            else
                {
                    if((w != '' && w != 'null' && w != null) || (l != '' && l != 'null' && l != null))
                        {
                            document.getElementById('width').value = w;
                            document.getElementById('length').value = l;
                            //var url = 'http://localhost:49517/ajaxstuff/default.aspx?T=htfsearch&w=' + w + '&l=' + l;
                            var url = 'http://www.nourison.com/ajaxcode/default.aspx?T=htfsearch&w=' + w + '&l=' + l;
                            var ctl = 'qresults';
			    htfbreadcrumbs(l,w, s);
                            ajaxFunction(url, ctl, true);
                        }
                }
        }

    function htfbreadcrumbs(l,w,s)
	{

		var msg = 'Search results for ';
		if(s != '' && s != null && s != 'null')
			{
				switch(s)
					{
					case 'RD':
						msg = msg + 'ROUNDS';
						break;
					case 'SQ':
						msg = msg + 'SQUARES';
						break;
					case 'OV':
						msg = msg + 'OVALS';
						break;
					case 'OVS':
						msg = msg + 'OVERSIZES';
						break;
					case 'OC':
						msg = msg + 'OCTAGONS';
						break;
					case 'OS':
						msg = msg + 'OVERSIZED PIECES';
						break;
					case 'RUN':
						msg = msg + 'RUNNERS';
						break;
					case 'MSC':
						msg = msg + 'MISCELLANEOUS';
						break;
					default:
						msg = msg + 'RECTANGLES';
						break;
					}
			}
		else
			{

				if(w != '' && w != null && w != 'null' && l != '' && l != null && l != 'null')
					{
						msg = msg + w + ' x ' + l;
					}
				if(w == '' || w == null || w == 'null' && l != '' && l != null && l != 'null')
					{
						msg = msg + 'Lengths: ' + l;
					}

				if(w != '' && w != null && w != 'null' && l == '' || l == null || l == 'null')
					{
						msg = msg + 'Widths: ' + w;
					}
			}
		
		document.getElementById('searchbc').innerHTML = msg;
	}

/*
    function htfbreadcrumbs(l,w,s)
	{
		var msg = 'Search results for ';

		if(w != '' && w != null && w != 'null')
			msg = msg + 'Width: ' + w + ', ';
		else
			msg = msg + 'Width: Any, ';

		if(l != '' && l != null && l != 'null')
			msg = msg + 'Length: ' + l + ', ';
		else
			msg = msg + 'Length: Any, ';
		if(s != '' && s != null && s != 'null')
			{
				switch(s)
					{
					case 'RD':
						msg = msg + 'Shape: Round';
						break;
					case 'SQ':
						msg = msg + 'Shape: Square';
						break;
					case 'OV':
						msg = msg + 'Shape: Oval';
						break;
					case 'OC':
						msg = msg + 'Shape: Octagon';
						break;
					case 'OS':
						msg = msg + 'Oversized Pieces';
						break;
					case 'RU':
						msg = msg + 'Runner';
						break;
					default:
						msg = msg + 'Shape: Rectangle';
						break;
					}
			}
		else
			msg = msg + 'Shape: Any ';

		document.getElementById('searchbc').innerHTML = msg;
		
	}

*/

//***********************************************************************************************************************************************

    function getParameter ( queryString, parameterName ) 
        {   
        // Add "=" to the parameter name (i.e. parameterName=value)   
        var parameterName = parameterName + "=";   
        if ( queryString.length > 0 ) 
            {      
                // Find the beginning of the string      
                begin = queryString.indexOf ( parameterName );      
                // If the parameter name is not found, skip it, otherwise return the value      
                if ( begin != -1 ) 
                    {         
                        // Add the length (integer) to the beginning         
                        begin += parameterName.length;         
                        // Multiple parameters are separated by the "&" sign         
                        end = queryString.indexOf ( "&" , begin );      
                        if ( end == -1 ) 
                            {         
	                            end = queryString.length      
                            }      
                        // Return the string      
                        return unescape ( queryString.substring ( begin, end ) );   
                    }   
                // Return "null" if no parameter has been found   
                return "null";   
            }
        }
// use above for hard to find search
//*******************************************************************
function popupselector(id)
    {
        var async=false;
        var url='AjaxGetInfo.aspx?TYPE=';
        var ctl='selbox';
        switch(id.toLowerCase())
            {
                case 'cometorders':
                    url = url + 'nrordpehrefs';
                    break;
                case 'cometaccts':
                    url = url + 'nrordpehaccts';
                    break;
                default:
                    return false;
                    break;
            }
            
       ajaxFunction(url, ctl, async);
            
        /*
        if(!ajaxFunction(url, ctl, async))
            {
                document.getElementById('divoverlay').style.visibility='hidden';
                document.getElementById('divperms').style.visibility='hidden';
            }
       */
    }
//I use this ajax function for anything that can be created on the server side and just sent back as html.
//url = the webpage that contains the routine being called. I use getajaxinfo.aspx and include a parameter so it knows what routine to call
//      if I call LocationsList box the url is getajaxinfo.aspx?TYPE=locationlistbox
//ctl is the span tags you have put on the html page that you want the return data to go to. Span work better than div because it has no 
//      impact on the flow of the page. Give each span tag an id. It does not have to have the runat=”server” element
//async is always true or it won’t work in FF
function ajaxFunction(url, ctl, async)
    {
        document.getElementById(ctl).innerHTML='<p style="width:100%; height:100%; vertical-align:middle; text-align:center;"><img alt="" src="Images/ajax-loader.gif"></p>';
        var xmlHttp;
        try
            {  // Firefox, Opera 8.0+, Safari  
                xmlHttp=new XMLHttpRequest();  
            }
        catch (e)
            {  // Internet Explorer  
                try
                    {    
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
                    }
                catch (e)
                    {    
                        try
                            {      
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
                            }
                        catch (e)
                            {      
                                alert("Your browser does not support AJAX!");      
                                document.getElementById(ctl).innerHTML='';
                                return false;      
                            }   
                    }  
           }
        xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                    {
                        try
                            {
                                var obj = document.getElementById(ctl);
                                obj.innerHTML = xmlHttp.responseText;
                                return true;
                            }
                        catch (e)
                            {
                                
                                alert(e.message);
                                return false;
                            }
                    }
                else
                    {
                        //alert(xmlHttp.readyState);
                    }
            }
        xmlHttp.open("GET",url,async);
        xmlHttp.send(null);  
    }

function ajaxFunctionWithPOPUP(url, ctl, async, popup)
    {
        document.getElementById(ctl).innerHTML='<p style="width:100%; height:100%; vertical-align:middle; text-align:center;"><img alt="" src="Images/ajax-loader2.gif"></p>';
        var xmlHttp;
        try
            {  // Firefox, Opera 8.0+, Safari  
                xmlHttp=new XMLHttpRequest();  
            }
        catch (e)
            {  // Internet Explorer  
                try
                    {    
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
                    }
                catch (e)
                    {    
                        try
                            {      
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
                            }
                        catch (e)
                            {      
                                alert("Your browser does not support AJAX!");      
                                document.getElementById(ctl).innerHTML='';
                                return false;      
                            }   
                    }  
           }
        xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                    {
                        try
                            {
                                var obj = document.getElementById(ctl);
                                obj.innerHTML = xmlHttp.responseText;
                                document.getElementById('divoverlay').style.visibility='visible';
                                //document.getElementById('divperms').style.visibility='visible';
                                return true;
                            }
                        catch (e)
                            {
                                document.getElementById('divoverlay').style.visibility='hidden';
                                document.getElementById('divperms').style.visibility='hidden';
                                document.getElementById('imgview').style.visibility='hidden';
                                alert(e.message);
                                return false;
                            }
                    }
                else
                    {
                        //alert(xmlHttp.readyState);
                    }
            }
        xmlHttp.open("GET",url,async);
        xmlHttp.send(null);  
    }

//I use signin and ajaxSignOn function for sign ons because in this case I only needed feedback to the same form if it fails other wise it 
//redirects to another page. 
//url = the webpage that contains the routine being called. The signin function shows the paarmeters I use to call the ajaxSignOn function
//ctl in this case is the span tags you have put on the html page that you want the return data to go to. Return data in my case only occurs when the sign on  fails
//async is always true or it won’t work in FF

function signin()
    {
        document.getElementById('signinmessage').innerHTML='';
        var u=document.getElementById('userid').value;
        var p=document.getElementById('pwd').value;
        ajaxSignOn('getajaxinfo.aspx?TYPE=signin&u=' + u + '&p=' + p, 'signinmessage', false);
    }

function ajaxSignOn(url, ctl, async)
    {
        document.getElementById(ctl).innerHTML='<p style="width:100%; height:100%; vertical-align:middle; text-align:center;"><img alt="" src="Images/ajax-loader2.gif"></p>';
        var xmlHttp;
        try
            {  // Firefox, Opera 8.0+, Safari  
                xmlHttp=new XMLHttpRequest();  
            }
        catch (e)
            {  // Internet Explorer  
                try
                    {    
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
                    }
                catch (e)
                    {    
                        try
                            {      
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
                            }
                        catch (e)
                            {      
                                alert("Your browser does not support AJAX!");      
                                document.getElementById(ctl).innerHTML='';
                                return false;      
                            }   
                    }  
           }
        xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                    {
                        try
                            {
                                m = xmlHttp.responseText;
                                if(m=='ok')   
                                    {
                                        window.location.replace('checkreset.aspx');
                                    }
                                else
                                    {
                                        document.getElementById(ctl).innerHTML= m + '<br />';
                                        //alert(m);
                                    }
                            }
                        catch (e)
                            {
                                
                                alert(e.message);
                                return false;
                            }
                    }
                else
                    {
                        //alert(xmlHttp.readyState);
                    }
            }
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);  
    }

function ajaxGetMsg(url, async)
    {
        var xmlHttp;
        try
            {  // Firefox, Opera 8.0+, Safari  
                xmlHttp=new XMLHttpRequest();  
            }
        catch (e)
            {  // Internet Explorer  
                try
                    {    
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
                    }
                catch (e)
                    {    
                        try
                            {      
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
                            }
                        catch (e)
                            {      
                                alert("Your browser does not support AJAX!");      
                                document.getElementById(ctl).innerHTML='';
                                return false;      
                            }   
                    }  
           }
        xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                    {
                        try
                            {
                                m = xmlHttp.responseText;
                                if(m=='ok')   
                                    {
                                        alert(m);
                                    }
                                else
                                    {
                                        //alert(m);
                                        alert(m);
                                    }
                            }
                        catch (e)
                            {
                                
                                alert(e.message);
                                return false;
                            }
                    }
                else
                    {
                        //alert(xmlHttp.readyState);
                    }
            }
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);  
    }
