﻿function Char13Return(e,postbackcontrol)
{
    var key;
    if(window.event)
    {
        key = e.keyCode;
    }
    else
    {
        key = e.which;
    }
    
    if(key==13)postbackcontrol.click();
    else return true;
}
function VaildateMin(source, arguments)
{
var temp = arguments.Value.replace(/[^\d]*/,"");
var myNumber = Number(temp.trim());arguments.IsValid = false;
if(myNumber != null){var mymin;
if(source.MinimumValue) mymin = Number(source.MinimumValue);
else {mymin = Number(source.attributes.MinimumValue.value);}
if(mymin!=null){arguments.IsValid = myNumber>=mymin;} }
}
var quantityhold = "";
function NumbersOnly(e)
{var keynum;var keychar;var numcheck;
if(window.event) // IE
{keynum = e.keyCode;} else {
if(e.which) // Netscape/Firefox/Opera
{keynum = e.which;}else return true;}
keychar = String.fromCharCode(keynum);
numcheck = /\d/;if(keynum<33) return true;
else return numcheck.test(keychar);
}

    //assumes the error span is next sibbling and field is an input
    //invalidList is an array to hold the error spans to display for non valid fields
    function ValidateField(field)
    {
        var valid = true;
        var requiredValue;
        var hasRequired = false;
        if(field.required)
        {
             hasRequired = true;          
             requiredValue = field.required;//if it exist and is IE
        }
        else 
        {
            if(field.attributes != null && field.attributes.required != null)
            {
                hasRequired = true;
                requiredValue = field.attributes.required.value;//if it exists and is non IE browser
            }
            else hasRequired = false;
        }
        if(hasRequired)
        {
            
            if(!field.value || field.value.length==0)
            {
                valid = false;
            }
            if(requiredValue!="true")//see if it requires parsing of value
            {
                var testvalue = null;
                switch(requiredValue)
                {
                    case "mm/dd/yyyy":
                        if( Date.parseInvariant(field.value) == null)
                        {
                            valid = false;
                        };
                        break;
                    case ">today":
                        testvalue = Date.parseInvariant(field.value);
                        if(testvalue!= null)
                        {//compare with today's date
                            var today = new Date();
                            if(testvalue<=today)
                            {
                                valid = false;
                            }
                        }
                        break;
                    case "today+":
                        testvalue = Date.parse(field.value);
//                        testvalue = Date.parseInvariant(field.value);
                        if(testvalue!= null)
                        {//compare with today's date
                            var today = new Date();
                            today.setHours(0);
                            today.setMinutes(0);
                            today.setSeconds(0);
                            today.setMilliseconds(0);
                            if(testvalue<today)
                            {
                                valid = false;
                            }
                        }
                        break;
                    case "email":
                        var matches = field.value.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i);
                        if(matches)
                        {
                            valid = (matches[0]==field.value.trim());
                        }
                        else valid = false;
                        break;
                    case "password":
                        valid=true;
                        break;                        
                }//end switch
            }//end requiredValue!=true
        }
        else
        {
            valid = true;
        }
        
        if(!valid)
        {
          var errorSpan = null;//first check if a specific span id was set for the error text
          if(field.errorspan)
          {
            errorSpan = document.getElementById(field.errorspan);
          }
          else
          {
             if(field.attributes != null && field.attributes.errorspan != null)
             errorSpan = document.getElementById(field.attributes.errorspan.value);
          }
          if(errorSpan) errorSpan.style.display= "";//show the error span
          else//there was a specific error span
          {
             errorSpan = field.parentNode.getElementsByTagName("span");//check for a general error span in the parent element
             if(errorSpan)
             {
                 for(var e in errorSpan)
                 {
                    if(errorSpan[e].required)
                    {
                        errorSpan[e].style.display= "";//show the error span 
                    }
                    else
                    {
                        if(errorSpan[e].attributes != null && errorSpan[e].attributes.required != null)
                        errorSpan[e].style.display= "";//show the error span
                    }
                 }
             }
          }
 
        }
        
        return valid;
    }
    
    //errorSpans is the array of error spans being used for validation on the object
    function ClearValidationErrors(errorSpans)
    {       
        var hasRequired ;
        for(var e in errorSpans)
        {   
            if(errorSpans[e].required) hasRequired = true;//if it exist and is IE
            else 
            {
                if(errorSpans[e].attributes != null && errorSpans[e].attributes.required != null)hasRequired = true;//if it exists and is non IE browser
                else hasRequired = false;
            }
            if(hasRequired)
            {
                errorSpans[e].style.display = "none";
            } 
        }
    }
    

//args = type||value 1==string, 2==elementID to be converted to refrence to element,3==pagemethod to call
//last two elements are editTitle,populateMethod, as strings no type||value format
//needs to handle multiple edit clicks--if the call back is the same cancel, othwerwise cklear the old call back
var currentEditContext;
function EditOrderBarElement(args)
{
    var context = new Array();//first element is always going to be callback, followed by the elements passed in through args
    var callback = Function.createCallback(EditCallBack,context);
    if(currentEditContext)
    {
        currentEditContext[0](true);
    }
    
    Array.add(context,callback);
    for(var loop = 0; loop< (args.length-2); loop++)
    {
        var next = args[loop].split("||");
        if(next.length==2)
        {
            switch(next[0])
            {
                case "1"://string
                    Array.add(context,next[1]);
                    break;
                case "2"://element
                    Array.add(context,document.getElementById(next[1]));
                    break;
                case "3"://pagemethod
                    Array.add(context,eval("PageMethods."+next[1]));
                    break;
            }
            
        }
        else
        {
            Array.add(context,args[loop]);
        }
    }
    if(args.length>1)
    {
        DisplayNextStep(false,false);
        UpdateEditTitle(args[args.length-2]);
        var method = eval(args[args.length-1]);
        var editDisplay = {};//add an element to the array for canceling the display
        Array.add(context,editDisplay);
        currentEditContext = context;
        method(context);
    }
}

function EditCallBack(cancel, context)
{ 
    ShowOrderEdit(context[context.length-1], false);
    UpdateEditTitle("");
    DisplayNextStep(true,cancel);
    currentEditContext = null;
    if(cancel)return true;
}

function ShowOrderEdit(elementID, display)
{
    if(display)document.getElementById(elementID).style.display = "";
    else document.getElementById(elementID).style.display = "none";
}

//context[0]=focus field ID
//context[1] if !null is "required *" span field ID
var previousRequiredSpan;

function RadioButtonSetFocusAndRequired(evt, context)
{
    if(context.length>0) document.getElementById(context[0]).focus();
    if(previousRequiredSpan && previousRequiredSpan.length>0)
    {   for(var e in previousRequiredSpan)
        {   
            if(e==0)continue;
            document.getElementById(previousRequiredSpan[e]).style.display="none";
        }
    }
    if(context.length>0)
    {
        previousRequiredSpan = context;
        for(var e in previousRequiredSpan)
        {
            if(e==0)continue;
            document.getElementById(previousRequiredSpan[e]).style.display="";
        }
    }
    else
    {
        previousRequiredSpan= null;
    }
}

//args = type||value
//first two elements are sessionkey and PageMethodForUpdating,
//then displayColSize, editColSize, displayTitle,editTitle
//then any values specific to edit control
//last is populate method
var profileEditContext;
function EditProfileElement(args)
{
    var context = new Array();//first element is always going to be callback, followed by the elements passed in through args
    var callback = Function.createCallback(EditProfileCallBack,context);
    if(profileEditContext)//check if there is an exist edit in process and clear it out
    {
        profileEditContext[0](true);
    }
    Array.add(context,callback);
    for(var loop = 0; loop< (args.length-1); loop++)
    {
        var next = args[loop].split("||");
        if(next.length==2)
        {
            switch(next[0])
            {
                case "1"://string
                    Array.add(context,next[1]);
                    break;
                case "2"://element
                    Array.add(context,document.getElementById(next[1]));
                    break;
                case "3"://pagemethod
                    Array.add(context,eval("PageMethods."+next[1]));
                    break;
            }
            
        }
        else
        {
            Array.add(context,args[loop]);
        }
    }
    if(args.length>1)
    {
        var method = eval(args[args.length-1]);
        var editDisplay = {};//this will be set by populate method
        Array.add(context,editDisplay);
        profileEditContext = context;
        method(context);
    }
}

function EditProfileCallBack(cancel, context)
{
    //context[context.length-1]( false);
    profileEditContext = null;
    if(cancel)return true;
}

function UpdateProfileTitle(titleID,strTitle)
{
    $get(titleID).innerHTML = strTitle;
}
//returns a function that can show or hide edit functionality
function DisplayEdit(mainContentID,requiredInfoID,containingDivID,editTrigID,saveID,cancelID,wrappingDivsArray,displaySize,editSize,titleID,strDisplayTitle,strEditTitle)
{

    return function(boolEdit)
     {
        ShowEditSpans(mainContentID, boolEdit);
        $get('RequiredSpan').style.display=(boolEdit)?"":"none";
        ShowProfileEdit(containingDivID,(!boolEdit));
        DisplayEditButton(editTrigID,(!boolEdit));
        DisplayUpdateCancel(saveID,cancelID,boolEdit);
        if(boolEdit)
        {   
            ChangeColumnClass(wrappingDivsArray, editSize,displaySize);
            UpdateProfileTitle(titleID,strEditTitle);
        }
        else 
        {
            ChangeColumnClass(wrappingDivsArray, displaySize,editSize);
            UpdateProfileTitle(titleID,strDisplayTitle);
        }
    }
}

function DisplayEditButton(editID,display)
{
    $get(editID).style.display = (display)?"":"none";
}
function DisplayUpdateCancel(updateID,CancelID,display)
{
    $get(updateID).style.display = (display)?"":"none";
    $get(CancelID).style.display = (display)?"":"none";
}

 //this variable needs to be set by pageLoad fo any page that uses the following method
 //sets up for being able to selectively display only the edit element of profile

var profileSections = new Array();

function ShowProfileEdit(id_to_show,showall)
{
        if(profileSections)
        {
            for(var e in profileSections)
            {
                $get(profileSections[e]).style.display = (showall)?"":"none";
            }
        }
        if(!showall)$get(id_to_show).style.display = "";

}

function ShowEditSpans(containingElementID,showedit)
{
    var editspans = $get(containingElementID).getElementsByTagName('span');
    var myspantype=null;
    if(editspans && editspans.length>0)
    {
        for(var e in editspans)
        {
            myspantype=null
            if(editspans[e].spantype)myspantype=editspans[e].spantype;
            else
            {
                if(editspans[e].attributes != null && editspans[e].attributes.spantype != null)
                {
                    myspantype=editspans[e].attributes.spantype.value;
                }
                else myspantype = null;
            }
            if(myspantype)
            {
                if(showedit)
                {
                  editspans[e].style.display=(showedit & (myspantype=="edit"))?"":"none";  
                }
                else
                {
                    editspans[e].style.display=(myspantype=="display")?"":"none"; 
                }
            
            }
        }
    }
}

function ChangeColumnClass(wrapperArray, newsize,oldsize)
{
    if(wrapperArray && wrapperArray.length>1)
    {
        $get(wrapperArray[1]).style.display="none";
        //type is first value
        for(var loop = 1; loop<wrapperArray.length;loop++)
        {
            switch(loop)
            {
                case 1://wrapping dive
                    Sys.UI.DomElement.removeCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Wrap",oldsize,wrapperArray[0]));
                    Sys.UI.DomElement.addCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Wrap",newsize,wrapperArray[0]));
                    break;
                case 2://top div
                    Sys.UI.DomElement.removeCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Top",oldsize,wrapperArray[0]));
                    Sys.UI.DomElement.addCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Top",newsize,wrapperArray[0]));
                    break;
                case 3://title
                    Sys.UI.DomElement.removeCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Title",oldsize,wrapperArray[0]));
                    Sys.UI.DomElement.addCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Title",newsize,wrapperArray[0]));                
                    break;
                case 4://middle content
                    Sys.UI.DomElement.removeCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Mid",oldsize,wrapperArray[0]));
                    Sys.UI.DomElement.addCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Mid",newsize,wrapperArray[0]));
                    break;
                case 5://bottom div
                    Sys.UI.DomElement.removeCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Bottom",oldsize,wrapperArray[0]));
                    Sys.UI.DomElement.addCssClass($get(wrapperArray[loop]),String.format("{0}Col{1}Bottom",newsize,wrapperArray[0]));
                    break;
            }

        }
        $get(wrapperArray[1]).style.display="";
    }
}


                            

							
