//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2009 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

// GOSPCommon.js

//GLOBAL VARIABLES
var formSubmitCount = 0;

// JS function to create coookie
function createCookie(name,value,days) {
	
	if (null != value && value.indexOf("&amp;") != -1) {
		value = value.replace(/&amp;/g, "&");
	}
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));	
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";	
}

// JS function to create UTF8Encoded coookie
function createUTF8EncodedCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));	
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	value = escape(_utf8_encode(value));
	document.cookie = name + "=" + value + expires + "; path=/";	
}

//This function is called when the user clicks on 'Sign in' button
function singleClickFormSubmit(form) {
	if (formSubmitCount == 0) {
		formSubmitCount++;
		form.submit();
	}    
}


function highlightNavigationTab () {
var navi2 = document.getElementById('mainnavi2ndlvl');
var navi2_li = navi2.getElementsByTagName('li');

var selectedTabLink = location.pathname;
  for (var i = 0; i < navi2_li.length; i++) {
  
    var link = navi2_li[i].getElementsByTagName('a')[0].pathname;
    if (link.substring(0, 1) != '/') {
    	link = '/' + link;
    }
    if (link == selectedTabLink) {
    	navi2_li[i].className = 'r50_active';
    }
    
  }
}


/*************** Added by Ujwal *************/
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  
  return true
}
/********************************************/

function popup(url, width, height, resizable) 
{
	 var width  = width;
	 var height = height;
	 //var left   = (screen.width  - width)/2;
	 //var top    = (screen.height - height)/2;
	 var left   = 10;
	 var top    = 10;
	 var params = 'width='+width+', height='+height;
	 params += ', top='+top+', left='+left;
	 params += ', directories=no';
	 params += ', location=no';
	 params += ', menubar=no';

	 if(resizable == undefined)
		 params += ', resizable=no';
	 else
	 	params += ', resizable=' + resizable;
	 	
	 params += ', scrollbars=yes';
	 params += ', status=no';
	 params += ', toolbar=no';
	 newwin=window.open(url,'windowname5', params);
	 if (window.focus) {newwin.focus()}
	 return false;
}


// method for UTF-8 encoding
function _utf8_encode(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}	

	return utftext;	
}	
 
// method for UTF-8 decoding
function _utf8_decode(utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}


//function to open footer links in new window

function wopen(url, name, w, h)
{
	// Fudge factors for window decoration space.
	 // In my tests these work well on all platforms & browsers.
	w += 32;
	h += 96;
	 var win = window.open(url,
	  name,
	  'width=' + w + ', height=' + h + ', ' +
	  'location=no, menubar=no, ' +
	  'status=no, toolbar=no, scrollbars=yes, resizable=yes');
	 win.resizeTo(w, h);
	 win.focus(); 
}		

/**
 * Validates the String with the US phone number and 
 * formats to xxx-xxx-xxxx
 **/
function checkUSPhone (phoneNoObj)
{
   var phoneNoVal=phoneNoObj.value;
   if ( null == phoneNoVal || "" == phoneNoVal ) return true;

   var normalizedPhone ="";
   //Strip all the characters except the numbers
   
   for (i = 0; i < phoneNoVal.length; i++)
   {
   	 // Check that current character is number.
	 var c =phoneNoVal.charAt(i);
	 if (isDigit(c)) normalizedPhone += c;
   }
   
   if(normalizedPhone.length == 10 ) {
	   if (( normalizedPhone.length > 3 ) && ( normalizedPhone.length <= 6 ) )
	   {
	   	  var len=normalizedPhone.length - 3;
		  phoneNoObj.value=reformat (normalizedPhone, "-", 3, "- ",len)
		  return true;
	   }
	   else if ( normalizedPhone.length > 6 )
	   {
		 var len=normalizedPhone.length - 6;	
		 phoneNoObj.value=reformat (normalizedPhone, 3, "-", 3, "-", len )
		 return true;
	   }
	   else
	   {
		 phoneNoObj.value=normalizedPhone;
		 return true;
	   }
   }
} //end checkUsPhone

/**
 *  Returns true if character c is a digit (0 .. 9).
 **/
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

/**
 *  Function formats the string based on the function arguments
 **/
function reformat (s){
    var arg;
    var sPos = 0;
    var argPos=0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 != 1) {
       		if(argPos < s.length)
       		resultString += arg;
        }
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
           argPos+=arg;
       }
    }
    return resultString;
}