//
// JavaScript Web Site
//

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) { //v3.0
  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); 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];}
}
function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=250,left = 262,top = 134');");
}

//If only height needs to be adjusted pass -1 in for width
function newDefaultWindow( URL )
{
	newWindow( URL, -1, -1 );
}

function newWindow(URL, aWidth, aHeight )
{
	var height = "440";
	var width  = "680";

	if( URL != "" )
	{
		if( aHeight != null )
		{
			if( aHeight > 0 )	
				height	=	aHeight;
		}
		if( aWidth != null )
		{
			if( aWidth > 0 )
				width	=	aWidth;
		}
		eval("pageWC= window.open(URL, 'WC', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=" + width + ",height=" + height + ",left = 262,top = 134');");
	}
}
// end of function newWindow()

function popinfo(url){
	var LeftPosition = (screen.width) ? (screen.width-300)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-275)/2 : 0;
	var mywin = window.open(url,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,WIDTH=500,height=350,top='+TopPosition+',left='+LeftPosition);
}

function checkTextArea(Obj,textLimit) {
	if(Obj.value.length > textLimit) {
		if(confirm('You are entering more than '+textLimit+' characters which is the maximum amount allowed.\nWhen saving all characters after '+textLimit+' will be removed.\n\nClick "OK" if you want the system to remove the extar characters. Click "Cancel" to remover the charactors yourself.')) 
			Obj.value = Obj.value.substr(0,textLimit);
	}
}

function setupValidateFormBeforeSubmit(sumbitType,sForm,LiveSite,Auth_UserID) {
	if(sumbitType == 'part') {
		if(Auth_UserID == 0) sForm.action = LiveSite+'search/AddPartNoLogin_action.cfm';
		else sForm.action = LiveSite+'buyer/addpart_action.cfm';
		if(sForm.Desiredpartnumber.value == '') {
			alert(providepartnumberalert);
			return false;
		}
		else if(String(sForm.Desiredpartnumber.value).length < 3) {
			alert(partnumthree);
			return false;
		}
		else return true;
	}
	
	if(sumbitType == 'quote') {
		sForm.action = LiveSite+'catalog7/rfq.cfm';
		if(sForm.part.value == '') {
			alert(providepartnumberalert);
			return false;
		}
		else if(String(sForm.part.value).length < 4) {
			alert(partnumfour);
			return false;
		}
		else {
			sForm.pn.value = sForm.part.value;
			return true;
		}
	}
}

function valLoginSubmit() {
	var lForm = document.login;
	
	if(lForm.username.value == '') {
		alert(usernamealert);
		return false;
	}
	
	if(lForm.password.value == '') {
		alert(passwordalert);
		return false;
	}
	
	return true;
}

function submitRefresh(formObj) {
	formObj.refreshLang.value = true;
	formObj.submit();
}

function getNewForm(fObj,noalert) {
	if(noalert || confirm('If you proceed with this action without saving any change(s) you made\nthe change(s) will be losted.\n\nClick \'OK\' to proceed.')) {
		fObj.makeNewPhrase.value = true;
		fObj.action = 'PhraseEditor.cfm';
		fObj.submit();
	}
}

function ascii_value(c) {
	// restrict input to a single character
	c = c.charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}

function checkStrForNonASCII(str) {
	for(var c = 0; c < str.length; ++c) {
		if(ascii_value(str.charAt(c)) > 255) return true;
	}
	return false;
}

function getQuote(formObj,partNum) {
	formObj.part.value = partNum;
	formObj.submit();
}

function sortColumn(sortCol) {
	var divCollection = document.resultsForm.getElementsByTagName("tr");
	
	for (var i=0; i<divCollection.length; i++) {
		if(String(divCollection[i].getAttribute("id")).indexOf(sortCol) != -1) {
			divCollection[i].style.visibility = 'visible';
			divCollection[i].style.position = 'relative';
		} else if(divCollection[i].getAttribute("id") != '' && divCollection[i].getAttribute("id") != null) {
			divCollection[i].style.visibility = 'hidden';
			divCollection[i].style.position = 'absolute';	
		}
	}	

	document.getElementById("sorttable").style.visibility = 'visible';
	document.getElementById("sorttable").style.position = 'relative';
}