// ---------------------------------------------------------
//	GLOBAL VARIABLES
//----------------------------------------------------------
var popWin;
var popWinType;
var ie;

ie = (document.all) ? true : false;

/* -----------------------------------------------
FUNCTION: helpModLvl 
DESC: Create a module level pop up window
ARGS:
	hrefTarget - url to be opened
-------------------------------------------------*/
function helpModLvl(hrefTarget) {
	//Set Variables
	var name, width, height;
	name = "helpModLvl";
	width="463";
	height="500";
	// Pop up the window
	popWindow(hrefTarget,name,width,height,resizable="yes");
}

/* -----------------------------------------------
FUNCTION: helpFieldLvl 
DESC: Create a field level pop up window
ARGS:
	hrefTarget - url to be opened
-------------------------------------------------*/
function helpFieldLvl(hrefTarget) {
//Set Variables
	var name, width, height;
	name = "helpFieldLvl";
	width="323";
	height="500";
	popWindow(hrefTarget,name,width,height,resizable="yes");
}

/* -----------------------------------------------
FUNCTION: printerFriend 
DESC: Create a printer friendly pop up window
ARGS:
	hrefTarget - url to be opened
-------------------------------------------------*/
function printerFriend(hrefTarget) {
//Set Variables
	var name, width, height;
	name = "printerVersion";
	width="603";
	height="500";
	popWindow(hrefTarget,name,width,height,resizable="yes");
}


/* -----------------------------------------------
FUNCTION: funSetPopUpFocus
DESC: focus on a request window (used to implement modal window)
ARGS:
	win- window to focus on
-------------------------------------------------*/
function SetPopUpFocus(win) {		
	if (win != null && !win.closed)
		win.focus();						
}


/* -----------------------------------------------
FUNCTION: newWindow 
DESC: Create a new browser window
ARGS:
	hrefTarget - url to be opened
-------------------------------------------------*/
function newWindow(hrefTarget) {
//Set Variables
	window.open(hrefTarget)
}

/* -----------------------------------------------
FUNCTION: popWindow 
DESC: Opens a window and positions it on the screen

ARGS:
	hrefTarget - url to be opened
	name - name of popup window (becomes popupType)
	width - width of the pop up window
	height - height of the pop up window
-------------------------------------------------*/
function popWindow(hrefTarget,name,width,height) {
// Create offset
	if (document.all) {
		xMax = screen.width, yMax = screen.height;
	} else {
		if (document.layers) {
			xMax = window.outerWidth, yMax = window.outerHeight;
		} else {
			xMax = 640, yMax=480;
		}
	}
	var xOffset = (xMax - 586)/2, yOffset = (yMax - 700)/2;
//Check if pop up exists	
	if (!popWin||popWin.closed) {
		// no pop up exists set the popWinType to the current type
		popWinType = name;
		// open pop up window
		popWin = window.open(hrefTarget,name,'width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+', top='+yOffset+',left='+xOffset+',scrollbars=yes,menubar=yes,resizable=yes');
	} else {
		// Check if the pop up is the same type
		if (popWinType != name) {
		// it is not the same type so close the current pop up
		popWin.close();
		// set the popWinType to the current type
		popWinType = name;
		// open pop up window
		popWin = window.open(hrefTarget,name,'width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+', top='+yOffset+',left='+xOffset+',scrollbars=yes,menubar=yes,resizable=yes');
		return;
		}
		// Bring pop up to the foreground
		popWin.focus();
		// Change the location of the pop up to the location being requested
		popWin.location = hrefTarget;
	}		
}

/* -----------------------------------------------
FUNCTION: popLanguage
DESC: Create a module level pop up window
ARGS:
	formName - url to be opened
	sel - name of the select element from which to
		  get the value.
-------------------------------------------------*/
function popLanguage(formName,sel) {
	//Set Variables
	var name, width, height, fieldValue, localeValue, hrefTarget,objTest,bolTestResult,npc;
	name = "LanguageSelection";
	width ="316";
	height ="350";
	objTest = /_none/;
	fieldValue = sel.options[sel.selectedIndex].value;
	localeValue = fieldValue.substring(3,5) + "_" + fieldValue.substring(0,2);
	bolTestResult = objTest.test(fieldValue);

	if (bolTestResult) {
		formName.submit();
	} else if(fieldValue == ""){
		alert('Please select a Country')
	} else {
		location.href="/content/"+fieldValue.substring(0,2)+"/"+fieldValue.substring(3,5)+"/index.jsx";
	}
	return false;
}

/* -----------------------------------------------
FUNCTION: setPreferences
DESC: calls the url to change the language / homepage
		preferences...then closes the window.
ARGS:
	strRedirectorKey - url
	strPermanentFlag - flag that says whether or not
					   the use wants to save these settings.
					   ("yes"/"no")
-------------------------------------------------*/
function setPreferences(strRedirectorKey,strPermanentFlag) {
	if (opener)
	{
		opener.location.href = "/?cookie="+strRedirectorKey+"&setCookie="+strPermanentFlag;
		window.close();
	}
}

/* -----------------------------------------------
FUNCTION: popWrapper
DESC: Wrapper function takes in a url and a popup type
ARGS:
	hrefTarget - url to be opened
	popType - type of popup to use

NOTES:
	valid popup types are:
	pop_up_2_col = 2-Column Help
	pop_up_3_col = 3-Column Help
	pr_fr_3_col = 3 column printer friendly page
	pr_fr_4_col = 4 column printer friendly page
	pop_up_full_page = pop up full page
	pop_up_4_col = 4- Column Help
-------------------------------------------------*/
function popWrapper(hrefTarget,popType) {
	if ((popType == "pop_up_2_col"))
	{
		helpFieldLvl(hrefTarget);
	}
	else if ((popType == "pop_up_3_col") || (popType == "pop_up_3_col") || (popType == "pr_fr_3_col"))
	{ 
		helpModLvl(hrefTarget);
	}
	else if ((popType == "pop_up_4_col") ||(popType == "pr_fr_4_col"))
	{ 
		printerFriend(hrefTarget);
	}
	else if ((popType == "pop_up_full_page")) 
	{
		newWindow(hrefTarget);
	}
	else {
		return;
	}
}

function PopupClose()
{
	window.close();
}

//to check for valid date

	function isValidTextDate(formName,fieldName)
{

	var returnFlag	= true;
	var frmIndex	= findFormIndex(formName);
	
	var fieldIndex	= findElementIndex(frmIndex, fieldName);
	alert(fieldIndex);
	
	// getting the values of Date

	var strDate = document.forms[frmIndex].elements[fieldIndex].value;
	var intDateLen = strDate.length;
	if (intDateLen > 0){
		if (intDateLen > 10)
		{
			alert("Invalid Date Entered");
			document.forms[frmIndex].elements[fieldIndex].focus();
			returnFlag = false; 
			return false;
		}

		strDate = strDate.replace('-','/');
		strDate = strDate.replace('-','/');
	
		var arrDate = strDate.split('/');
	
		var dd = arrDate[1]
		var mm = arrDate[0]
		var yy = arrDate[2]

		if (mm==0 || isNaN(mm))
		{
		 alert('Please Enter Valid Date');
		 document.forms[frmIndex].elements[fieldIndex].focus();
		 returnFlag = false; 
		 return false;
		 }
		else if(dd==0 || isNaN(dd))
		{
 		 alert('Please Enter Valid Date');
		 document.forms[frmIndex].elements[fieldIndex].focus();
		 returnFlag = false; 
		 return false;
		 }
		else if(yy==0 || isNaN(yy))
		{
		 alert('Please Enter Valid Date');
		 document.forms[frmIndex].elements[fieldIndex].focus();
		 returnFlag = false; 
 		 return false;
		 }
		/*
		else if (yy.length != 4 || yy<1900) 
		{
		 alert('Please Enter the Year in yyyy format e.g (2003)');		
 		 document.forms[frmIndex].elements[fieldIndex].focus();
 		 returnFlag = false; 
		 return false;
		}
		*/
		else	// Month Validation
		{

			if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
			 	totDays=31;				//	if the month is Jan,Mar,May,July,Aug,Oct and Dec
			else if(mm==4 || mm==6 || mm==9 || mm==11)
			    totDays=30;				//	if the month is Apr,Jun,Sep and Nov
				else if(yy%4==0 && yy%100!=0 || yy%400==0)
					totDays=29;			//	if the year is Leap Year
					else  
					    totDays=28;		//	if the year is not a Leap Year
						   
			if (dd > totDays)
			 {
			 	alert("Please select valid Date");
			 	document.forms[frmIndex].elements[fieldIndex].focus();
			 	returnFlag=false;
			 	return false; 
			 }
			 
			if (mm<1 || mm>12)			//	if the month is not a valid Month
			 {
			 	alert("Please select valid Month");
			 	document.forms[frmIndex].elements[fieldIndex].focus();
			 	returnFlag=false;
			 	return false; 
			 }
	
		}		// end of else for Month Validation
	}
		if(returnFlag==true)
		{
			return true;
		}	
}

// ***********    Function to  the find Index of specified Form		***********    
function findFormIndex(formName)
{
	var frmLen = document.forms.length ;
	var frmIndex;
	var formFlag = false;
		
	for (var i=0; i<frmLen; i++)
	{	
		if(document.forms[i].name == formName )
		{
			frmIndex = i;
			formFlag = true;
		}
	}
	
	if (formFlag==true)
		return frmIndex;
	else
	{
	    alert('Invalid Form Name');
	    return false;
	}
	    
}		// end of Function  findFormIndex(formName)





// ***********    Function to  the find Index of specified Element		***********    


function findElementIndex(frmIndex, fieldName)
{
	var elemLen = document.forms[frmIndex].elements.length;
	var fieldIndex;
	var fieldFlag = false;
	
	for (var i=0; i<elemLen; i++)
	{	
		if(document.forms[frmIndex].elements[i].type == 'text'  || 
			document.forms[frmIndex].elements[i].type == 'select-one')
		{	
			if (document.forms[frmIndex].elements[i].name == fieldName)
			{
				fieldIndex = i;
				fieldFlag = true;
			}	
		}
	}
	if (fieldFlag == true)
		return fieldIndex;
	else
	{
	    alert('Invalid Field Name');
	    return false;
	}
	
}		// end of Function findElementIndex(frmIndex, fieldName) 

	
function Trim(str)
{
	while (str.charAt(0) == " ") str = str.substring(1);

	while (str.charAt(str.length - 1) == " ")  str = str.substring(0, str.length - 1);

	return str;

}


function IsTypedCharNumeric()
{	
	var ValidChars = "0123456789., -";
	var IsNumber = true;
	var charTyped = window.event.keyCode;
	var IsValidChar = (ValidChars.indexOf(String.fromCharCode(charTyped)) >= 0);
	
	return (IsValidChar || charTyped == 8 || charTyped == 13 || charTyped == 9)
	/*
	{
		return true;
 	}
	else
  	{
		if (ie)
		{
			window.event.returnValue = null;
		}
		else 
		{
			e.preventDefault();
		}
		
	    return false;
    }
    */
}


function addOnloadEvent2(fnc)
{
	if ( typeof window.addEventListener != "undefined" )
		window.addEventListener( "load", fnc, false );
	else if ( typeof window.attachEvent != "undefined" ) {
		window.attachEvent( "onload", fnc );
	}
	else {
		if ( window.onload != null ) {
		var oldOnload = window.onload;
		window.onload = function ( e ) {
			oldOnload( e );
			window[fnc]();
		};
		}
		else 
		window.onload = fnc;
	}
}

function LoadMainContent(url, height, pagetitle)
{
    var spanTitle = document.getElementById('spanPageSubTitle');
    if (spanTitle != null)
        spanTitle.innerHTML = pagetitle;
    var newLocation = window.location.protocol + '//' + window.location.host + 
                        "/uStore/CustomMain.aspx?url=" + url 
                        + ((height != null)? "&height="+height : "")
                        + ((pagetitle != null)? "&pagetitle="+pagetitle : "");
    window.location.href = newLocation;    
}

//Dynamic DUKES
function UpdateDialValue(val,txtDialValueClientId)
{
  document.getElementById(txtDialValueClientId).value = val;
}                           

//Encode text to HTML
function escapeHTML(str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}

// Obsolete - This function is not supported in FireFox 
//function GetElementPosition2(element) {
//    var result = new Object();
//    result.x = 0;
//    result.y = 0;
//    result.width = 0;
//    result.height = 0;
//    if (element.offsetParent) {
//        result.x = element.offsetLeft;
//        result.y = element.offsetTop;
//        var parent = element.offsetParent;
//        while (parent) {
//            result.x += parent.offsetLeft;
//            result.y += parent.offsetTop;
//            var parentTagName = parent.tagName.toLowerCase();
//            if (parentTagName != "table" &&
//                parentTagName != "body" && 
//                parentTagName != "html" && 
//                parentTagName != "div" && 
//                parent.clientTop && 
//                parent.clientLeft) {
//                result.x += parent.clientLeft;
//                result.y += parent.clientTop;
//            }
//            parent = parent.offsetParent;
//        }
//    }
//    else if (element.left && element.top) {
//        result.x = element.left;
//        result.y = element.top;
//    }
//    else {
//        if (element.x) {
//            result.x = element.x;
//        }
//        if (element.y) {
//            result.y = element.y;
//        }
//    }
//    if (element.offsetWidth && element.offsetHeight) {
//        result.width = element.offsetWidth;
//        result.height = element.offsetHeight;
//    }
//    else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
//        result.width = element.style.pixelWidth;
//        result.height = element.style.pixelHeight;
//    }
//    return result;
//}

