﻿// JScript File
//
//
if(location.href.indexOf('en-us')>=0 && location.href.indexOf('b/')<0 && location.href.indexOf('B/')<0 ) 
    location.href=location.href.replace('b2/','b/').replace('B2/','b/').replace('b3/','b/').replace('B3/','b/')
else if(location.href.indexOf('sp-mx')>=0 && location.href.indexOf('b2/')<0 && location.href.indexOf('B2/')<0 ) 
    location.href=location.href.replace('b/','b2/').replace('B/','b2/').replace('b3/','b2/').replace('B3/','b2/')  
else if(location.href.indexOf('pt-br')>=0 && location.href.indexOf('b3/')<0 && location.href.indexOf('B3/')<0 ) 
    location.href=location.href.replace('b/','b3/').replace('B/','b3/').replace('b2/','b3/').replace('B2/','b3/') 

//QSAction left side navigation
//Changes the action and preserves all other QS parameters 
//NOTE: action must be 1 char long for this to work
function QSAction(action)
{
  if(location.href.indexOf('action')<0) 
  {
    if(location.href.indexOf('?')<0) {location.href=location.href + "?action=" + action;} 
    else {location.href=location.href + "&action=" + action;} 
  }
  else
  {
    var strAction = location.href.substr(location.href.indexOf('action'), 8);
    var newHref = location.href.replace(strAction, 'action=' + action);
    if(location.href.indexOf('guid=')>location.href.indexOf('action='))
        newHref = newHref.substring(0,location.href.indexOf('guid=') + 41);
    location.href = newHref;    
  }
}

//QSActionClear left side navigation
//Clears all QS parameters after action
function QSActionClear(action)
{
  if(location.href.indexOf('action')<0) 
  {
    if(location.href.indexOf('?')<0) {location.href=location.href + "?action=" + action;} 
    else {location.href=location.href + "&action=" + action;} 
  }
  else{location.href=location.href.substr(0,location.href.indexOf('action')) + 'action=' + action;}
}

function DoLookup(elementId)
{
  var productCodeElementId = "";
  productCodeElementId = elementId.replace(elementId.substr(elementId.lastIndexOf('_'), elementId.length-1), "_txtProductCode");
  //alert(document.all(productCodeElementId).value);
  //document.forms[0].submit();
  var args = new Array();
  args[0] = "action";
  args[1] = productCodeElementId; 
  document.all(args[1]).value = window.showModalDialog("Lookup.aspx", args, "status=no,help=no");
}

function dump_props(object, object_name)
{
    var res = "";
    for(var i in object)
        res += object_name + "." + i + " = " + object[i] + "<br/>";
	var win = window.open();
	win.document.write(res);
}

//Cookies
function GetCookie(name)
{
    var cookies = document.cookie;
    var startname = cookies.indexOf(name + "=");
    var startvalue = cookies.indexOf("=", startname) + 1;
    var endvalue = cookies.indexOf(";", startvalue);
    if(endvalue == -1)
        endvalue = cookies.length;
    var cvalue = cookies.substring(startvalue, endvalue);
    return cvalue;     
}

function SetCookie (name, value, expires, path, domain, secure)
{
    document.cookie = name + "=" + escape(value) + 
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "") 
    
}

/* SET COOKIE EXAMPLE
var expires = new Date();
expires.setTime(expires.getTime() + 60000); //1 min// (3E11 == 10 years == forever)
SetCookie("Cookie", "Test", expires);
alert(document.cookie.split(';').join('\n'));
alert(GetCookie("Cookie"));
*/

//STRING MANIPULATION
function Replace(string, oldValue, newValue)
{
    if(string.indexOf(oldValue)>=0)
    {
        string = string.replace(oldValue,newValue);
        Replace(string, oldValue, newValue);
    } 
    return string;   
}

//TABLE ROW PROCESSING FUNCTIONS
function parseRow(row,tag)
{
    var result = "";
    var tempStr = "";
    var cells = row.getElementsByTagName(tag);
    for(i=0; i<cells.length; i++)
    {
       if(cells[i].innerHTML.toUpperCase().indexOf("TEXTAREA")>=0)
            result += cells[i].firstChild.innerText + "|";
       else if(cells[i].innerHTML.toUpperCase().indexOf("INPUT")>=0)
            result += cells[i].firstChild.attributes['value'].value + "|"; 
       else
       {
            result += cells[i].innerText + "|";
       }
    }
    return result;
}

function buildUrl(strKeys, strValues)
{
    var url = "";
    var keys = new Array();
    var values = new Array();
    keys = strKeys.split("|");
    values = strValues.split("|");
    for(i=0; i<keys.length; i++)
    {
        keys[i] = keys[i].replace(" ", "").replace(" ", "").replace(" ", ""); 
        try
        {
            if(keys[i].length > 0)
            {
                if(keys[i].indexOf("Date") >=0)
                    values[i] = ToSAPDate(values[i]);
                if(values[i] == " ")
                    values[i] = "";
                url += "&" + keys[i] + "=" + escape(Replace(values[i], "'", "`")); 
            }

        }
        catch(e){}
    }
    //alert(handleSAPDate + url);
    return url;
    
}

function ToSAPDate(strDate)
{
    dateTokens = strDate.split("/")
    var Month = dateTokens[0];
    var Day = dateTokens[1];
    var Year = dateTokens[2];
    if(Month.length == 1)
        Month = "0" + Month; 
    if(Day.length == 1)
        Day = "0" + Day; 
    return Year + Month + Day;  
}

function GetSAPDate(objDate)
{
    var Year = objDate.getFullYear() + ""; 
    var Month = (objDate.getMonth() + 1) + ""; //0 based
    var Day = objDate.getDate() + "";
    if(Month.length == 1)
        Month = "0" + Month; 
    if(Day.length == 1)
        Day = "0" + Day; 
    return Year + Month + Day;  
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
