﻿var AjaxArray = new Array()

function RequestItemStore(o, k)
{
    if (!k) k = '';
    this.Object = o;
    this.Key = k;
}

function createRequestObject() 
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	else
		ro = new XMLHttpRequest();
	return ro;
}



function handleResponse() 
{
    var cnt = arguments[0];
    if ((AjaxArray[cnt]) && (AjaxArray[cnt].Object) && (AjaxArray[cnt].Key != 'dead'))
    {
        var http = AjaxArray[cnt].Object;
        if(http.readyState == 4){
	        var response = http.responseText;
	        //alert(response);
		    AjaxArray[cnt].Key = 'dead';
	        CleanAjaxArray();
    	    if (!HandleAjaxResponse(response))
		    {
		        if (response != '')
		            document.write(response);
		    }
	    }
	}
}

function RemoveExistingAjaxRequestWithSameKey(key)
{
    if (key != '')
    {
        for (i in AjaxArray)
        {
            if ((AjaxArray[i]) && (AjaxArray[i].Key == key))
                AjaxArray[i].Key = 'dead';
        }
		CleanAjaxArray();
    }
}

function CleanAjaxArray()
{
    for (i in AjaxArray)
    {
        if ((AjaxArray[i] != null) && (AjaxArray[i].Key == 'dead'))
            AjaxArray[i] = null;
    }
}

function sndReq(action, key) 
{	
    if (!key) key = '';
    
    //alert(action);
    
    RemoveExistingAjaxRequestWithSameKey(key);
    
    var cnt = AjaxArray.length;
	var http = createRequestObject();
	AjaxArray[cnt] = new RequestItemStore(http, key);
	
	var sp = action.split('?');
	var url = sp[0];
	var parameters = '';
	for (i=1; i<sp.length; i++)
	    parameters += sp[i];
	   
	http.onreadystatechange = function() { handleResponse(cnt); };
	http.open('POST', url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", parameters.length);
    http.setRequestHeader("Connection", "close");
    http.send(parameters);
}

function AddWrapperDiv(el, wrapperID)
{
    var wrapper = document.createElement('div'); 
    wrapper.appendChild(el.cloneNode(true)); 
    el.parentNode.replaceChild(wrapper, el);
    wrapper.setAttribute('id', wrapperID); 
    return wrapper;
}

function getElementPosition(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail){
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft,top:offsetTop};
}

function GetXMLObject(xml)
{
    var doc;
    if (window.ActiveXObject)
    { 
        doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(xml);
    }
    else
    {
        var parser=new DOMParser();
        doc=parser.parseFromString(xml,"text/xml");
    }
    return doc;
}

function SetImagePath(cntrl, src)
{
    document.getElementById(cntrl).src = src;
}


function ImageLoadingError(i, Size, ID)
{
    i.src = jsImagePath + 'Browser/noimage_' + Size + '.gif';
    if (document.getElementById('divEnlargeImage' + ID)) document.getElementById('divEnlargeImage' + ID).style.display = 'none';
}

function GetImageDisplay(Image, URL, ProductName, Size, Alt, ID)
{
    if (!ID) ID = '';
    if (URL != '')
    {
        if (Image != '')
            Image = '<a href="' + URL + '"><img src="/getfile/' + Image + '/ProductImage.aspx?maxsidesize=' + Size + '"  class="ProductImage" alt="' + Alt + '" onerror="ImageLoadingError(this, ' + Size + ', \'' + ID + '\');" ></a>'
        else
            Image = '<a href="' + URL + '"><img src="' + jsImagePath + 'Browser/noimage_' + Size + '.gif" border="0" alt="' + Alt + '" onerror="ImageLoadingError(this, ' + Size + ', \'' + ID + '\');" ></a>'
    }
    else
    {
        if (Image != '')
            Image = '<img src="/getfile/' + Image + '/ProductImage.aspx?maxsidesize=' + Size + '"  class="ProductImage" alt="' + Alt + '" onerror="ImageLoadingError(this, ' + Size + ', \'' + ID + '\');"  >'
        else
            Image = '<img src="' + jsImagePath + 'Browser/noimage_' + Size + '.gif" border="0" alt="' + Alt + '" onerror="ImageLoadingError(this, ' + Size + ', \'' + ID + '\');"  >'
    }
    return Image;
}

function ReplaceTag(str, origtext, newtext)
{
    if (origtext != newtext)
        str = doReplace(str, origtext, newtext);
    return str;
}

function doReplace(str, origtext, newtext)
{
    if (!newtext) newtext = '';
    if (str)
    {
        var i = 0
        while (i < str.length)
        {
            if (str.substr(i, origtext.length) == origtext)
            {
                str = str.substr(0, i) + newtext + str.substr(i+origtext.length);
                i += (newtext.length - origtext.length);
            }
            i++
        }
    }
    return str;
}


function removeBadChars(str)
{
    var ret = ''
    for (i=0; i<str.length; i++)
    {
        if (GoodCharacters.indexOf(str.substr(i, 1)) != -1)
            ret += str.substr(i, 1)
    }
    return ret;
}

function trim (str) {
    return str.replace(/^s+/, '').replace(/s+$/, '');
}

function PDF(pth)
{
    window.open(pth, '_blank', 'height=500, width=470, fullscreen=0, status=0, toolbar=0, menubar=0, titlebar=0, scrollbars=1' );
}

function loadflash(id, file, width, height, flashvars)
{
    document.write(getloadflashtext(id, file, width, height, flashvars));
}

function getloadflashtext(id, file, width, height, flashvars)
{
        var s = '<OBJECT id="'+ id +'" codeBase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" '
		+ '	height="' + height + '" width="' + width + '" align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '
		+ '		VIEWASTEXT> '
		+ '		<PARAM NAME="_cx" VALUE="25268"> '
		+ '		<PARAM NAME="_cy" VALUE="15081"> '
		+ '		<PARAM NAME="FlashVars" VALUE="' + flashvars + '"> '
		+ '		<PARAM NAME="Movie" VALUE="' + file + '"> '
		+ '		<PARAM NAME="Src" VALUE="' + file + '"> '
		+ '		<PARAM NAME="Play" VALUE="-1"> '
		+ '		<PARAM NAME="Loop" VALUE="-1"> '
		+ '		<PARAM NAME="Quality" VALUE="High"> '
		+ '		<PARAM NAME="SAlign" VALUE=""> '
		+ '		<PARAM NAME="Menu" VALUE="-1"> '
		+ '		<PARAM NAME="Base" VALUE=""> '
		+ '		<PARAM NAME="AllowScriptAccess" VALUE="Always"> '
		+ '		<PARAM NAME="Scale" VALUE="ShowAll"> '
		+ '		<PARAM NAME="DeviceFont" VALUE="0"> '
		+ '		<PARAM NAME="EmbedMovie" VALUE="0"> '
		+ '		<PARAM NAME="SWRemote" VALUE=""> '
		+ '		<PARAM NAME="MovieData" VALUE=""> '
		+ '		<PARAM NAME="SeamlessTabbing" VALUE="1"> '
		+ '		<PARAM NAME="Profile" VALUE="0"> '
		+ '		<PARAM NAME="ProfileAddress" VALUE=""> '
		+ '		<PARAM NAME="ProfilePort" VALUE="0"> '
		+ '		<PARAM NAME="wmode" VALUE="transparent"> '
		+ '		<embed  '
		+ '	src="' + file + '" quality="high" width="' + width + '"  '
		+ '	height="' + height + '" name="' + id + '" align="middle"  '
		+ '	allowScriptAccess="always" FlashVars="' + flashvars + '"  '
		+ '	type="application/x-shockwave-flash" wmode="transparent" '
		+ '	pluginspage="http://www.macromedia.com/go/getflashplayer" /> '
		+ '		</OBJECT> ';
		return s;
}

function SetVal(src, dest, cntrl)
{
    document.getElementById(dest + cntrl).value = document.getElementById(src + cntrl).value;
}

function validateText(txtbox, fieldName, validationCell)
{
    if (document.getElementById(txtbox).value == '')
    {
        document.getElementById(validationCell).innerHTML = 'Please enter a valid ' + fieldName;
        document.getElementById(txtbox).className = 'TextFormEntryInvalid';
        return false;
    }
    document.getElementById(validationCell).innerHTML = '&nbsp;';
    document.getElementById(txtbox).className = 'TextFormEntry';
    return true;
}

function checkTextBoxIsValid(txtbox, fieldName, validationCell)
{
    var val = validateText(txtbox, fieldName, validationCell);
    if (IsPageValid)
        IsPageValid = val;
}

function checkDropDownIsValid(dd, fieldName, validationCell)
{
    if (document.getElementById(dd).value == '-1')
    {
        document.getElementById(validationCell).innerHTML = 'Please enter a valid ' + fieldName;
        document.getElementById(dd).className = 'TextFormEntryInvalid';
        IsPageValid = false;
    }
    else
    {
        document.getElementById(validationCell).innerHTML = '&nbsp;';
        document.getElementById(dd).className = 'TextFormEntry';
    }
}

function checkTitleFormEntry(drp, txt, cl)
{
    d = document.getElementById(drp);
    t = document.getElementById(txt);
    c = document.getElementById(cl);
    d.className = 'TextFormEntry';
    t.className = 'TextFormEntry';
    c.innerHTML = '&nbsp;';
    
    if (d.value == '-1')
    {
        c.innerHTML = 'Please select a title';
        d.className = 'TextFormEntryInvalid';
        IsPageValid = false;
    }
    
    if ((d.value == 'Other') && (t.value == ''))
    {
        c.innerHTML = 'Please select a title';
        t.className = 'TextFormEntryInvalid';
        IsPageValid = false;   
    }
}

function validateEmail(txt, fieldName, validationCell)
{
    if ((txt.indexOf(".") > 2) && (txt.indexOf("@") > 0))
    {
        document.getElementById(validationCell).innerHTML = '&nbsp;';
        return true;
    }   
    document.getElementById(validationCell).innerHTML = 'Please enter a valid ' + fieldName;
    return false;
}

function checkDateSelected(Day, Month, Year, allowAllBlank, validationCell)
{
    var drpD = document.getElementById(Day);
    var drpM = document.getElementById(Month);
    var drpY = document.getElementById(Year);
    document.getElementById(validationCell).innerHTML = '&nbsp;'
    
    var isAllBlank = true;
    if ((drpD.value != '-') || (drpM.value != '-') || (drpY.value != '-'))
        isAllBlank = false;
    
    if (isAllBlank && allowAllBlank)
       return;
    else
    {
        if ((drpD.value == '-') || (drpM.value == '-') || (drpY.value == '-'))
        {
            document.getElementById(validationCell).innerHTML = 'Please select a complete date';
            IsPageValid = false;
        }
    } 
}

function drpDayDateValidate(Day, Month, Year, validationCell)
{
    document.getElementById(validationCell).innerHTML = '&nbsp;'
    var drpD = document.getElementById(Day);
    var drpM = document.getElementById(Month);
    var drpY = document.getElementById(Year);
    
    if (drpM.value != '-')
    {
        var max = GetMonthMaxDay(drpM.value, drpY.value);
        if (drpD.value > max)
            drpD.value = max;
    }
}

function GetMonthMaxDay(month, year)
{
    var max = 31;
    if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
        max = 30;
    
    if (month == 2)
    {
        if (year == '-')
            max = 28;
        else
        {
            if (year%4 == 0)
                max = 29;
            else
                max = 28;
        }
    }
    
    return max;
}


function isInteger (s)
{
   var i;

   if (isEmpty(s))
   if (isInteger.arguments.length == 1) return 0;
   else return (isInteger.arguments[1] == true);

   for (i = 0; i < s.length; i++)
   {
      var c = s.charAt(i);
      if (!isDigit(c)) return false;
   }

   return true;
}
function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
   return ((c >= "0") && (c <= "9"))
}
function TextBoxValueUpDown(id, dir)
{
    var txt = document.getElementById(id);
    var val = txt.value;
    if ((!isInteger(val)) || ((val * 1) + (dir * 1) < 1))
        txt.value = '1';
    else
        txt.value = (val * 1) + (dir * 1);
}

function getInnerText (node) 
{
	if (typeof node.textContent != 'undefined') {
		return node.textContent;
	}
	else if (typeof node.innerText != 'undefined') {
		return node.innerText;
	}
	else if (typeof node.text != 'undefined') {
		return node.text;
	}
	else {
		switch (node.nodeType) {
			case 3:
			case 4:
				return node.nodeValue;
				break;
			case 1:
			case 11:
				var innerText = '';
				for (var i = 0; i < node.childNodes.length; i++) {
					innerText += getInnerText(node.childNodes[i]);
				}
				return innerText;
				break;
			default:
			return '';
		}
	}
}

function EndsWith(string, search)
{
    return ( string.substr(string.length-search.length, search.length) == search)
}

function RemoveFromEnd(string, remove)
{
    if (EndsWith(string, remove))
        string = string.substr(0, string.length-remove.length);
    
    return string;
}