﻿// JScript File

    var sOKText='Details OK!';
    var sProblemsText='<b>Problems found with the form</b>. Please check the following:';
    var bProblemWithForm=false;
    var pProblems;
    var pConfirmation;
    
    
function swapItemImage(NewImageName)
{
    //alert(NewImageName);
    var itemImage=document.getElementById('itemImage');
    itemImage.src='/images/items/alts/' + NewImageName;
}

function viewGalleryPicture(File)
{
    
    window.open('/images/gallery/' + File);
    
}

function viewRealBridePicture(File)
{
    
    window.open('/images/realbrides/' + File);
    
}



function getControl(controlName, Type)
{
    //alert('2a');
    var cont=getControlFullName(document, controlName, Type);
    //alert('Control found: ' + cont + ', requested ID: ' + controlName);
    cont=document.getElementById(cont);
    return cont;
}


function toggleShoeSizeOptions()
{

    //var ddlShoeSize=getControlFullName(document,'ddlShoeSize');
    var txtShoeLength=getControlFullName(document,'txtShoeLength','input');
    var txtShoeWidth=getControlFullName(document,'txtShoeWidth','input');
    var chkPickCustomSize=getControlFullName(document,'chkPickCustomSize','input');
    //var divCustomShoeSizeOptions=getControlFullName(document, 'divCustomShoeSizeOptions');
    
    var customOptionTicked=document.getElementById(chkPickCustomSize).checked;
    //alert(customOptionTicked);
    
    //alert(ddlShoeSize);
    var standardshoediv=document.getElementById('divStandardShoeSizeOptions');
    var slcontrol=document.getElementById(txtShoeLength);
    var sqcontrol=document.getElementById(txtShoeWidth);
    var customshoediv=document.getElementById('divCustomShoeSizeOptions');
    //alert(control.id);
    //alert(sscontrol.style.visibility);
    //alert(customshoediv.id);
    show(standardshoediv,!customOptionTicked);
    show(customshoediv,customOptionTicked);
}

function toggleVisibility(thisControl)
{
    if(thisControl.style.visibility!='visible' || thisControl.style.visibility==undefined)
        {
        show(thisControl,1);
        //alert('visible');
        }else{
        show(thisControl,0);
        //alert('invisible');
        }
}

function show(thisControl,show)
{
    //alert('show: ' + show);
    if(show==true)
        {
        //alert('trying to show ' + thisControl.id);
        thisControl.style.visibility='visible';
        thisControl.style.display='block';
        }else{
        //alert('trying to hide ' + thisControl.id);
        thisControl.style.visibility='hidden';
        thisControl.style.display='none';
        }
}

function isVisible(thisControl)
{
    var visible = 'true';
    //alert('test3 ' + thisControl);
    //alert(thisControl.style.display);
    if (thisControl.style.display=='none' || thisControl.style.display==null)
        {
        visible = 'false';
        }
        
    //alert(visible);
    return visible
}

function isChecked(thisControl)
{
    var checked='false';
    //alert('checked ' + thisControl.checked);
    if (thisControl.checked==true)
        {
        checked='true';
        }
    return checked;
}

function getDropDownValue(thisControl)
{
    //var return;
    return thisControl.options[thisControl.selectedIndex].value;
}




function checkShoeDetails()
{

    // **** RESET VALIDATION VARIABLES **** 
    var bReturn=false;
    bProblemWithForm=false;
    //alert('test');
    
    var ddlShoeSize=getControl('ddlShoeSize','select');
//    //alert(ddlShoeSize);
    //var chkPickCustomSize=getControl('chkPickCustomSize','input');
    var txtShoeLength=getControl('txtShoeLength','input');
    var txtShoeWidth=getControl('txtShoeWidth','input');
//    
    pProblems=document.getElementById('pProblems');
    pConfirmation=document.getElementById('pConfirmation');
    //alert(pProblems.id);
//    
    var ddlHeelHeight=getControl('ddlHeelHeight','select');
    var ddlLaceColour=getControl('ddlLaceColour','select');
    var ddlShoeColour=getControl('ddlShoeColour','select');
    var ddlGlitter=getControl('ddlGlitter','select');
    var ddlShoePostage=getControl('ddlShoePostage','select');
    var ddlHeelColour=getControl('ddlHeelColour','select');
    
    var chkReadTerms=getControl('chkReadTerms','input');

    pProblems.innerHTML='';
    pConfirmation.innerHTML='';
    
    //alert('test1');
    var customSizeChecked='false'//isChecked(chkPickCustomSize);
    if (customSizeChecked=='true')
        {
        //alert('custom option checked');
        var shoeWidth;
        var shoeLength;
        var swidth;
        var slength;
        shoeWidth=txtShoeWidth.value;
        shoeLength=txtShoeLength.value;
        //alert('custom shoe size; length:' + shoeLength + ', width: ' + shoeWidth);
        if (shoeLength=='')
            {
            showProblem('No custom shoe length entered');
            }else
            {
            showConfirmation('Custom shoe length of ' + shoeLength + ' selected');
            }
        if (shoeWidth=='')
            {
            showProblem('No custom shoe width entered');
            }else
            {
            showConfirmation('Custom shoe width of ' + shoeWidth + ' selected');
            }          
        }else
        {
        //alert('custom option NOT checked');
        if (ddlShoeSize.selectedIndex>0)
            {
            var size;
            size=getDropDownValue(ddlShoeSize);
            //alert('size ' + size + ' selected');
            }else
            {
            //alert('shoe size NOT selected');
            showProblem('No shoe size selected');
            }
        }
        
    //Check heel height
    //alert(ddlHeelHeight.id);
    if (ddlHeelHeight==null)
        {
        showConfirmation('heel height is not shown');
        }else
        {
        if (ddlHeelHeight.selectedIndex>0)
            {
            var heelHeight;
            heelHeight=getDropDownValue(ddlHeelHeight);
            showConfirmation('height ' + heelHeight + ' selected');
            }else
            {
            showProblem('No heel height selected');
            }
        }
        
    if (ddlHeelColour==null)
        {
        showConfirmation('heel colour is not shown');
        }else
        {
        if (ddlHeelColour.selectedIndex>0)
            {
            var heelColour;
            heelColour=getDropDownValue(ddlHeelColour);
            showConfirmation('Heel colour ' + heelColour + ' selected');
            }else
            {
            showProblem('No heel colour selected');
            }
        }
        
    //Check lace
    if (ddlLaceColour==null)
        {
        showConfirmation('lace colour is not shown');
        }else
        {
        if (ddlLaceColour.selectedIndex>0)
            {
            var laceColour;
            laceColour=getDropDownValue(ddlLaceColour);
            showConfirmation('lace colour ' + laceColour + ' selected');
            }else
            {
            showProblem('No lace colour selected');
            }
        }
        
    //Check shoe colour
    if (ddlShoeColour==null)
        {
        showConfirmation('shoe colour is not shown');
        }else
        {
        if (ddlShoeColour.selectedIndex>0)
            {
            var shoeColour;
            shoeColour=getDropDownValue(ddlShoeColour);
            showConfirmation('shoe colour ' + shoeColour + ' selected');
            }else
            {
            showProblem('No shoe colour selected');
            }            
        }
    //Check glitter
    if (ddlGlitter==null)
        {
        showConfirmation('glitter is not shown');
        }else
        {
        if (ddlGlitter.selectedIndex>0)
            {
            var glitter;
            glitter=getDropDownValue(ddlGlitter);
            showConfirmation('glitter ' + glitter + ' selected');
            }else
            {
            showProblem('No glitter selected');
            }     
            
        }               
    //Check shoe postage
    if (ddlShoePostage==null)
        {
        showConfirmation('shoe postage is not shown');
        }else
        {
        //if (ddlShoePostage.selectedIndex>0)
        //    {
            var shoepostage;
            //alert('About to get postage');
            shoepostage=getDropDownValue(ddlShoePostage);
            //alert('Got postage');
            showConfirmation('shoe postage ' + shoepostage + ' selected');
        //    }else
        //    {
        //    showProblem('No shoe postage selected');
        //    }     
        }        
        
    if (chkReadTerms==null)
        {
        showConfirmation('checkbox for terms and conditions not found');
        }else
        {
        if (chkReadTerms.checked==true)
            {
            //do nothing
            }else
            {
            showProblem('You must confirm that you have read, understood and agree to be bound by the Terms and Conditions');
            }
        }
        
            
            
           
    //Were there any problems??
    if (!bProblemWithForm)
        {
        pProblems.innerHTML=sOKText;
        bReturn='true';
        }
       
       //alert('problem ' + bProblemWithForm + ', return ' + bReturn);
       
    return bReturn;
}

function showProblem(ThisProblem)
{
    //alert('pProblems.innerHTML: ' + pProblems.innerHTML);
    if (pProblems.innerHTML=='')
        {
        pProblems.innerHTML=sProblemsText + '<br/>- ' + ThisProblem;
        }else
        {
        pProblems.innerHTML=pProblems.innerHTML + '<br/>- ' + ThisProblem;
        }
    bProblemWithForm=true;
}

function showConfirmation(ThisText)
{
    //alert('pProblems.innerHTML: ' + pProblems.innerHTML);
    if (pConfirmation.innerHTML=='')
        {
        //pConfirmation.innerHTML='<br/>- ' + ThisText;
        }else
        {
        //pConfirmation.innerHTML=pConfirmation.innerHTML + '<br/>- ' + ThisText;
        }
}

function getControlFullName(ParentControl,ControlToFind,type)
{
    var controlToFindFullName='';
    var thisControl;
    var thisControlChildren;
    
    var inputCollection = ParentControl.getElementsByTagName(type);
    //alert('looking for ' + ControlToFind + ', children of ' + ParentControl.id + ': ' + inputCollection.length);
    for (var i=0;i<inputCollection.length;i++) {
        //alert('repetition ' + i + ' of ' + inputCollection.length);
        //thisControl=ParentControl[i];
        thisControl=inputCollection[i];
        //alert('(' + i + ' of ' + inputCollection.length-1 + '): comparing ' + thisControl.id + ' to ' + ControlToFind + ': ' + thisControl.id.indexOf(ControlToFind));
        if (thisControl.id.indexOf(ControlToFind) != -1)
            {
            //alert('FOUND ' + ControlToFind + ': ' + thisControl.id);
            return thisControl.id
            }
        thisControlChildren=thisControl.getElementsByTagName(type);
        //alert('children of ' + thisControl.id + ': ' + thisControlChildren.length);
        if (thisControlChildren.length>0)
            {
            //alert('finding children of ' + thisControl.id);
            controlToFindFullName=getControlFullName(thisControl,ControlToFind);
            if (controlToFindFullName != '')
                {
                return controlToFindFullName;
                }
            }
    }
}

function checkNumberField(field, evnt)
{
    //alert('checking number field ' + field)
    var bReturn=false;
    
    var schar=(evnt.which)?evnt.which: event.keyCode;
    
    //alert(schar);
    
    //0-9 on top row of keyboard
    if (schar>=48 && schar<=57)
        {
        bReturn=true;
        }
    //0-9 on number keypad
    if (schar>=96 && schar<=105)
        {
        bReturn=true;
        }
    //full stop
    if (schar==190){bReturn=true;}
    //full stop on numeric keypad
    if (schar==110){bReturn=true;}
    //backspace
    if (schar==8){bReturn=true;}
    //delete
    if (schar==46){bReturn=true;}
    //left/right arrow keys
    if (schar==37 || schar==39){bReturn=true;}
    //Tab
    if (schar==9){bReturn=true;}
    
    
    return bReturn;
}


