﻿var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "An unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function ArticleView(url) 
{
    link = window.open(url,"Article","toolbar=0,loczation=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=500,left=40,top=50");
} 
//below function is used when displaying links and resources in a dropdown
//this will hide/show the anchor element next to the dropdown based on the selection
function setAnchorElement(ctrl, id) 
{ 
   var selIndex = ctrl.selectedIndex;
   var selValue = ctrl[selIndex].value.split("[#####]");
   if (selValue[0] != "#")
   {
        document.getElementById(id).style.display="";
        document.getElementById(id).href=selValue[0];
        document.getElementById(id).target=selValue[1];
   }
   else
   {
        document.getElementById(id).style.display="none";
   }

}    
//below function is used when displaying links and resources in a dropdown
//this will hide/show the anchor element next to the dropdown based on the selection
function RedirectLinkResource(ctrl) 
{ 
   var selIndex = ctrl.selectedIndex;
   var selValue = ctrl[selIndex].value.split("[#####]");
   if (selValue[0] != "#")
   {
        if (selValue[1] == "_self")
        {
            window.navigate(selValue[0]);
        }
        else
        {
            window.open(selValue[0],"LinkResource");
        }
   }


} 
//below function is used for displaying news in a popup window
function openpopup(Path) 
{ 
   if (window.showModalDialog) 
    {
        window.showModalDialog(Path,"newclient","dialogWidth:750px;dialogHeight:475px");
    } 
    else
    {
        window.open(Path,"newwin","top=100,left=100,width=750,height=475,resizable=yes,scrollbars=yes").focus();
    }
} 
//below function is used for displaying dynamic result
function Symm_UpdateExpressionResult(behaviorId, ExpressionId, FormatType, FormId, PageId, ControlId, ControlType, Control) 
{
    var behavior = $find(behaviorId);
    arrHdn = document.getElementsByTagName('INPUT');
    var ctrlHdn;
    for (m=0;m<arrHdn.length;m++)
    {
        if (arrHdn[m].type=="hidden" && arrHdn[m].id.indexOf("hdnResponseXML_" + FormId) > -1)
        {
            ctrlHdn = arrHdn[m];
            break;
        }
    }
    var xml = ctrlHdn.value;
    var UserResponseNode = '';
    if (xml.lastIndexOf("<UserResponseId>")>-1)
    {
        UserResponseNode = xml.substring(xml.lastIndexOf("<UserResponseId>"));
        xml = xml.substring(0, xml.lastIndexOf("<UserResponseId>"));
    }
    var xmlDoc = xml;
    try //Internet Explorer
      {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async=false;
        xmlDoc.loadXML(xml);
        x=xmlDoc.childNodes[0].childNodes;
        for (j=0;j<x.length;j++)
        {
            if (x[j].getAttribute("Id") == PageId)
            {
	            xc = x[j].childNodes;
	            for (i=0;i < xc.length;i++)
	            {
		            if (xc[i].getAttribute("Id") == ControlId)
		            {
                        xmlDocNew = new ActiveXObject("Microsoft.XMLDOM");
                        xmlDocNew.async=false;
			            xcc = xc[i].getElementsByTagName("ControlValue")[0];
			            xmlDocNew.loadXML(CreateXMLData(PageId, ControlId, ControlType, Control, "Value"));
			            newNode = xmlDocNew.childNodes[0];
			            xc[i].replaceChild(newNode, xcc);
			            
                        xcc = xc[i].getElementsByTagName("ControlText")[0];
                        xmlDocNew.loadXML(CreateXMLData(PageId, ControlId, ControlType, Control, "Text"));
			            newNode = xmlDocNew.childNodes[0];
			            xc[i].replaceChild(newNode, xcc);
			            
		            }
	            }
            }
        }
        xml = xmlDoc.xml;
      }
    catch(e)
      {
      try //Firefox, Mozilla, Opera, etc.
        {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(xml,"text/xml");
            x = xmlDoc.childNodes[0].childNodes;
            
            for (j=0;j < x.length;j++)
            {
                if (x[j].getAttribute("Id") == PageId)
                {
	                xc = x[j].childNodes;
	                for (i=0;i<xc.length;i++)
	                {
		                if (xc[i].getAttribute("Id") == ControlId)
		                {
                            parserNew = new DOMParser();
                            
                            xcc = xc[i].getElementsByTagName("ControlValue")[0];
                            xmlDocNew = parserNew.parseFromString(CreateXMLData(PageId, ControlId, ControlType, Control, "Value"),"text/xml");
                            newNode = xmlDocNew.childNodes[0];
                            xc[i].replaceChild(newNode, xcc);

                            xcc = xc[i].getElementsByTagName("ControlText")[0];
                            xmlDocNew = parserNew.parseFromString(CreateXMLData(PageId, ControlId, ControlType, Control, "Text"),"text/xml");
                            newNode = xmlDocNew.childNodes[0];
                            xc[i].replaceChild(newNode, xcc);
		                }
	                }
                }
            }
            var objXMLSerializer = new XMLSerializer();
            xml=objXMLSerializer.serializeToString(xmlDoc);
        }
      catch(e) {alert(e.message)}
      }
    ctrlHdn.value = xml + UserResponseNode;
    if (behavior) {
        behavior.populate(xml + UserResponseNode + "<ExpressionId>" +  ExpressionId + "</ExpressionId><FormatType>" +  FormatType + "</FormatType>");
    }
}  
function CreateXMLData(PageId, ControlId, ControlType, Control, ValueOrText)
{
    var ValueXML;
    switch (ControlType)
    {
        case "LBL":
        case "MTXT":
        case "TXT":
        {
            if (ValueOrText == "Text")
                ValueXML = "<ControlText><Row1><Column1><Value0>" + Control.value + "</Value0></Column1></Row1></ControlText>";
            else
                ValueXML = "<ControlValue><Row1><Column1><Value0>" + Control.value + "</Value0></Column1></Row1></ControlValue>";

            break;
        }
        case "DP":
        {
            if (ValueOrText == "Text")
                ValueXML = "<ControlText><Row1><Column1><Value0>" + Control.value + "</Value0></Column1></Row1></ControlText>";
            else
                ValueXML = "<ControlValue><Row1><Column1><Value0>" + Control.value + "</Value0></Column1></Row1></ControlValue>";

            break;
        }
        case "RBL":
        {
            if (ValueOrText == "Text")
            {
                var text = "";
                var labels=document.getElementsByTagName("label"),i;
                for( i=0; i<labels.length;i++ )
                {
                    if(labels[i].htmlFor==Control.id)
                    {
                        text = labels[i].innerHTML;
                    }
                }            
                ValueXML = "<ControlText><Row1><Column1><Value1>" + text + "</Value1></Column1></Row1></ControlText>";
            }
            else
                ValueXML = "<ControlValue><Row1><Column1>" + Control.value + "</Column1></Row1></ControlValue>";
            break;
        }
//        case "SMTX":
//        {
//            if (ValueOrText == "Text")
//            {
//                var text = "";
//                var labels=document.getElementsByTagName("label"),i;
//                for( i=0; i<labels.length;i++ )
//                {
//                    if(labels[i].htmlFor==Control.id)
//                    {
//                        text = labels[i].innerHTML;
//                    }
//                }            
//                ValueXML = "<ControlText><Column1><Value1>" + text + "</Value1></Column1></ControlText>";
//            }
//            else
//                ValueXML = "<ControlValue><Column1>" + Control.value + "</Column1></ControlValue>";
//            break;
//        }        
        case "CDD":
        case "DDL":
        {
            if (ValueOrText == "Text")
            {
                ValueXML = "<ControlText><Row1><Column1><Value1>" + Control.options[Control.selectedIndex].text + "</Value1></Column1></Row1></ControlText>";
            }
            else
                ValueXML = "<ControlValue><Row1><Column1>" + Control.value + "</Column1></Row1></ControlValue>";
            break;
        }  
        case "CB":
        {
            var arrCheckValue = Control.value.split("~");
            var selValues = "";
            var selTexts = "";
            if (Control.checked)
            {
                selValues = "true";
                selTexts = arrCheckValue[0];
            }
            else
            {
                selValues = "false";
                selTexts = arrCheckValue[1];
            }
            if (ValueOrText == "Text")
                ValueXML = "<ControlText><Row1><Column1><Value1>" + selTexts + "</Value1></Column1></Row1></ControlText>";
            else
                ValueXML = "<ControlValue><Row1><Column1><Value1>" + selValues + "</Value1></Column1></Row1></ControlValue>";

            break;
        }               
    }
    return ValueXML;
}

function rnd() {
    today=new Date();
    jran=today.getTime();

    ia=9301;
    ic=49297;
    im=233280;
    jran = (jran*ia+ic) % im;
    return jran/(im*1.0);
}

function openHelp(name) 
{ 
	 
	if (window.showModalDialog) {
	    var myRand = Math.ceil(rnd()*50000);
		var returnValue = window.showModalDialog(name + "&rand=" + myRand, "newclient","scroll:no;resizable:no;status:no;dialogWidth:990px;dialogHeight:660px");
		if(returnValue != null && returnValue!='')
		{
		}
	} 
	else
	{
		window.open(name,"newwin","top=100,left=100,width=990,height=660,resizable=no,scrollbars=no").focus();
	}
} 
function openExtrasPopup(name)
{
    if (window.showModalDialog) {
	    var myRand = Math.ceil(rnd()*50000);
		var returnValue = window.showModalDialog(name + "&rand=" + myRand, "newclient","scroll:yes;resizable:yes;status:no;dialogWidth:750px;dialogHeight:550px");
		if(returnValue != null && returnValue!='')
		{
		}
	} 
	else
	{
		window.open(name,"newwin","top=100,left=100,width=900,height=550,resizable=yes,scrollbars=yes").focus();
	}
}
function openContactViewPopup(name) 
{ 
	if (window.showModalDialog) {
	    var myRand = Math.ceil(rnd()*50000);
		var returnValue = window.showModalDialog(name + "&rand=" + myRand, "newclient","scroll:no;resizable:yes;status:no;dialogWidth:400px;dialogHeight:165px");
		if(returnValue != null && returnValue!='')
		{
		}
	} 
	else
	{
		window.open(name,"newwin","top=100,left=100,width=400,height=165,resizable=yes,scrollbars=yes").focus();
	}
} 
function openNewsViewPopup(name) 
{ 
	if (window.showModalDialog) {
	    var myRand = Math.ceil(rnd()*50000);
		var returnValue = window.showModalDialog(name + "&rand=" + myRand, "newclient","scroll:yes;resizable:yes;status:no;dialogWidth:750px;dialogHeight:475px");
		if(returnValue != null && returnValue!='')
		{
		}
	} 
	else
	{
		window.open(name,"newwin","top=100,left=100,width=750,height=475,resizable=yes,scrollbars=yes").focus();
	}
} 
function openWorkflowNotesViewPopup(name) 
{ 
	if (window.showModalDialog) {
	    var myRand = Math.ceil(rnd()*50000);
		var returnValue = window.showModalDialog(name + "&rand=" + myRand, "newclient","scroll:no;resizable:yes;status:no;dialogWidth:750px;dialogHeight:475px");
		if(returnValue != null && returnValue!='')
		{
		}
	} 
	else
	{
		window.open(name,"newwin","top=100,left=100,width=750,height=475,resizable=yes,scrollbars=yes").focus();
	}
} 
function openOpportunityViewPopup(name) 
{ 
	if (window.showModalDialog) {
	    var myRand = Math.ceil(rnd()*50000);
		var returnValue = window.showModalDialog(name + "&rand=" + myRand, "newclient","scroll:yes;resizable:yes;status:no;dialogWidth:750px;dialogHeight:475px");
		if(returnValue != null && returnValue!='')
		{
		}
	} 
	else
	{
		window.open(name,"newwin","top=100,left=100,width=750,height=475,resizable=yes,scrollbars=yes").focus();
	}
} 
function checkDecimals(id, evt)
{
    var txtControl = (evt.srcElement) ? document.getElementById(evt.srcElement.id).value : document.getElementById(evt.target.id).value ;
	var txtLength=txtControl.length;
	var fieldvalues=new Array(txtLength);
	var charCode = (evt.which) ? evt.which : event.keyCode

	if(txtControl.length>0)
	{
		for(i=0;i<txtLength;i++)
		{
			fieldvalues[i]=txtControl.slice(i, i + 1);
		}
		if(charCode==0x2E)
		{
			for(i=0;i<txtLength;i++)
			{
				if(fieldvalues[i]=='.')
				{
					charCode = 0;
				}
			}
		}
	}
	pass =(((charCode >= 0x30) && (charCode <= 0x39))||((charCode == 0x2E))||((charCode == 8)));
	if (pass == false)
	{
		charCode = 0;
	}
	return pass;
}

/* Date validation dd/MM/yyyy starts here*/
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function checkDate(ctrl, format){
	dtStr = ctrl.value;
	if (dtStr == "")
		return true;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
    var strDay;
    var strMonth;
    var strYear;
	if (format == "dd/MM/yyyy")
	{
	    strDay=dtStr.substring(0,pos1)
	    strMonth=dtStr.substring(pos1+1,pos2)
	}
	else if (format == "MM/dd/yyyy")
	{
	    strMonth=dtStr.substring(0,pos1)
	    strDay=dtStr.substring(pos1+1,pos2)
	}
    strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : " + format);
		ctrl.value="";
		ctrl.focus();
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		ctrl.value="";
		ctrl.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		ctrl.value="";
		ctrl.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		ctrl.value="";
		ctrl.focus();
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		ctrl.value="";
		ctrl.focus();
		return false
	}
return true
}
/* Date validation dd/MM/yyyy starts here*/

var fontSize = 1;
function ZoomIn() {
	fontSize += 0.1;
	document.body.style.fontSize = fontSize + "em";
}
function ZoomOut() {
	fontSize -= 0.1;
	document.body.style.fontSize = fontSize + "em";
}


/* Horizontal Menu with image starts here*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/* Horizontal Menu with image ends here*/
//this function is used for removing session variables in the admin side
//in the master page we use one hidden field for storing the required data instead of storing in session
function SubmitForm(path)
{
    document.aspnetForm.method='post';
    document.aspnetForm.action=path;
    document.aspnetForm.submit();
}
function getAlternative(divid, alternateDivId)
{
  document.getElementById("tbl" + divid + "_" + alternateDivId).style.display ="" ;
  document.getElementById("tbl" + alternateDivId + "_" + divid).style.display ="none" ;
}

function formDurationElapsed(hiddenFieldId, redirectPath, timeOutMessage)
{
    var arrTimer = document.getElementById(hiddenFieldId).value.split(",");
    if(arrTimer[1] == 'false')
    {
        alert(timeOutMessage);
        document.getElementById(hiddenFieldId).value = "0,true";
        __doPostBack('', 'TimedForm');
    }
}
function scrollToTop()
{
window.scrollTo(0,0);
}

