var sText = null; //global variable that has the text that will be pronunced

/*This function get selected text in window and return text*/
function GetSelectedText() {
    var txt = "";

    if (window.getSelection)// recent Mozilla versions 
    {

        txt = window.getSelection();
    }
    else if (document.getSelection) // older Mozilla versions
    {

        txt = document.getSelection();
    }
    else if (document.selection) {
        txt = document.selection.createRange().text;
    }
    else if (document.all) {
        txt = document.selection.createRange().text;
    }
    else {
        txt = document.getSelection(); // FUNKAR FoR ALLA UTOM EXPLORER
    }

    sText = txt;
 
    return txt;

}

/*This function get the inner text for the element and return text*/
function GetElementValue(elementID) {

    var value;
    if (document.all) {
        value = document.getElementById(elementID).innerText;

    }
    else {

        value = document.getElementById(elementID).textContent;


    }

	value = value.replace(/ل " ا/g, 'ل');
	value = 'قارئ الأخبار من جريدة الرياض.' + value;
    sText = value;
	
    return value;
}

//function to check if there is selected text in window
function HaveSelectedText() {
    var selectedText = GetSelectedText();
    if (selectedText != '') {
        return true;
    }
    else {
        return false;
    }
}

//function to create iframe
function createIframe(displayID, iframeName, width, height) {
    var iframe;
    //get the iframe in document
    var divelement = document.getElementById(displayID);
    var oldiframe = document.getElementById('iframe0');
    //remove iframe if it is exsist
    if (oldiframe)
        divelement.removeChild(oldiframe);

//create iframe
    if (document.createElement && (iframe =
document.createElement('iframe'))) {
        iframe.name = iframe.id = iframeName;
        iframe.width = width;
        iframe.height = height;
        iframe.src = 'about:blank';
        iframe.setAttribute("scrolling", "no");
        iframe.setAttribute("frameborder", "0");
        
        divelement.appendChild(iframe);

    }
    return iframe;
}


/*This function to play the player in the same page using iframe to determine
where the player will appear.It take 4 parameters:
1- displayID:the id of dive that the media player will displayin it and the minmum hieght and width is 250  
2- elementID:the element that contain the text
3- Voicename:the name of voice user want
4- cusid:the customer id that take from speechworkers.com
*/
function ListenInSamePage(displayID, elementID, Voicename, cusid) {

//get selected text
    if (HaveSelectedText() != true) {
        GetElementValue(elementID);
    }
    try {
        sText = sText.replace(/"/g, ' ');		
    }
    catch (err) { }
   
    //create iframe
    var iframe = createIframe(displayID, 'iframe0', 250, 250);
    if (iframe) {
        var iframeDoc;
        if (iframe.contentDocument) {
            iframeDoc = iframe.contentDocument;
        }
        else if (iframe.contentWindow) {
            iframeDoc = iframe.contentWindow.document;
        }
        else if (window.frames[iframe.name]) {
            iframeDoc = window.frames[iframe.name].document;
        }
        //write in document of iframe
        if (iframeDoc) {
            iframeDoc.open();

            iframeDoc.write(
  '<form id="frm" name="frm" action="http://www.speechworkers.com.eg/SWServices/MediaPlayer.aspx" method="POST" >' +
       '<input  name="sVoiceName" type="hidden" value=' + Voicename + ' />' +
       '<input name="sourceText" type="hidden" value="" />' +
       '<input name="comId" type="hidden" value=' + cusid + ' />' +
       '</form>');
            iframeDoc.close();
            
            //get form in iframe and make submit
            var frm = iframeDoc.getElementById('frm');
            frm.sourceText.value = sText;
            frm.submit();
        }
    }
}

/*function using to listen to text in popup window
it take 3 parameters:
2- elementID:the element that contain the text
3- Voicename:the name of voice user want
4- cusid:the customer id that take from speechworkers.com
*/
function ListenInPopPage(elementID, Voicename, cusid) {

    //get the text and set to stext
    if (HaveSelectedText() != true) {
        GetElementValue(elementID);
    }
    try
    {
    sText = sText.replace(/"/g, ' ');
}
    catch(err){}
    
    
    //create the form
    var oldform = document.getElementById('formsubmit');
    if (oldform)
        document.body.removeChild(oldform);
    if (document.createElement && (form =
document.createElement('form'))) {
        form.setAttribute("id", "formsubmit");
        form.setAttribute("action", "");
        form.setAttribute("method", "");
        form.setAttribute("target", "");
        document.body.appendChild(form);
}


//create the hidden inputs in form
//the sVoiceName input
        var input = document.createElement("input"); // create an input element
        with (input) {
            setAttribute("name", "sVoiceName"); // give input a name
            setAttribute("type", "hidden"); // make it a hidden input
            setAttribute("value", Voicename); // give input a value
        }
        form.appendChild(input); // append input to form

        form.appendChild(document.createElement("br"));

        //the sourceText input
        var input = document.createElement("input"); // create an input element
        with (input) {
            setAttribute("name", "sourceText"); // give input a name
            setAttribute("type", "hidden"); // make it a hidden input
            setAttribute("value", sText); // give input a value
        }
        form.appendChild(input); // append input to form

        form.appendChild(document.createElement("br"));

        //the comId input
        var input = document.createElement("input"); // create an input element
        with (input) {
            setAttribute("name", "comId"); // give input a name
            setAttribute("type", "hidden"); // make it a hidden input
            setAttribute("value", cusid); // give input a value
        }
        form.appendChild(input); // append input to form

        form.appendChild(document.createElement("br"));

     
    //set the action,method and target
    form.action = "http://www.speechworkers.com.eg/SWServices/MediaPlayer.aspx";
    form.method = "post";
    form.target = "show";
  
    //open window
    var opened = window.open(form.action, form.target, "toolbars=no,scrollbar=no,width=400,height=250");
    form.submit();


}







