﻿function ShowProductImage(img, title, desc) {
    document.getElementById('imgProduct').src = 'images/products/' + img;
    document.getElementById('divProductTitle').innerHTML = title;
    document.getElementById('divProductDescription').innerHTML = desc;
}
function ChangeLanguage(a) {
    var LinkLanguage = a.innerHTML;
    var Language = (LinkLanguage == 'English') ? 'en' : 'ar';
    AllSiteWS.ChangeLanguage(Language, ChangeLanguage_Callback, FailedCallback);
}
function ChangeLanguage_Callback() {
    document.forms[0].submit();
}
function FailedCallback(error) {
    $find('Preloader_Popup').hide();
    var msg = error.get_message();
    alert(msg);
}


var mouseposx = 0;
var mouseposy = 0;
function GetMousePosition(evt) {
    evt = (!evt && window.event) ? window.event : evt;
    if (evt.pageX) { //mozilla
        mouseposx = evt.pageX;
        mouseposy = evt.pageY;
    }
    else if (evt.clientX) {
        mouseposx = evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        mouseposy = evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
}

function ShowHideVirtualPopWindowDialog(v) {
    var w = $get('divVirtualWindow');
    if (v == 'hidden') { $get("VirtualPopWindowDialog_divContent").innerHTML = ''; }
    w.style.visibility = v;
    centerDivToWindow(w); // center window
    w.style.top = (mouseposy-100) + 'px'; // override position
    
}
function ShowVirtualWindow(contentHTML, title) {
    $get('VirtualPopWindowDialog_Title').innerHTML = title;
    $get('VirtualPopWindowDialog_divContent').innerHTML = contentHTML;
    ShowHideVirtualPopWindowDialog('visible');
}


function browserIsIE() { if (typeof (window.innerWidth) == 'number') return false; else return true; };
function getWindowSize() { var myWidth = 0, myHeight = 0; if (!browserIsIE()) { myWidth = window.innerWidth; myHeight = window.innerHeight; } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; } return [myWidth, myHeight]; };
function getDIVSize(divObj) { var divWidth = 0, divHeight = 0; divWidth = divObj.clientWidth; divHeight = divObj.clientHeight; return [divWidth, divHeight]; } ;
function centerDivToWindow(divObj) { var WindowSize = getWindowSize(); var DivSize = getDIVSize(divObj); var LeftPos = (WindowSize[0] / 2) - (DivSize[0] / 2); var TopPos = (WindowSize[1] / 2) - (DivSize[1] / 2); divObj.style.top = TopPos + 'px'; divObj.style.left = LeftPos + 'px'; };


function isValidEmail(emailaddress) {
    var regExEmail = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;
    return regExEmail.test(emailaddress);
}


function SendMessage() {
    var nm = $get('ctl00_ContentPlaceHolder1_txtName').value.trim();
    var eml = $get('ctl00_ContentPlaceHolder1_txtEmail').value.trim();
    var sbj = $get('ctl00_ContentPlaceHolder1_txtSubject').value.trim();
    var msg = $get('ctl00_ContentPlaceHolder1_txtMessage').value.trim();
    if ((nm.length == 0) || (eml.length == 0) || (sbj.length == 0) || (msg.length == 0)) {
        alert('All fields are required!');return false;
    }
    if (!isValidEmail(eml)) {
        alert('Please enter a valid email!'); return false;
    }
    $get('divSubmitControls').innerHTML = "<img alt='processing...' src='siteimages/preloadersml.gif' border='0' />"
    AllSiteWS.SubmitInquiry(nm, eml, sbj, msg, SubmitInquiry_Callback, FailedCallback);
}
function SubmitInquiry_Callback() {
    $get('ctl00_ContentPlaceHolder1_txtName').value='';
    $get('ctl00_ContentPlaceHolder1_txtEmail').value='';
    $get('ctl00_ContentPlaceHolder1_txtSubject').value='';
    $get('ctl00_ContentPlaceHolder1_txtMessage').value = '';
    $get('divSubmitControls').innerHTML = "<input type='button' id='cmdSend' value='Send' onclick='SendMessage();' />&nbsp; &nbsp;&nbsp;<input type='reset' value='Reset' />";
    alert('Thank you for you inquiry. Our representative will be contacting you very soon.');
}

//admin page
function GetWebPagesListItems(pn) {
    $find('Preloader_Popup').show();
    AllSiteWS.CreateWebPagesListItemsDropdownList(pn, CreateWebPagesListItemsDropdownList_Callback, FailedCallback)
}
function CreateWebPagesListItemsDropdownList_Callback(value) {
    $find('Preloader_Popup').hide();
    $get('divListItems').innerHTML = value;
    $get('WebPagesListItems').selectedIndex = -1;
}
function LoadItemToEditor(pge, itm) {
    $find('Preloader_Popup').show();
    AllSiteWS.GetPageItem(pge, itm, LoadItemToEditor_Callback, FailedCallback)
}
function LoadItemToEditor_Callback(pi) {
    $find('Preloader_Popup').hide();
    editorContentAR.setData(pi.ar)
    editorContentEN.setData(pi.en)
}
function WebPageEditor_SaveChanges() {
    var lbPages = $get('ctl00_ContentPlaceHolder1_WebPagesList');
    var lbItem = $get('WebPagesListItems');
    if ((!lbPages) || (!lbItem)) {return false;}

    var confirmed = confirm('تأكيد تريد حفظ التغييرات؟\n Confirm you want to save changes?')
    if (!confirmed) return false;

    var pge = lbPages.options[lbPages.selectedIndex].value;
    var itm = lbItem.options[lbItem.selectedIndex].value;
    var ar = editorContentAR.getData();
    var en = editorContentEN.getData();

    $find('Preloader_Popup').show();
    AllSiteWS.SavePageItemChangesToDB(pge, itm, ar, en, SavePageItemChanges_Callback, FailedCallback);
}
function SavePageItemChanges_Callback() {
    $find('Preloader_Popup').hide();
    alert('Changes was made successfully!');
}



//Seadragon-Silverlight
function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}