var w;
var progressbar;

function helpPopup(id) {
    //alert("In helpPopup");
    var url = "";
    url = "/FarAppHelp?topic="+id;
    var mywin = window.open(url, "farapphelp",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function helpViewerPopup(id) {
    //alert("In helpPopup");
    var url = "";
    url = "/FarAppHelpViewer/"+id;
    var mywin = window.open(url, "FarAppHelpViewer",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function ugPopup() {
    var url = "/secure_UserGuide";
    var mywin = window.open(url, "FarAppUserGuide",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function rulePopup() {
    //alert("In helpPopup");
    var url = "";
    url = "/secure_MappingRuleDescriptions";
    var mywin = window.open(url, "mappingrulesdescriptions",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function uploadPopup() {
    //alert("In helpPopup");
    var url = "";
    url = "/secure_RawItemUpload";
    var mywin = window.open(url, "rawitemupload",'directories=0,status=0,scrollbars=0,menubar=0,toolbar=0,location=0,width=500,height=250,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function cguploadPopup() {
    var url = "";
    url = "/secure_CollegeGearProductUpload";
    var mywin = window.open(url, "ProductUpload",'directories=0,status=0,scrollbars=0,menubar=0,toolbar=0,location=0,width=500,height=250,resizable=yes');
    w = mywin;
    mywin.focus();
    return false;
}

function custfieldPopup() {
    //alert("In helpPopup");
    var url = "";
    url = "/secure_CustomFieldDescriptions";
    var mywin = window.open(url, "customfielddescriptions",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function imagecompPopup(productid, reload) {
    //alert("In helpPopup");
    var url = "";
    url = "/secure_NetSuiteImageComps/" + productid;
    if(reload){
        url = url + '/reload'
    }
    var mywin = window.open(url, "netsuiteimagecomparisons",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function awsKeyHelpPopup() {
    //alert("In helpPopup");
    var url = "";
    url = "/secure_AwsAccessKeyHelp";
    var mywin = window.open(url, "awsaccesskeyhelp",'scrollbars=yes,menubar=yes,toolbar=yes,location=yes,width=800,height=600,resizable=yes');
    w = mywin;
    //mywin.location.href = url;
    mywin.focus();
    return false;
}

function amzProdPage(url) {
    //alert("In helpPopup");
    //alert('Opening ' + url);
    var mywin = window.open(url);
    w = mywin;
    mywin.focus();
    return false;
}

function closeWindow(){
    //w.close();
    window.close();
}

function confirmAction(question){
    //var curloc = window.location;
    if (window.confirm(question)){
        //window.location(url);
        retval = true;
        //alert('ok');
    }
    else{
        //window.location = curloc;
        //alert('canceled');
        retval = false;
    }
    return retval;
}

function showError(result) {
    alert('checkProgress Error!');
    //for(var i = 0; i < arguments.length; i++) {
    //    alert('argument[' + i + '] = ' + arguments[i]);
    //}
    //alert(result);
    //window.setTimeout("checkProgress('0'," + callerid + ")", 10000);
}

function showProgress(result) {
    //result['progress'] is a string to display; if 'DONE', then this function flashes done and exits
    // the id for this process is passed in as kwarg in result
    //for(var i = 0; i < arguments.length; i++) {
    //    alert('argument[' + i + '] = ' + arguments[i]);
    //}
    var callerid = result.callerid;
    var progressStatus = result.status;
    var message = result.message;
    //alert(id);
    //alert(progressStatus);
    //alert('message: ' + message);
    //getElement("progress").innerHTML = result['progress'];
    //alert('callerid: ' + callerid + ' status: ' + progressStatus + ' message: ' + message);
    //getElement("progress").innerHTML = 'CallerID: ' + callerid + ' - ' + result['message'];
    getElement("progress").innerHTML = result.message;
    showElement("progress");
    //if (result['progress'] != 'DONE') {
    if (progressStatus != 'DONE') {
        //alert('setting timer for checkProgress');
        window.setTimeout("checkProgress('0'," + callerid + ")", 10000);
        //window.setTimeout("alert('timeouttest')");
    }
    //else {
    //    window.setTimeout('hideElement("progress")', 3000);
    //}
}

function getID() {
    t = new Date().getTime();
    t += Math.random();
    s = String(t);
    //alert('ID: ' + s);
    return s;
}

function checkProgress(callid, callerid) {
    //var ts = getID(); //new Date().getTime();
    //for(var i = 0; i < arguments.length; i++) {
        //alert('argument[' + i + '] = ' + arguments[i]);
    //}
    var ProgressURL = 'Progress';
    var ts = getID();
    //alert('loadJSONDoc called with '+ ProgressURL + "/" + callerid + '?callid=' + callid);
    var d = loadJSONDoc(ProgressURL + "/" + callerid + '?callid=' + callid + '&ts=' + ts);
    //alert('adding callback for showProgress');
    d.addCallback(showProgress);
    d.addErrback(showError);
}

function findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        for(op=obj; op !== null; op = op.offsetParent) {
            curleft += op.offsetLeft;
            curtop += op.offsetTop;
        }
        return [curleft, curtop];
    } else {
        return [0, 0];
    }
}

//function findRealPos(obj) {
//    var curleft = 0;
//    var curtop = 0;
//    if (obj.offsetParent) {
//        do {
//            curleft += obj.offsetLeft;
//            curtop += obj.offsetTop;
//        } while (obj = obj.offsetParent);
//        return [curleft, curtop];
//    } else {
//        return [0, 0];
//    }
//}

function show(obj,msg){
    //par = obj.offsetParent;
    //messageBox.style.top=obj.offsetTop;
    //messageBox.style.top=par.offsetTop;
    //messageBox.style.left=obj.offsetLeft+obj.offsetWidth+5;
    //messageBox.style.left=par.offsetLeft-(obj.offsetWidth + 100);
    var x, y;
    a = findPos(obj);
    //b = findRealPos(obj);
    //alert('pos x/y: ' + a[0] + '/' + a[1] + ' realpos x/y' + b[0] + '/' + b[1]);
    x = a[0];
    y = a[1];
    document.getElementById('messageBox').style.left = x -(obj.offsetWidth + 200);
    document.getElementById('messageBox').style.top = y-150; // + 50;
    document.getElementById('contents').innerHTML=msg;
    document.getElementById('messageBox').style.display="block";
    //document.getElementById('messageBox').style.padding="10";
}

function unshow(obj){
    document.getElementById('messageBox').style.display="none";
}

function showObject(o){
    var retval = '';
    for(var prop in o){
        retval = retval + prop + ' = ' + o[prop] + '\n';
    }
    return retval;
}


////////////////////////////////////////////////
/***********************************************
* Show Hint script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var horizontal_offset="9px"; //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset="0"; //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all;
var ns6=document.getElementById&&!document.all;

function getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}

function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function clearbrowseredge(obj, whichedge){
    var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1;
    if (whichedge=="rightedge"){
        var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40;
        dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
        if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
            edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset);
        }
        else{
            var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
            dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;
            if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){
                edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight;
            }
        }
    return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
    if ((ie||ns6) && document.getElementById("hintbox")){
        dropmenuobj=document.getElementById("hintbox");
        dropmenuobj.innerHTML=menucontents;
        dropmenuobj.style.left=dropmenuobj.style.top=-500;
        if (tipwidth!=""){
            dropmenuobj.widthobj=dropmenuobj.style;
            dropmenuobj.widthobj.width=tipwidth;
        }
        dropmenuobj.x=getposOffset(obj, "left");
        dropmenuobj.y=getposOffset(obj, "top");
        dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px";
        dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px";
        dropmenuobj.style.visibility="visible";
        obj.onmouseout=hidetip;
    }
}

function hidetip(e){
    dropmenuobj.style.visibility="hidden";
    dropmenuobj.style.left="-500px";
}

function createhintbox(){
    var divblock=document.createElement("div");
    divblock.setAttribute("id", "hintbox");
    document.body.appendChild(divblock);
}

if (window.addEventListener){
    window.addEventListener("load", createhintbox, false);
}
else if (window.attachEvent){
    window.attachEvent("onload", createhintbox);
}
else if (document.getElementById){
    window.onload=createhintbox;
}
