/*
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
* 
*/

 
//#################################################
// Created Public FUNCTIONS    
function findInArray(arr, obj) {
    for(var i=0; i<arr.length; i++) {
        if (arr[i] == obj) return true;
    }
    return false;
}


function changeInputType(id,newType){
    input = document.getElementById(id);
    if(id != null){
        input.type=newType
    }
}
//#################################################


//#################################################
// ROLLOVER FUNCTIONS    style.color/backgroundcolor       
function bwhighlight(element,instance,colorSwap,name){
 if (name > 0){ 
    for (var n = 1; n < name; n++){ 
        var div = document.getElementById(element + n);
            if( div != null){
                 if (instance=='0'){     
                        div.style.color = colorSwap;
                 }else if (instance=='1'){          
                        div.style.backgroundColor = colorSwap;  
                 }
            }
        }
    var n = 1
 }else{
        var div = document.getElementById(element);                                    
        if (instance=='0'){     
                div.style.color = colorSwap;
         }else if (instance=='1'){    
                div.style.backgroundColor = colorSwap;  
         }
    }
}
             
// ROLLOVER FUNCTIONS    image swap                   
function rollover(element,imageSwap){ 
    var div = document.getElementById(element); 
    //if(div == null) alert (element+' '+imageSwap);
    //alert (element+' '+imageSwap);
    if(div != null) div.src = imageSwap;        
}                          

function rolloverBkg(element,BkgImageSwap){ 
    var div = document.getElementById(element); 
    if(div != null) div.style.backgroundImage="url("+BkgImageSwap+")";        
}    

function buttonDisplayHighlight(id,style){
    var left = document.getElementById('left'+id); 
    var middle = document.getElementById('middle'+id); 
    var right = document.getElementById('right'+id);
    if( (left != null) && (middle != null) && (right != null) ){
        var imageLeft = 'images/display/buttons/buttonParts/'+style+'/left.png';
        var imageMiddle = 'images/display/buttons/buttonParts/'+style+'/middle.png';
        var imageRight = 'images/display/buttons/buttonParts/'+style+'/right.png';
        rolloverBkg('left'+id,imageLeft);    
        rolloverBkg('middle'+id,imageMiddle);    
        rolloverBkg('right'+id,imageRight);    
    }
    
}


                           
// ROLLOVER CURSOR CHANGE:                                                       
function cursor_hand() {
    document.body.style.cursor = 'pointer';
}

function cursor_clear() {
    document.body.style.cursor = 'default';
}


function revealHide(revealID, hideID){
    var reveal = document.getElementById(revealID);
    var hide = document.getElementById(hideID);
    if((reveal != null) && (hide != null)){
        hide.style.display = 'none';
        hide.style.visibility = 'hidden';
        reveal.style.display = 'block';
        reveal.style.visibility = 'visible';
    }
}




//#################################################
//window functions
function open_window(URL,w, h, n, t,l,d,st,m,sc,r){   
    var win =window.open(URL, n,"toolbar="+t+",loaction="+l+",directories="+d+",status="+st+",menubar="+m+",scrollbars="+sc+",resizeable="+r+",width="+w+", height="+h+"");
    win.focus();
}
 
function close_window(){   
    window.close();
}

function close_refresh_window(){   
    window.close();              
    window.opener.location.reload();  
}





//#################################################
// GENERAL FUNCTIONS
function print_me(element) {
    var div =document.getElementById(element);                                                           
    div.print();
}

// RE-DIRECT USERS
function js_redirect(URL){
    window.location = URL;
}

// toggle Divs                   
function toggleDiv(divid, img){
    if(document.getElementById(divid).style.display == 'none'){
        document.getElementById(divid).style.display = 'block';
        document.getElementById(divid).style.visiblity = 'visible';
        if( img !== '0'){
            document.getElementById(img).src = "[URL]/images/display/buttons/minus.gif";  
        }
    }else{
        document.getElementById(divid).style.display = 'none';
        document.getElementById(divid).style.visiblity = 'hidden';
        if( img !== '0'){
            document.getElementById(img).src = "[URL]/images/display/buttons/plus.gif";  
        }
    }
    
}

// change bkg color
function changeBackgroundColor(id, color){    
    document.getElementById(id).style.backgroundColor = color;
}

//trim
function trim(str){
    var trimmed = str.replace(/^\s+|\s+$/g, '') ;
    return trimmed;
}



//#################################################
// FORMATTING FUNCTIONS   
// NUMBER FORMATTING:          
function numbersonly(e, decimal, id) {
    var key;
    var keychar;                                      
    var str = document.getElementById(id).value;  
         
    if (window.event) {
       key = window.event.keyCode;
    }else if (e) {
       key = e.which;
    }else {
       return true;
    } 
    keychar = String.fromCharCode(key);

    
    var allowedKeys=new Array(0,8,9,10,11,12,13,27,28,29,30,31);
    var allowedCtlKeys=new Array(82);
    if ((key==null) || (findInArray(allowedKeys, parseInt(key))) ){     
        return true;
    }else if((e.ctrlKey) || (e.altKey)){
        /*
        if((str != null) && (str != '')){
            var digits = str.split('');
            var newStr;
            var decimalAdded = false;
            for(i in digits){
                if(("0123456789").indexOf(digits[i]) > -1){
                    newStr = + digits[i];
                }else if ((decimal==true) && (digits[i] == ".")) {   
                    if (decimalAdded == false){
                        newStr =+ digits[i];
                    }                             
                }                                                            
            }
            alert(str +':'+ newStr)
            if(newStr != '') {
                document.getElementById(id).value = newStr;
                return;
            }
        }
        */
        return true;
    }else if ((("0123456789").indexOf(keychar) > -1)) {
        return true;                      
    }else if ((decimal==true) && (keychar == ".")) {   
        var substrings = str.split('.');
        var instances = substrings.length;   
        if( instances > 1 ){
            return false;            
        }
        return true;        
    }else{
        return false;
    }
}


function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}

function stripAlphaChars(pstrSource) { 
    var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, '');
    return m_strOut; 
}

function addCommas(nStr){
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}


// string Formatting:
function addslashes(str) {
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\0/g,'\\0');
    return str;
}

function stripslashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\\\/g,'\\');
    str=str.replace(/\\0/g,'\0');
    return str;
}



/*
################################################################################
DropDown Menus :: BEGIN
################################################################################
*/               
//DROP-DOWN MENUS  - open & close MENUS
// menu default state:
menuClick='off';   
activeMenu = '';
activeFolder = '';
// open/close menu:
function mopenClose(id, imgFolder){    
    //alert(menuClick);       
    if(menuClick == null) menuClick = 'off';
    if(menuClick=='off'){
        menuClick = 'on';
    }else{
        menuClick = 'off';
    }           
    if(activeMenu == null) activeMenu = '';
    if(id == null) id = '';
    if(menuClick=='on'){
        activeMenu = id;
    }else{                
        activeMenu = '';
    }        
    if(activeFolder == null) activeFolder = '';
    if(imgFolder == null) imgFolder = '';
    if(menuClick=='on'){
        activeFolder = imgFolder;
    }else{                
        activeFolder = '';
    }     
}   
// open menu:          
function mopen(id, imgFolder){    
    //validateLogInFormBkg();
    if(activeMenu == null) activeMenu = '';                    
    if( (id != activeMenu) && (activeMenu !== '') ){ 
        //if user rolls over another item, turn the active one off:                  
        var activeMenuItem = document.getElementById(activeMenu);
        if(activeMenuItem != null) {
            activeMenuItem.style.visibility = 'hidden';   
            var activeMenuImg = document.getElementById(activeMenu+'img');  
            if(activeMenuImg != null) activeMenuImg.src = activeFolder+'imageOff.png';   
            mopenClose(activeMenu, imgFolder);                    
        }                                                                                         
    }         
    menuitem = document.getElementById(id);
    menuitem.style.visibility = 'visible';
    var menuitemImg = document.getElementById(id+'img');
    if(menuitemImg != null) menuitemImg.src = imgFolder+'imageOn.png';   
   
}  
                                
function changeFormAction(){
    var form = document.getElementById('location');
    var location = document.getElementById('location').value;
    if(location == '1'){
        document.forms['logInFormForm'].action = 'http://66.104.241.105/website_aspera_login.asp';
    }else{         
        document.forms['logInFormForm'].action = '';   
    }
}    
// close menu:    
function mclose(id, imgFolder){      
    if(menuClick == null) menuClick = 'off';                      
    if(menuClick=='off'){
        menuitem = document.getElementById(id);
        if(menuitem) menuitem.style.visibility = 'hidden';  
        var menuitemImg = document.getElementById(id+'img');
        if(menuitemImg != null) menuitemImg.src = imgFolder+'imageOff.png';      
    }else{
    }
}   

function styleLogInForm(id,state,bkgImage){
    var input = document.getElementById(id);
    if(input !== null){      
        /*
        if(id=='username'){
            */
        if(state== 'on'){
            if(input.value == '')input.style.backgroundImage = '';
        }else{   
            if(input.value == '')input.style.backgroundImage = "url('"+bkgImage+"')";
        }
            /*
        }else if(id=='password'){  
            if(state== 'on'){
                if(input.value == '')input.style.backgroundImage = 'images/display/logInFormBkg'+id+'.png'
            }else{   
                if(input.value == '')input.style.backgroundImage = 'images/display/logInFormBkg'+id+'.png'
            }
            
        }
        */
    }
}           

/*
################################################################################
DropDown Menus :: END
################################################################################
*/  
 
 
       

/*
################################################################################
Forms Validations :: begin
################################################################################
*/  
function validateLogInFormBkg(){
    var username = document.getElementById('username');
    if(username !== null){
        if(username.value !== ''){
            username.style.backgroundImage ='';
        }  
    }                                                                                                       
    var password = document.getElementById('password');
    if(password !== null){
        if(password.value !== ''){                                                           
            password.style.backgroundImage ='';
        }  
    }        
}  

function validateEmail(email){
    if(trim(email).search(/\s/) > 0) return false;
    var emailArray = trim(email).split('@');
    if(emailArray.length != 2) return false;
    var name = emailArray[0]; 
    var address = emailArray[1]; 
    if((name != null ) && (name != '') && (address != null ) && (address != '')){  
        if(name.search(/\s/) > 0) return false;
        if(address.search(/\s/) > 0) return false;
        var addressArray = address.split('.');
        if(addressArray.length != 2) return false;
        if((addressArray[0] != null ) && (addressArray[0] != '') && (addressArray[1] != null ) && (addressArray[1] != '')){
            return true;
        }
    }
    return false;
}
   

/*
################################################################################
Forms Validations :: end
################################################################################
*/     

/* 
##############################################
PAGE FUNCTIONS
##############################################
*/                         
function emailForm(email, name, action){    
    var formId = document.getElementById('emailForm'+email);
    var linkId = document.getElementById('emailLink'+email);
    if( (formId  != null) && (linkId  != null) ) {                
        if(formId.style.display == 'none'){   
            if( (action == '0') || (action == '1') || (action == '2') ){ 
                returnPage = 'contact';
            }else{
                returnPage = action;
            }
            var URL = 'includes/modules/email/ajax.php?action=getEmailForm&email='+email+'&name='+name+'&returnPage='+action;   
            var AjaxResponse = $j.ajax({   
                url: URL,
                async: false                      
            }).responseText;           
            var response = AjaxResponse.split('^^^');       
            if(response[0].replace(/\s/g, "")  == 'success'){   
                if( (response[1] != null) && (response[1] != '') ){
                    var formId = document.getElementById('emailForm'+email);  
                    formId.innerHTML = response[1];  
                } 
            }else{
                return;
            }  
            formId.style.display = 'block';
            formId.style.visibility = 'visible';
            linkId.style.display = 'none';
            linkId.style.visibility = 'hidden';   
        }else{                       
            linkId.style.display = 'block';
            linkId.style.visibility = 'visible';
            formId.style.display = 'none';
            formId.style.visibility = 'hidden';   
        }                
    }
    return;
}

function sendEmailForm(email){
    var formId = document.getElementById('emailFormForm'+email);  
    //var formLabel = 'emailForm'+email;  
    var emailId = document.getElementById('emailFrom'+email);
    var nameId = document.getElementById('emailName'+email);
    if( (formId  != null) && (emailId  != null) && (nameId  != null) ){    
        if(nameId.value  == ''){
            alert('you must enter your name before you can send the message');
            return;
        } 
        if(emailId.value  == ''){
            alert('you must enter your email address before you can send the message');
            return;
        }
        if(validateEmail(emailId.value) != true){
            alert('your email address entered is invalid');
            return;
        }   
        //document.getElementById('emailFormForm'+email).submit();
        formId.submit();
        
    }
    return;                
    
}


function sendMailingListForm(email){
    var formId = document.getElementById('mailingListForm');  
    //var formLabel = 'emailForm'+email;  
    var emailId = document.getElementById('mailingListFrom');
    var nameId = document.getElementById('mailingListName');
    if( (formId  != null) && (emailId  != null) && (nameId  != null) ){    
        if(nameId.value  == ''){
            alert('you must enter your name before you can send the message');
            return;
        } 
        if(emailId.value  == ''){
            alert('you must enter your email address before you can send the message');
            return;
        }
        if(validateEmail(emailId.value) != true){
            alert('your email address entered is invalid');
            return;
        }   
        //document.getElementById('emailFormForm'+email).submit();
        formId.submit();
        
    }
    return;                
    
}





/*
################################################################################
Overlay/ Lightbox Functions
################################################################################
*/     
    
function lightboxOverlay(data,dragId){     
         
    /*                               
    var imageContainer = document.getElementById('imageContainer');    
    if(imageContainer != null) {
        if( (data != null) && (data != '') ){     
            imageContainer.innerHTML = data;      
        }else{    
            imageContainer.innerHTML = '<img id="lightboxImage">' 
            +'<div id="hoverNav"><a id="prevLink" href="#"></a><a id="nextLink" href="#"></a></div>'
            +'<div id="loading"><a id="loadingLink" href="#"><img src="images/display/lightbox/loading.gif"></a></div>';            
        }
    }
    return;
    */   
    
    var lightBoxOverlayAll = document.getElementById('lightBoxOverlayAll');
    var lightBoxOverlayConatiner = document.getElementById('lightBoxOverlayConatiner');
    var lightBoxDataContainer = document.getElementById('lightBoxDataContainer');       
    
           
    if( (data != null) && (data != '') ){                       
        if(lightBoxDataContainer != null) lightBoxDataContainer.innerHTML = data;
            lightBoxDataContainer.style.top = '30%';
            /* for IE:  get position of cursor */
        /*
        if(lightBoxOverlayAll != null) {
            lightBoxOverlayAll.style.display = 'block';
            lightBoxOverlayAll.style.visibility = 'visible';     
        }        
        */
        if(lightBoxOverlayConatiner != null) {
            lightBoxOverlayConatiner.style.display = 'block';
            lightBoxOverlayConatiner.style.visibility = 'visible';     
        }        
        if(lightBoxOverlayConatiner != null) {
            /*
            position: fixed;    
            top: 0px;         
            margin:0px auto;   
            text-align:center;        
            */                                                     
        }  
        if( (dragId != null)&&(dragId != '') ){
            new Draggable(dragId);     
        }                     
    }else{             
        if(lightBoxDataContainer != null) lightBoxDataContainer.innerHTML = '';
        /*
        if(lightBoxOverlayAll != null) {
            lightBoxOverlayAll.style.display = 'none';
            lightBoxOverlayAll.style.visibility = 'hidden';
        } 
        */       
        if(lightBoxOverlayConatiner != null) {
            lightBoxOverlayConatiner.style.display = 'none';
            lightBoxOverlayConatiner.style.visibility = 'hidden';
        }    
    }      
    return;
}
    
function createDrag(dragId){                                      
    if( (dragId != null)&&(dragId != '') ){
        new Draggable(dragId);     
    }
    return;
}
                                    
  
function lightboxOverlayReposition(data,dragId,topPos){  
    var lightBoxOverlayAll = document.getElementById('lightBoxOverlayAll');
    var lightBoxOverlayConatiner = document.getElementById('lightBoxOverlayConatiner');
    var lightBoxDataContainer = document.getElementById('lightBoxDataContainer');       
    
           
    if( (data != null) && (data != '') ){                       
        if(lightBoxDataContainer != null) {
            lightBoxDataContainer.innerHTML = data;
            lightBoxDataContainer.style.top = topPos;   
        }
        if(lightBoxOverlayConatiner != null) {
            lightBoxOverlayConatiner.style.display = 'block';
            lightBoxOverlayConatiner.style.visibility = 'visible';      
        }        
        if(lightBoxOverlayConatiner != null) {
            /*
            position: fixed;    
            top: 0px;         
            margin:0px auto;   
            text-align:center;        
            */                                                     
        }  
        if( (dragId != null)&&(dragId != '') ){
            new Draggable(dragId);     
        }                     
    }else{             
        if(lightBoxDataContainer != null) {
            lightBoxDataContainer.innerHTML = '';  
        }
        /*
        if(lightBoxOverlayAll != null) {
            lightBoxOverlayAll.style.display = 'none';
            lightBoxOverlayAll.style.visibility = 'hidden';
        } 
        */       
        if(lightBoxOverlayConatiner != null) {
            lightBoxOverlayConatiner.style.display = 'none';
            lightBoxOverlayConatiner.style.visibility = 'hidden';
        }    
    }      
    return;
}



function customerPhotosPopup(image,title){
    var URL = 'includes/modules/displayElements/ajax.php?action=getImageDetail&image='+image+'&title='+title;   
    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 dragId = '';
            if( (response[2] != null) && (response[2] != '') )dragId = response[2];                
            lightboxOverlay(response[1], dragId);
        } 
    }     
    return;
}       

/*
################################################################################
Image Gallery
################################################################################
*/                                
function openImageGallery(imageFolder,defaultImage,title){
    if((imageFolder == null)||(imageFolder == ''))return;
    var URL = 'includes/modules/imageGallery/ajax.php?action=getImageGallery&imageFolder='+imageFolder+'&defaultImage='+defaultImage+'&title='+title;   
    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 dragId = '';
            if( (response[2] != null) && (response[2] != '') )dragId = response[2];                
            //lightboxOverlayReposition(response[1], dragId, '0');
            lightboxOverlayReposition(response[1], dragId, '0');
        } 
    }
    
}
function loadImageGalleryPhoto(image){
    if((image == null)||(image == ''))return;  
    var URL = 'includes/modules/imageGallery/ajax.php?action=validateImage&image='+image;   
    var AjaxResponse = $j.ajax({   
        url: URL,
        async: false                      
    }).responseText;           
    var response = AjaxResponse.split('^^^');     
    //if(response[0] == 'success'){ 
    if(response[0].replace(/\s/g, "")  == 'success'){  
        var imageGalleryPhoto = document.getElementById('imageGalleryPhoto');
        if (imageGalleryPhoto != null){
            imageGalleryPhoto.src = image;
        }
    }   
}
