/*Translation: Fields labels*/
var phoneLabel="Phone Number";
var emailLabel="Email";


/*Translation: Messages*/
var requiredFieldsMsg ="You should provide at least one of the following fields: ";
var requestSuccessMsg="Your direct contact request was successfully sent to Dutchworld Contact Mail box.";
var requestFailureMsg="Sorry, there was a problem with the request. Please try aigain.";



/*Allows to select a specific range in a html text input or textarea.*/
/*If selStart=selEnd, the cursor will be put at the begining of the text input component*/
function setSelRange(textInputObject, selStart, selEnd)
{   if (textInputObject.setSelectionRange)
    {   textInputObject.focus();
        textInputObject.setSelectionRange(selStart, selEnd);
    }
    else if (textInputObject.createTextRange)
    {   var range = textInputObject.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selEnd);
        range.moveStart('character', selStart);
        range.select();
    }
}

function get_radio_value()
{   for (var i=0; i < document.directform.rdgWhen.length; i++)
    {   if (document.directform.rdgWhen[i].checked)
        {   var radioValue = document.directform.rdgWhen[i].value;
        }
    }
    return radioValue;
}

function obtenerPosicionX(elemento)
{   var x=0;
    while(elemento)
    {   x += elemento.offsetLeft;
        elemento=elemento.offsetParent;
    }
    return x;
} 

function obtenerPosicionY(elemento)
{   var y=0;
    while(elemento)
    {   y += elemento.offsetTop;
        elemento=elemento.offsetParent;
    }
    return y;
} 


var tabsBeingDisplayed=Array();
var openedTabs= new Array();
/*Display tab panel and set tab icon. Does not control the floating positioning of the panel (i.e. top, left, ...)*/
function displayNonFloatingTabPanelAndSetTabIcon(event,tabPanelDivId, title, width, height, tabIconId, activeTabImgRelPath, inactiveTabImgRelPath)
{   e=event;
    //Do nothing if the display of a tab is running
    if(tabsBeingDisplayed.length>0)
    {   aTabId=tabsBeingDisplayed.pop();
        if(!((document.getElementById(aTabId).style.opacity=="") || (document.getElementById(aTabId).style.opacity==null)))
        {   tabsBeingDisplayed.push(aTabId); //The display process is still running on this tab
            return; //Do nothing
        }           
    }    
    //Close all opened tabs
    currTabWillBeOpened=true;
    while(openedTabs.length>0)
    {   tab=openedTabs.pop();
        closeTab(tab.iconId, tab.panelDivId, tab.inactiveTabImgRelPath);
        if(tab.iconId==tabIconId) {currTabWillBeOpened=false;}
    }
    //Display new tab
    displayNonFloatingDiv(tabPanelDivId, title, width, height);
    setCorrectTabIcon(tabIconId, tabPanelDivId, activeTabImgRelPath, inactiveTabImgRelPath);

    //Mark the current tab as opened if the display should cause it to be visible
    if(currTabWillBeOpened)
    {   currTab ={iconId : tabIconId, panelDivId : tabPanelDivId, inactiveTabImgRelPath: document.getElementById('dcDirectoryParent').value+inactiveTabImgRelPath};
        openedTabs.push(currTab);
    }
    //Notify that No display process is running on tab
    tabsBeingDisplayed.pop();
}

function tabDisplayNotFinish(tabPanelDivId)
{   //A display process is running on this tab
    if(!((document.getElementById(tabPanelDivId).style.opacity=="") || (document.getElementById(tabPanelDivId).style.opacity==null)))
    {   tabsBeingDisplayed.push(tabPanelDivId);
    }
}

function closeOpenedTabs()
{   while(openedTabs.length>0)
   {   tab=openedTabs.pop();
       closeTab(tab.iconId, tab.panelDivId, tab.inactiveTabImgRelPath);
   }
}

function closeTab(tabIconId, tabPanelDivId, inactiveTabImgRelPath)
{   if (document.getElementById(tabPanelDivId).style.display != "none")
    {   Effect.toggle(tabPanelDivId,'appear');
        //Set the tab image to the inactive one
        if((document.getElementById(tabPanelDivId).style.opacity=="") || (document.getElementById(tabPanelDivId).style.opacity==null))
        {   //document.getElementById(tabIconId).src=inactiveTabImgRelPath;
            document.getElementById(tabIconId).style.backgroundImage="url("+inactiveTabImgRelPath+")";
        }
    }
}

//DDI
function displayNonFloatingDiv(divId, title, width, height)
{   var obj = document.getElementById("leveler");
    document.getElementById(divId).style.width = width + 'px';
    document.getElementById(divId).style.height = height + 'px';
    //Make sure the opacity of each tab is completely set before moving to another tab
    Effect.toggle(divId,'appear');
}

function displayFloatingDiv(divId, title, width, height, left, top)
{   var obj = document.getElementById("leveler");
    var x=obtenerPosicionX(obj);
    var y=obtenerPosicionY(obj);
    //
    document.getElementById(divId).style.width = width + 'px';
    document.getElementById(divId).style.height = height + 'px';
    document.getElementById(divId).style.left = (x+left) + 'px'; //DDI
    document.getElementById(divId).style.top = (y+top)  + 'px';  //DDI
    Effect.toggle(divId,'appear');
    setCorrectTelIcon(divId);
}

/*Display the floating div without setting the left positioning*/
function displayFloatingDivNoLeftPositioning(divId, title, width, height, top)
{   var obj = document.getElementById("leveler");
    var x=obtenerPosicionX(obj);
    var y=obtenerPosicionY(obj);
    //
    document.getElementById(divId).style.width = width + 'px';
    document.getElementById(divId).style.height = height + 'px';
    document.getElementById(divId).style.top = (y+top)  + 'px';
    //
    Effect.toggle(divId,'appear');
    setCorrectTelIcon(divId);
}

function setCorrectTabIcon(tabIconId, panelDivId, activeTabImgRelPath, inactiveTabImgRelPath)
{   //Display the new active tab
    if( (document.getElementById(panelDivId).style.opacity=="") || (document.getElementById(panelDivId).style.opacity==null))
    {   if (document.getElementById(panelDivId).style.display == "none")
        {   document.getElementById(tabIconId).style.backgroundImage="url("+document.getElementById('dcDirectoryParent').value+activeTabImgRelPath+")";
        }
        else
        {   document.getElementById(tabIconId).style.backgroundImage="url("+document.getElementById('dcDirectoryParent').value+inactiveTabImgRelPath+")";
        }
    }
}

function sendRequest(url)
{   new Ajax.Request(url,
	{   method: 'get'
            , onSuccess:function(req){ alert(document.getElementById("dcMailConfirmMessage").value) //alert(requestSuccessMsg);
            }
            , onFailure:function(req){ alert(requestFailureMsg);}
    });
}



/*Function to handle telephone based contact request*/
var telContactRequiredFields ="\n-"+phoneLabel;
function sendTelContact()
{   missingFields=0;
    url = document.getElementById('dcDirectoryParent').value+'directcontact/DWDC_CONTACT_REQUEST2.php'

    elm = document.getElementById("txfPhone");
    phone = elm.value;
    if(phone=='') missingFields+=1;

    elm = document.getElementById("txfName");
    name= elm.value;
    //
    when="";
    for (var i=0; i < document.dctelForm.rdgWhen.length; i++)
    {  if (document.dctelForm.rdgWhen[i].checked)
       {   when = document.dctelForm.rdgWhen[i].value;
           break;
       }
    }
    //
    code = "aaaaaabbbccc"
    elm = document.getElementById("todayTime");
    todayTime=elm.value;
    elm = document.getElementById("tomorrowTime");
    tomorrowTime=elm.value;
    elm = document.getElementById("subject1");
    subject1=elm.value;
    elm = document.getElementById("subject2");
    subject2=elm.value;
    elm = document.getElementById("code");
    code=elm.value;
    elm = document.getElementById("dcMailFromAccount");
    dcMailFromAccount=elm.value;
    elm=document.getElementById("dcMailFromDomain");
    dcMailFromDomain=elm.value;
    elm=document.getElementById("dcMailConfirmMessage");
    dcMailConfirmMessage=elm.value;
    params= "dcMailFromAccount="+dcMailFromAccount+"&txfPhone="+phone+"&txfName="+name
    +"&rdgWhen="+when+"&code="+code+"&tomorrowTime="+tomorrowTime+"&todayTime="+todayTime+"&subject1="
    +subject1+"&subject2="+subject2+"&dcMailFromDomain="+dcMailFromDomain+"&dcMailConfirmMessage="+dcMailConfirmMessage;
    if(missingFields==0)
    {   //makeRequest(url+params, '');
        sendRequest(url+'?'+params);
        closeOpenedTabs();
    }
    else
    {   alert(requiredFieldsMsg+telContactRequiredFields+'.');
    }
}


/*Function to handle email based contact request*/
var   emailContactRequiredFields='\n-'+emailLabel;
function sendEmailContact()
{   missingFields=0;
    //todo url
    url = document.getElementById('dcDirectoryParent').value+'directcontact/DC_EMAIL_CONTACT_REQUEST.php'

    //
    elm = document.getElementById("txfName");
    name= elm.value
    elm = document.getElementById("txfEmail");
    email = elm.value;
    if(email=='')    missingFields+=1;
    elm = document.getElementById("txrComment");
    comment= elm.value

    elm = document.getElementById("subject1");
    subject1=elm.value;
    elm = document.getElementById("subject2");
    subject2=elm.value;
    elm = document.getElementById("code");
    code=elm.value;
    elm = document.getElementById("dcMailFromAccount");
    dcMailFromAccount=elm.value;
    elm=document.getElementById("dcMailFromDomain");
    dcMailFromDomain=elm.value;
    elm=document.getElementById("dcMailConfirmMessage");
    dcMailConfirmMessage=elm.value;
    //
    params= "dcMailFromAccount="+dcMailFromAccount+"&txfName="+name+"&txfEmail="+email+"&txrComment="+comment+"&subject1="
    +subject1+"&subject2="+subject2+"&dcMailFromDomain="+dcMailFromDomain+"&dcMailConfirmMessage="+dcMailConfirmMessage;
    if(missingFields==0)
    {   sendRequest(url+'?'+params);
        closeOpenedTabs();
    }
    else
    {   alert(requiredFieldsMsg+emailContactRequiredFields+'.');
    }
}


/*Function to handle direct contact request with email and phone inputs*/
var requiredFields ="\n-"+phoneLabel+'\n-'+emailLabel;
function sendemail()
{   missingFields=0;
    url = document.getElementById('dcDirectoryParent').value+'directcontact/DWDC_CONTACT_REQUEST2.php'

    elm = document.getElementById("txfPhone");
    phone = elm.value;
    if(phone=='') missingFields+=1;

    elm = document.getElementById("txfName");
    name= elm.value
    elm = document.getElementById("txfEmail");
    email = elm.value;
    if(email=='') missingFields+=1;

    when = get_radio_value();
    code = "aaaaaabbbccc"
    elm = document.getElementById("todayTime");
    todayTime=elm.value;
    elm = document.getElementById("tomorrowTime");
    tomorrowTime=elm.value;
    elm = document.getElementById("subject1");
    subject1=elm.value;
    elm = document.getElementById("subject2");
    subject2=elm.value;
    elm = document.getElementById("code");
    code=elm.value;
    elm = document.getElementById("dcMailFromAccount");
    dcMailFromAccount=elm.value;
    elm=document.getElementById("dcMailFromDomain");
    dcMailFromDomain=elm.value;
    elm=document.getElementById("dcMailConfirmMessage");
    dcMailConfirmMessage=elm.value;
    params= "dcMailFromAccount="+dcMailFromAccount+"&txfPhone="+phone+"&txfName="+name+"&txfEmail="+email
    +"&rdgWhen="+when+"&code="+code+"&tomorrowTime="+tomorrowTime+"&todayTime="+todayTime+"&subject1="
    +subject1+"&subject2="+subject2+"&dcMailFromDomain="+dcMailFromDomain+"&dcMailConfirmMessage="+dcMailConfirmMessage;
    if(missingFields<2)
    {   //makeRequest(url+params, '');
        sendRequest(url+'?'+params);
        closeOpenedTabs();
    }
    else
    {   alert(requiredFieldsMsg+requiredFields+'.');
    }
}



