﻿// JScript File
function GoToURL(thisURL)
{
    alert(thisURL);
    top.contentPane.document.location.href = thisURL;
}


function getElementSrc(thisElement)
{
    var thisElement;
    if(navigator.appVersion.indexOf("MSIE")!=-1)
    {
    
       //IE Browser
       try
       {
            thisElement = window.frames[1].document.getElementById(thisElement);  
       }
       catch(e)
       {
            thisElement = window.frames[0].document.getElementById(thisElement);  
       }
    }
    else
    {
       //Not IE
       //thisElement = window.frames[0].document.getElementById(thisElement);
       try
       {
            thisElement = window.frames[0].document.getElementById(thisElement);  
       }
       catch(e)
       {
            //alert(e);
            thisElement = window.frames[1].document.getElementById(thisElement);  
       }
    }
        
    return thisElement;
}

function ribbonClick(thisPage, thisBtn)
{
    
    //find page frame
    var frmID, frmSrc;
    frmID = "ctl00_cph1_frmFullAddEdit";
    frmSrc = top.document.getElementById(frmID);
    
    var btnID, btnSrc;
    
    var screenH = (document.body) ? document.body.clientHeight : innerHeight;
    showHideProgress(screenH);
    
    
    //find selected button, then click it
    if(thisBtn=="New")
    {
        if(thisPage=="Link")
        {
            var sitez = this.document.getElementById("siteID");
            alert(sizez.value);
            window.frames[0].document.location.href = "View" + thisPage + "AddEdit.aspx?siteID=" + sitez.value;
        }
        else
        {
            window.frames[0].document.location.href = "View" + thisPage + "AddEdit.aspx";
        }
    }
    else if(thisBtn=="Cancel")
    {
        if(thisPage=="MetaData")
        {
            //get siteID
            var site = document.getElementById("siteID");
            document.location.href = thisPage + "List.aspx?siteID=" + site.value;
        }
        else
        {
            document.location.href = thisPage + "List.aspx";
        }
    }
    else if(thisBtn=="SaveClose")
    {
        btnID = "btnSaveClose";
        btnSrc = getElementSrc(btnID);
        btnSrc.click();
    }
    else if(thisBtn=="SaveMore")
    {
        btnID = "btnSaveMore";
        btnSrc = getElementSrc(btnID);
        btnSrc.click();
    }
    else if(thisBtn=="SaveNew")
    {
        btnID = "btnSaveNew";
        btnSrc = getElementSrc(btnID);
        btnSrc.click();
        
    }
    else if(thisBtn=="Delete")
    {
        //find id based on page
        var itemID, itemSrc;
        itemID = "tb" + thisPage + "ID";
        itemSrc = getElementSrc(itemID);
        if(itemSrc.value=="0")
        {
            alert("This appears to be a new " + thisPage + ". Delete not available");
        }
        else
        {
            if(confirm("Are you sure you want to delete this " + thisPage + "?"))
            {        
                btnID = "btnDelete";
                btnSrc = getElementSrc(btnID);
                btnSrc.click();
            }
            else
            {
                showHideProgress(0);
            }
        }
    }
    else if(thisBtn=="Email")
    {
        //if this is conatct, try to find focus of email box
        var currEmail = getElementSrc("tbEmailFocus");
        btnID = "tbEmail1";
        btnSrc = getElementSrc(currEmail.value);
        
        if(btnSrc.value=="")
        {
            if(currEmail.value=="tbEmail1")
            {
                alert("Email 1 is empty, unable to create new message.");
            }
            else
            {
                alert("Email 2 is empty, unable to create new message.");
            }
        }
        else
        {
            top.document.location.href = "mailto:" + btnSrc.value;
            
        }
        
        showHideProgress(0);        
    }
    else if(thisBtn=="EmailOutlook")
    {
        var currEmail = getElementSrc("textbox_sEmail");
        if(currEmail.value!="")
        {
            try
            {
              //IE Browser
              window.frames[1].document.location.href = "mailto:" + currEmail.value;         	    
            }
            catch(e)
            {
              //Not IE
              top.document.location.href = "mailto:" + currEmail.value;
            }
        }
        else
        {
            alert("There is no email address specified!");
        }
        showHideProgress(0);
       
    }
    
    else if(thisBtn=="Task")
    {
        //get contact ID                
        //alert("Assign New Task Not Implemented Yet!");       
        showHideProgress(0);
        
        //pop task window
        var dto = window.showModalDialog("TaskAddEdit.aspx?TaskID=0&isContact=1", "contactTask", "dialogWidth:700px; dialogHeight:550px; center: Yes; help: No; resizable: No; status: No; scroll: No; edge: Raised;");
        if (dto != null)
		{
		    window.close();
		}
    }
    
    
}


function changeOrder(thisCol, thisPage)
{
    var srchID, srchSrc;
    var url;
    
    var oSearching;
    oSearching = top.document.getElementById("objSearching");

    try
    {
        //get search box        
        srchID = "ctl00_cph1_rgSearch_orl" + thisPage + "_tbx_Search" + thisPage + "s";
        srchSrc = document.getElementById(srchID);
    }
    catch(e)
    {
        srchSrc.value = "";
    }
    
    //determine Asc or Desc
    var sortSrc, thisSort;
    sortSrc = document.getElementById("currSort");
    
    if(sortSrc.value=="")
    {
        thisSort = " Desc"
        sortSrc.value = "Desc";
    }
    else
    {
        thisSort = ""
        sortSrc.value = "";
    }    
    
    url = "View" + thisPage + "List.aspx?orderBy=" + thisCol + thisSort;
    
    // Build a path to the view we want...
	url += "&param1=" + srchSrc.value;
	
	//Get both Divs
	var lsView, lsSrc;
      
    lsView = "frmFullList";
    lsSrc = document.getElementById(lsView);
    
    element = lsSrc;
    
       
    // Create asynchronous request...
	if (window.XMLHttpRequest) { 
		req = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { 
		req = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	
	req.onreadystatechange = function() 
	{   	    
	    if (req.readyState == 4 && req.status == 200) 
		{ 
		    element.innerHTML = req.responseText;
		    oSearching.className = "hidden";
		} 
		else
		{   
		    element.innerHTML = oSearching.innerHTML;
		}
	} 
    
	
	req.open("GET", FormatNonCacheableOrderUrl(url), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	req.send(null);

}

function FormatNonCacheableOrderUrl(url)
{
	// Workaround IE GET caching issue...
	
	if (url.indexOf("?") == -1)
		url += "?hash=" + Math.random();		
	else
		url += "&hash=" + Math.random();
    
    return url;
}

function showHideProgress(thisHeight)
{
    
    //find page frame
    var frmID, frmSrc;
    frmID = "ctl00_cph1_frmFullAddEdit";
    frmSrc = top.document.getElementById(frmID);
    
    var srchID, srchSrc;
    srchID = "objSearching2";
    srchSrc = getElementSrc(srchID);
    
    try
    {
        if(srchSrc.className=="hidden")
        {
            srchSrc.className = "objSearching2";
            srchSrc.style.height = thisHeight - 190 + "px";
        }
        else
        {
            srchSrc.className = "hidden";
        }
    }
    catch(e)
    {
    
    }
}

function setFocus(thisField, thisFocus)
{   
    var srchSrc;
    srchSrc = document.getElementById(thisField);
    srchSrc.value = thisFocus;
}

function changeTab(currTab, tabCount)
{

    var frmID, frmSrc;
    frmID = "ctl00_cph1_frmFullAddEdit";
    frmSrc = top.document.getElementById(frmID);
    
    for(var i=1;i<=tabCount;i++)
    {
        var currRow = getElementSrc("tab" + i);
                
        if(i==currTab)
        {
            currRow.style.display = "block";
        }
        else
        {
            currRow.style.display = "none";
        }
    }
}


function showDialog(url, width, height, userdata, nocaching)
{
	var features = "dialogHeight:" + height + "px; dialogWidth: " + width + "px; edge: Raised; " +
		"center: Yes; help: No; resizable: No; status: No; scroll: No;";
		
	if (nocaching)
	{
		if (url.lastIndexOf("?") != -1)
			url += "&ts=" + new Date(); // HACK: work around to eliminate caching...
		else
			url += "?ts=" + new Date();
	}

	//return window.showModalDialog(url, userdata, features);	
	return window.open(url, "xs", "address=no,location=no,status=no,width=700,height=600");				
}

function savePackage(thisP)
{
    var tSrc;
    tSrc = document.getElementById("ctl00_cph1_tbList");
    
    var pSrc;
    pSrc = document.getElementById("p" + thisP);
    
    var thisItem;
    thisItem = thisP + ":1";
    
    var currList;
    currList = tSrc.value;
    
    //If checked
    if(pSrc.checked)
    {
        tSrc.value = tSrc.value + thisItem + ",";
    }
    else
    {
        tSrc.value = removeItem(currList, thisItem);
    }
    
    calTotal(tSrc.value);
}

function removeItem(originalArray, itemToRemove) 
{	
    var oArray = originalArray.split(",");
    var finalArray = "";
    
    var j = 0;	
    while (j < oArray.length) 
    {	
        if (oArray[j] != itemToRemove) 
        {
            if(oArray[j].length != 0)
            {
                finalArray = finalArray + oArray[j] + ",";
            }
        } 
        
        j++;
     }	
     
     if(finalArray != "")
     {
        //finalArray = finalArray + ",";
     }
     return finalArray;
}

function saveExtra(thisE)
{
    
    var tSrc;
    tSrc = document.getElementById("ctl00_cph1_tbList");
    
    //get value for thisE
    var eSrc;
    eSrc = document.getElementById(thisE);
    
    //get total box for this item
    var totSrc;
    totSrc = document.getElementById("t" + thisE.replace("q",""));
    
    //get price for this item
    var pSrc;
    pSrc = document.getElementById("price" + thisE.replace("q",""));
        
    if(eSrc.value == 0)
    {
        //remove item
        tSrc.value = checkItem(tSrc.value, thisE.replace("q",""));
        totSrc.value = "0";
    }
    else
    {
        //add to string
        var thisItem;
        thisItem = thisE.replace("q","") + ":" + eSrc.value;
        
        
        if(tSrc.value.indexOf(thisE.replace("q","") + ":") >= 0)
        {
            tSrc.value = checkItem2(tSrc.value, thisE.replace("q",""), eSrc.value);
        }
        else
        {
            tSrc.value = tSrc.value + thisItem + ",";
        }
        
        totSrc.value = eval(pSrc.value * eSrc.value);
    }
    
    calTotal(tSrc.value);
    
}

function checkItem(originalArray, itemToCheck) 
{	
    var oArray = originalArray.split(",");
    var oArray2;
    var finalArray = "";
    
    var j = 0;	
    
    while (j < oArray.length) 
    {	
        oArray2 = oArray[j].split(":");
        if (oArray2[0] != itemToCheck) 
        {
            if(oArray[j].length != 0)
            {    
                finalArray = finalArray + oArray[j] + ",";                
            }
        } 
        
        j++;
     }	
     
     return finalArray;
}

function checkItem2(originalArray, itemToCheck, newVal) 
{	
    var oArray = originalArray.split(",");
    var oArray2;
    var finalArray = "";
    
    var j = 0;	
    
    while (j < oArray.length) 
    {	
        oArray2 = oArray[j].split(":");
        //alert("arr0: " + oArray2[0] + " ... " + itemToCheck);
        if (oArray2[0] != itemToCheck) 
        {
            //alert("checking... " + oArray[j]);
            if(oArray[j].length != 0)
            {
                finalArray = finalArray + oArray[j] + ",";
            }
        } 
        else
        {
            finalArray = finalArray + oArray2[0] + ":" + newVal + ",";
        }
        
        j++;        
     }	
     
    return finalArray;
}



function saveQuote()
{
    //alert("Saving Quote");
    
    var tSrc;
    tSrc = document.getElementById("ctl00_cph1_tbList");
    
    url = "SaveQuote.aspx?qList=" + tSrc.value;
    
    // Create asynchronous request...
	//if (window.XMLHttpRequest) { 
	//	req = new XMLHttpRequest(); 
	//} else if (window.ActiveXObject) { 
	//	req = new ActiveXObject("Microsoft.XMLHTTP"); 
	//} 
	
	if (window.XMLHttpRequest) { 
		req = new XMLHttpRequest(); 		
	} else if (window.ActiveXObject) { 
		req = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	else
	{
	    req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	
	req.onreadystatechange = function() 
	{   	 
	    //alert(req.readyState);   
	    if (req.readyState == 4 && req.status == 200) 
		{ 
		    //element.innerHTML = req.responseText;
		    //oSearching.className = "hidden";
		} 
		else
		{   
		    //element.innerHTML = oSearching.innerHTML;
		}
	} 
    
	
	req.open("GET", FormatNonCacheableOrderUrl(url), false);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	req.send(null);
    
}

function calTotal(pList)
{
    var arrP = pList.split(",");
    var pPrice;
    var tmpPrice = 0;
    var totalPrice = 0;
    
    var arrX;
    
    var j = 0;	
    
    while (j < arrP.length) 
    {	
        arrX = arrP[j].split(":");
        //find product price
        if(arrX[0].length > 0)
        {
            pPrice = document.getElementById("price" + arrX[0]);
            tmpPrice = pPrice.value;
            tmpPrice = tmpPrice.replace(",","");
            totalPrice = eval(totalPrice + eval(tmpPrice * arrX[1]));
        }        
        j++;
    }
    
    var uTotal;
    uTotal = document.getElementById("userTotal")
    
    if(document.all){
     document.getElementById('userTotal').innerText = formatCurrency(totalPrice);
    } else{
        document.getElementById('userTotal').textContent = formatCurrency(totalPrice);
    }

}

function formatCurrency(num) 
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}




