/*
c. jeremy van winkle. 2011 - brothers van winkle / visualstoryline  -> www.brothersvanwinkle.com / www.visualstoryline.com
*/    

/* 
*
* created by visualstoryline - 2011
* site/code is for use by www.bernhardtwatch.com
* programming by jeremy van winkle - 01/20/2011
* c. www.visualstoryline.com
* 
*/

        
       
         

/*
################################################################################
Admin Scripts :: begin
################################################################################
*/    
function deleteProduct(productId){
    var confirmDelete = confirm(" are you sure you want to delete this product? \n ALL PRODUCT INFORMATION WILL BE DELETED. \n there is no undo for this.");
    if(confirmDelete == true) {
        if(productId != null) {
            var URL = 'includes/modules/actions/ajaxAdmin.php?action=deleteProduct&productId='+productId;
            var AjaxResponse = $j.ajax({
                url: URL,
                async: false
            }).responseText; 
                                                                         
            var response = AjaxResponse.split('^^^');    
            if(response[0].replace(/ /g, "").replace(/\n/g, "") == "success"){
                alert('product has been deleted.');
                window.location.reload(true);   
            }else{
                
                alert('unknownError:' + AjaxResponse);
            }           
        }   
    }else{
        alert('deletion Cancelled');
    } 
    return; 
}         

function validateAddProductName(type){
    var input = document.getElementById('productName');
    var output = document.getElementById('tabName');
    var submit = document.getElementById('submit');
    if( (input != null) && (output != null) && (submit != null) ){
        var name = input.value;
        if( (name != null) &&(name != '') ){
            submit.disabled = false;
        }else{
            submit.disabled = true;
        }   
        if(type == '2'){
            if(output.value == '') output.value = name;   
        }
    }
}

function validateAddAcessoriesName(){
    var input = document.getElementById('accessoriesName'); 
    var submit = document.getElementById('submit');
    if( (input != null) && (submit != null) ){
        var name = input.value;
        if( (name != null) &&(name != '') ){
            submit.disabled = false;
        }else{
            submit.disabled = true;
        }  
    }
}

function checkAddProductOption(id){
    var div = document.getElementById('newProductOptionDiv'+id);
    var option = document.getElementById('productOption'+id);
    if((div != null) && (option != null)){
        div.innerHTML = '<div id="newOptionDiv'+id+'" style="position:relative; left: 8px; border: 1px solid #000000; padding:6px;"  ><div id="newOptionName'+id+'" >New Option Name <input type="input" name="newProductOption'+id+'" id="newProductOption'+id+'" value="" /></div><div id="createNewOptionButton'+id+'"><a href="javascript:createNewOption(\''+id+'\');" >create Option</a></div><div id="createNewOptionValues'+id+'" ></div></div>';
        if(option.value == 'addNewOption'){
            div.style.display = 'block';
            div.style.visibility = 'visible';
            return;
        }
        //}else{
        
        var optionId = option.value;
        if(Number(optionId) >= 1){                             
            var URL = 'includes/modules/actions/ajaxAdmin.php?action=addProductOptionValues&optionId='+optionId;   
            var AjaxResponse = $j.ajax({   
                url: URL,
                async: false                      
            }).responseText;           
            var response = AjaxResponse.split('^^^');  
            //alert(AjaxResponse);            
            //if(response[0] == 'success'){      
            if(response[0].replace(/\s/g, "")  == 'success'){  
                if( (response[1] != null) && (response[1] != '') ){
                    div.innerHTML = response[1];
                    div.style.display = 'block';
                    div.style.visibility = 'visible';
                    return;
                }   
            }      
        }else{  
            div.style.display = 'none';
            div.style.visibility = 'hidden';
            return;
        }
        //}
    }           
}

// Create a new Value for an Existing Option         
function addNewProductOptionValuesDiv(optionId){   
    var URL = 'includes/modules/actions/ajaxAdmin.php?action=addNewProductOptionValuesDiv&optionId='+optionId;   
    var AjaxResponse = $j.ajax({   
        url: URL,
        async: false                      
    }).responseText;           
    var response = AjaxResponse.split('^^^');     
    //if(response[0] == 'success'){   
    if(response[0].replace(/\s/g, "")  == 'success'){  
        if( (response[1] != null) && (response[1] != '') ){   
            var newOptionNameDiv = document.getElementById('newOptionName'+optionId);
            var newOptionValueDiv = document.getElementById('newOptionValue'+optionId);
            var addNewValueButton = document.getElementById('addNewValueButton'+optionId);
            if(newOptionNameDiv != null) {
                newOptionNameDiv.innerHTML = response[2];
            }
            if(newOptionValueDiv != null) {
                newOptionValueDiv.innerHTML = response[1];
                newOptionValueDiv.style.display = 'block';
                newOptionValueDiv.style.visibility = 'visible';
                if(addNewValueButton != null) {
                    addNewValueButton.style.display = 'none';
                    addNewValueButton.style.visibility = 'hidden';
                }
            }
        } 
    }                                                   
} 
 
function addNewOptionValue(id){    
    if (id !=null && id != ""){
        var newValueName = '';
        var newValueCode = '';
        var newValueCost = '';
        newValueName = document.getElementById('newOptionValueName'+id).value;
        newValueCode = document.getElementById('newOptionValueCode'+id).value;
        newValueCost = document.getElementById('newOptionValueCost'+id).value;
        if( (newValueName != null) && (newValueName != '') && (newValueCode != null) &&(newValueCost != null) ){
            var URL = 'includes/modules/actions/ajaxAdmin.php?action=addNewProductOptionValue&optionId='+id+'&name='+newValueName+'&code='+newValueCode+'&cost='+newValueCost;   
            var AjaxResponse = $j.ajax({   
                url: URL,
                async: false                      
            }).responseText;           
            var response = AjaxResponse.split('^^^');   
            //if(response[0] == 'success'){   
            if(response[0].replace(/\s/g, "")  == 'success'){  
                if( (response[1] != null) && (response[1] != '') ){   
                    var div = document.getElementById('availableValues'+id);
                    if( div != null) div.innerHTML += response[1];
                    var divNewOptionButton = document.getElementById('addNewValueButton'+id);
                    var divNewOptionInput = document.getElementById('newOptionValue'+id);
                    if(divNewOptionButton != null) {
                        divNewOptionButton.style.display = 'block';
                        divNewOptionButton.style.visibility = 'visible';
                    }
                    if(divNewOptionInput != null) {
                        divNewOptionInput.style.display = 'none';
                        divNewOptionInput.style.visibility = 'hidden';     
                    }
                    //alert(response[2]);
                    //alert(response[3]);
                    if( (response[2] != null) && (response[2] != '') && (response[3] != null) && (response[3] != '') ){      
                        var win = window.frames["uploadOptionIconFrame"];
                        var windoc = win.document;
                        var elementID = windoc.getElementById('valueId');              
                        var newOptionValueIdInput = elementID;
                        //var newOptionValueIdInput = window.frames["uploadOptionIconFrame"].document.getElementById('valueId');
                        if(newOptionValueIdInput != null){
                            newOptionValueIdInput.value = response[3];
                            //var uploadIconForm = document.getElementById('uploadOptionIconForm');
                            var uploadIconForm = windoc.getElementById('uploadOptionIconForm');                            
                            if(uploadIconForm != null) {
                                uploadIconForm.submit();
                            }else{
                                alert('could not submit form');
                            }
                        }else{
                            alert('could not access value input');
                        }
                    }else{
                        alert('no response 2 or 3');
                    }                                
                } 
            }    
        } 
    }
    return;
}

//create new product option
function createNewOption(id){
    var optionNameInput = document.getElementById('newProductOption'+id);
    if( (optionNameInput != null) && (optionNameInput.value != '')){
        var optionName = optionNameInput.value;    
        var URL = 'includes/modules/actions/ajaxAdmin.php?action=addNewProductOption&name='+optionName+'&id='+id;   
        var AjaxResponse = $j.ajax({   
            url: URL,
            async: false                      
        }).responseText;           
        var response = AjaxResponse.split('^^^');     
        //if(response[0] == 'success'){ 
        if(response[0].replace(/\s/g, "")  == 'success'){   
            if( (response[1] != null) && (response[1] != '') ){   
                optionNameInput.innerHTML = 'Option Name: '+response[2];
                var createNewOptionValuesDiv = document.getElementById('createNewOptionValues'+id);
                var createNewOptionValuesButton = document.getElementById('createNewOptionButton'+id);
                if(createNewOptionValuesDiv != null) {
                    createNewOptionValuesDiv.innerHTML = response[3];
                    createNewOptionValuesDiv.style.display = 'none';
                    createNewOptionValuesDiv.style.visibility = 'hidden';
                    addNewProductOptionValuesDiv(response[1]);
                    if(createNewOptionValuesButton != null) {
                        createNewOptionValuesButton.style.display = 'none';
                        createNewOptionValuesButton.style.visibility = 'hidden';
                    }
                }else{
                    return;
                }
            }else{
                return;
            }
                                   
            var URL2 = 'includes/modules/actions/ajaxAdmin.php?action=getProductOptions&id='+id;   
            var AjaxResponse2 = $j.ajax({   
                url: URL2,
                async: false                      
            }).responseText;           
            var response2 = AjaxResponse2.split('^^^');      
            if((response2[0].replace(/\s/g, "") == 'success') && (response2[2].replace(/\s/g, "") != null) ){  
                var newOptionsArray = response2[2].split('***'); 
                var OptionList = document.getElementById('productOption'+id);
                if( OptionList != null ){ 
                    //clear option list
                    for (x = OptionList.length; x >= 0; x = x - 1) {
                        OptionList[x] = null;
                    }
                    //repopulate option list
                                       
                    for (i=0; i <= newOptionsArray.length; i = i+1){
                        if( (newOptionsArray[i] != null) &&(newOptionsArray[i] != '') ){
                            var newOptionData = newOptionsArray[i].split('==>'); 
                            if( (newOptionData[0] != null) && (newOptionData[0] != '') && (newOptionData[1] != null) && (newOptionData[1] != '') ){
                                try{
                                    OptionList.add(new Option(newOptionData[1], newOptionData[0]), null); //add new option to end of "sample"
                                }catch(e){ //in IE, try the below version instead of add()
                                    OptionList.add(new Option(newOptionData[1], newOptionData[0])); //add new option to end of "sample"
                                }
                                if (newOptionData[0] == response[1]){
                                    //alert('hi there');
                                    OptionList.selectedIndex = i;  
                                }
                            }
                        }
                    }                   
                    checkAddProductOption(id);
                    return;
                }                
            }       
        } 
    }
    return; 
    
}

function openEditProductWindow(id, productId){
    alert('this feature is not yet available');
    var page = 'editProducts_' + id;  
    var URL = 'admin/index.php?page='+page+'&productId='+productId;  
}

function deleteProductOptionToValue(id,urlReturn){                     
    var URL = 'includes/modules/actions/ajaxAdmin.php?action=deleteProductOptionToValue&id='+id;   
    var AjaxResponse = $j.ajax({   
        url: URL,
        async: false                      
    }).responseText;           
    var response = AjaxResponse.split('^^^');      
    if( (response[0].replace(/\s/g, "") == 'success') ){ 
        //window.location.reload();
        js_redirect(urlReturn);  
    }else{
        alert('failed to delete option.');
    } 
    return;
}

function deletePressReview(id,urlReturn){                     
    var URL = 'includes/modules/actions/ajaxAdmin.php?action=deletePressReview&id='+id;   
    var AjaxResponse = $j.ajax({   
        url: URL,
        async: false                      
    }).responseText;           
    var response = AjaxResponse.split('^^^');      
    if( (response[0].replace(/\s/g, "") == 'success') ){ 
        //window.location.reload();
        js_redirect(urlReturn);  
    }else{
        alert('failed to delete review.'+response);
    } 
    return;
}

function deleteWebReview(id,urlReturn){                     
    var URL = 'includes/modules/actions/ajaxAdmin.php?action=deleteWebReview&id='+id;   
    var AjaxResponse = $j.ajax({   
        url: URL,
        async: false                      
    }).responseText;           
    var response = AjaxResponse.split('^^^');      
    if( (response[0].replace(/\s/g, "") == 'success') ){ 
        //window.location.reload();
        js_redirect(urlReturn);  
    }else{
        alert('failed to delete review.'+response);
    } 
    return;
}



function deleteCustomerPhoto(file,urlReturn){                     
    var URL = 'includes/modules/actions/ajaxAdmin.php?action=deleteCustomerPhoto&file='+file;   
    var AjaxResponse = $j.ajax({   
        url: URL,
        async: false                      
    }).responseText;           
    var response = AjaxResponse.split('^^^');      
    if( (response[0].replace(/\s/g, "") == 'success') ){ 
        //window.location.reload();
        js_redirect(urlReturn);  
    }else{
        alert('failed to delete review.'+response);
    } 
    return;
}





function clearOrderNumberField(){
    var orderNumberInput = document.getElementById('orderNumber');
    var orderIdInput = document.getElementById('orderId');  
    if( (orderNumberInput != null) && (orderIdInput != null) ){
        if(orderIdInput.value != '0'){
            orderNumberInput.value = '';
        }
    }
    return;
}

function clearOrderIdField(){
    var orderNumberInput = document.getElementById('orderNumber');
    var orderIdInput = document.getElementById('orderId');  
    if( (orderNumberInput != null) && (orderIdInput != null) ){
        if(orderNumberInput.value != ''){
            orderIdInput.value = '0';
        }
    }
    return;
}

