
//Contains the global javascript functions for ordercentral

String.prototype.trim = function()
{
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

function displayItemExtensionsOnFocusIn(mySelectBox)
{
	mySelectBox.style.backgroundColor="#ffffff";
}

function displayItemExtensionsOnFocus(mySelectBox)
{
	mySelectBox.style.backgroundColor="#ffffff";
}

function removeBadNumbers(inNumber)
{
	tmpNumber = "";

	for(i=0;i<inNumber.length;i++)
	{
		tmpStr = inNumber.substring(i, i+1)
		if(tmpStr == "-" || isNaN(tmpStr) || tmpStr == " ")
		{

		}
		else
		{
			tmpNumber = tmpNumber + inNumber.substring(i, i+1);
		}
	}
	
	return tmpNumber;
}

function doQuickSearch(searchPath)
{
	var searchString = '';
	try
	{
		searchString = document.getElementById('OCForm1').tSearchString.value;
		searchString = searchString.replace("(","").replace(")","").replace(" and "," ").replace(" & "," ");
	}catch(e){}

	if (searchString != '')
	{
		window.location = searchPath + 'search.aspx?actionType=QUICKSEARCH&posting=TRUE&searchString=' + escape(searchString);
	}
	else
	{
		alert('Enter a search term');
	}
}

function checkQuickSearch(myevent, searchPath)
{
		//if they hit the enter key
		if (myevent.keyCode == 13)
		{
			myevent.cancelBubble = true;
			myevent.returnValue = false;
			doQuickSearch(searchPath);
			myevent.keyCode = 8;
		
			return false;
		}
		else
		{
			return true;
		}
	
	return false;
}




function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
	try
	{
		var imageWindow = window.open(theURL,winName,features);
		imageWindow.focus();
	}catch(exception){}	
}

function openImageWindow(imageURL)
{
	MM_openBrWindow(imageURL,'enlargedImage','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=602,height=401');

/*	//var MyArgs= new Array(ParmA, ParmB, ParmC);
	var WinSettings = "status=1,toolbar=0,location=1,menubar=0,resizable=1,scrollbars=1,height=480,width=640,directories=0"
	
	try
	{
		var imageWindow = window.open(imageURL, "LargeImage", WinSettings, true);
		imageWindow.focus();
	}
	catch(exception){}
*/

}


function openPopupWindow(windowURL)
{
	//var MyArgs= new Array(ParmA, ParmB, ParmC);
	var WinSettings = "status=1,toolbar=1,location=0,menubar=1,resizable=1,scrollbars=1,height=480,width=680,directories=0"
	
	try
	{
		var popupWindow = window.open(windowURL, "Popup", WinSettings, true);
		popupWindow.focus();
	}
	catch(exception){}
}

function getTextBlockPopup(textType, textName)
{
	openPopupWindow('textBlock.aspx?textType=' + escape(textType) + '&textName=' + escape(textName));
}

function isDate(DateToCheck)
{
	if(DateToCheck==""){return true;}
	var m_strDate = FormatDate(DateToCheck);
	if(m_strDate=="")
	{
		return false;
	}
	var m_arrDate = m_strDate.split("/");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];
	if(m_YEAR.length > 4){return false;}
	
	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
	var testDate=new Date(m_strDate);

	if(testDate.getMonth()+1==m_MONTH)
	{
		return true;
	} 
	else
	{
		return false;
	}
}//end function



function FormatDate(DateToFormat,FormatAs){
if(DateToFormat==""){return"";}
if(!FormatAs){FormatAs="dd/mm/yyyy";}

var strReturnDate;
FormatAs = FormatAs.toLowerCase();
DateToFormat = DateToFormat.toLowerCase();
var arrDate
var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var strMONTH;
var Separator;

while(DateToFormat.indexOf("st")>-1){
DateToFormat = DateToFormat.replace("st","");
}

while(DateToFormat.indexOf("nd")>-1){
DateToFormat = DateToFormat.replace("nd","");
}

while(DateToFormat.indexOf("rd")>-1){
DateToFormat = DateToFormat.replace("rd","");
}

while(DateToFormat.indexOf("th")>-1){
DateToFormat = DateToFormat.replace("th","");
}

if(DateToFormat.indexOf(".")>-1){
Separator = ".";
}

if(DateToFormat.indexOf("-")>-1){
Separator = "-";
}


if(DateToFormat.indexOf("/")>-1){
Separator = "/";
}

if(DateToFormat.indexOf(" ")>-1){
Separator = " ";
}

arrDate = DateToFormat.split(Separator);
DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
		DateToFormat += arrDate[iSD] + Separator;
		}
	}
DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
arrDate = DateToFormat.split(Separator);

if(arrDate.length < 3){
return "";
}

var DAY = arrDate[0];
var MONTH = arrDate[1];
var YEAR = arrDate[2];




if(parseFloat(arrDate[1]) > 12){
DAY = arrDate[1];
MONTH = arrDate[0];
}

if(parseFloat(DAY) && DAY.toString().length==4){
YEAR = arrDate[0];
DAY = arrDate[2];
MONTH = arrDate[1];
}


for(var iSD = 0;iSD < arrMonths.length;iSD++){
var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
var MonthPosition = DateToFormat.indexOf(ShortMonth);
	if(MonthPosition > -1){
	MONTH = iSD + 1;
		if(MonthPosition == 0){
		DAY = arrDate[1];
		YEAR = arrDate[2];
		}
	break;
	}
}

var strTemp = YEAR.toString();
if(strTemp.length==2){

	if(parseFloat(YEAR)>40){
	YEAR = "19" + YEAR;
	}
	else{
	YEAR = "20" + YEAR;
	}

}


	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
	return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
	return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
	return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
	return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

return DAY + "/" + strMONTH + "/" + YEAR;;

} //End Function



function isValidZipCode(value) {
   var re = /^\d+([\-]\d+)?$/;

   return true;
   //return (re.test(value));
}

function isValidUsZipCode(value) {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}

function createCookie(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 = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
			c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0)
			{
				return c.substring(nameEQ.length,c.length);
			}
		}
	}
	return '';
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
	
}


function doChangeLanguage(theListElement)
{
	//ddlChangeLanguage
	var newLanguage = theListElement.options[theListElement.selectedIndex].value;
	
	var referrer = "";

	if (newLanguage != "")
	{
		window.location = "./changeLanguage.aspx?ddlChangeLanguage=" + escape(newLanguage) + "&referrer=" + escape(referrer);
	}
}
  
 
function paymentChange (theValue, fieldNo) 
{
  
  
}
function clearInput(x){
document.getElementById(x).value="";
}	
function newWindow(page, xVal, yVal)
{
	var newWindow=window.open(page,'theWin','top=100,left=100,toolbar=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,copyhistory=0,width='+xVal+',height='+yVal);
	newWindow.focus();
}


var dateFormat = function () {
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && (typeof date == "string" || date instanceof String) && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date();
		if (isNaN(date)) throw new SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};


