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_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_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];}
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function showResultBox(obj,url,boxLengthId) {	
	var node = document.getElementById('search_result_box');
	if(trim(obj.value) != '') {
		var searchBoxLength = document.getElementById(boxLengthId);
		
		var searchLeftPos = findPosX(obj);
		var searchBoxLengthLeftPos = findPosX(searchBoxLength);
		//alert(searchBoxLengthLeftPos);
		if(boxLengthId == 'search_box_length2')
		{
			var space = 4;
			var height = 20;
		}
		else if(boxLengthId == 'search_box_length')
		{
			var space = 1;
			var height = 22;
		}
		node.style.width=(searchBoxLengthLeftPos - searchLeftPos) - space + 'px';
		node.style.left=searchLeftPos+'px';
		node.style.top=(findPosY(obj)+height)+'px';
		node.style.visibility='visible';
		url = url+'?term='+escape(obj.value);
		getSeachResult(url);
	} else {
		node.style.visibility='hidden';
	}
}


function showResultBox2(obj, id ,url,boxLengthId) {
	var node = document.getElementById('search_result_box');
	if(trim(obj.value) != '') {
		var searchBoxLength = document.getElementById(boxLengthId);
		var searchLeftPos = findPosX(obj);
		var searchBoxLengthLeftPos = findPosX(searchBoxLength);
		node.style.width=(searchBoxLengthLeftPos - searchLeftPos) + 'px';
		node.style.left=searchLeftPos+'px';
		node.style.top=(findPosY(obj)+36)+'px';
		node.style.visibility='visible';
		url = url+'?term='+escape(obj.value)+'&productId='+id;
		getSeachResult(url);
	} else {
		node.style.visibility='hidden';
	}
}

function trim(str) {
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

var xmlHttpSearch;
var xmlHttp;

function getSeachResult(url) {
	if(xmlHttpSearch != null && xmlHttpSearch.readyState != 4) {
		xmlHttpSearch.abort();
	}
	xmlHttpSearch=GetXmlHttpObject(captureSearchResult);
	url = url+"&timestamp="+escape(""+(new Date()).toGMTString());
	if(xmlHttpSearch != null) {
		xmlHttpSearch.open("GET", url , true);
		xmlHttpSearch.send(null);
	} 
	return false;
}

function GetXmlHttpObject(handler) { 
	var objXmlHttp=null;
	if (navigator.userAgent.indexOf("MSIE")>=0) {  
       	try {
        	objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			objXmlHttp.onreadystatechange=handler; 
			return objXmlHttp;
      	} catch(e) {
        	try {
          		objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				objXmlHttp.onreadystatechange=handler; 
				return objXmlHttp;
        	} catch(e) {
				alert("Error. Scripting for ActiveX might be disabled"); 
          		return null;
        	}
		}
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0) {
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler;
		return objXmlHttp;
	}
} 

function captureSearchResult() { 
	if (xmlHttpSearch.readyState==4 || xmlHttpSearch.readyState=="complete") { 
		document.getElementById('search_result_box').innerHTML=xmlHttpSearch.responseText;
	} 
} 

function displayMore(id,img) {
	var div = parent.document.createElement('DIV');
	div.id='content'+id;
	div.style.cssText='position:absolute;background-color:#5C401D;border:1px solid black;overflow:hidden;'
	
	var html='<table width="450" border="0" cellspacing="0" cellpadding="0"><tr><td width="449" bgcolor="#525151" style="padding:5px;color:white;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" height="20" align="center">Large Image</td><td width="1" bgcolor="#525151" valign="top" align="right"><a href="#" onclick="closeMore('+id+'); return false;"><img src="/images/close.gif" border="0"></a></td></tr><tr><td colspan="2" style="padding:5px;font-family:Arial, Helvetica, sans-serif;font-size:12px;" bgcolor="white" height="155" valign="top" align="left"><img src="'+img+'"></td></tr></table>';

	div.innerHTML=html;

	var position = parent.document.getElementById('thumbnail');
	var x = findPosX(position);
	var y = findPosY(position);
	div.style.left=x+'px';
	div.style.top=y+'px';
	document.body.appendChild(div);
	return false;
}

function closeMore(id) {
	document.body.removeChild(document.getElementById('content'+id));
	return false;
}



function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);  
	return pattern.test(emailAddress);
  
}

$(document).ready(function(){
	//validate conversation.php	
	$("#btnSUBMIT").click(function() {
		
		var txt = '';							
			if($('#companyName').val() == ''){
                txt += 'Please input company name. \n';
            }			
			if($('#attn').val() == ''){
                txt += 'Please input your ATTN. \n';
            }
			if($('#address').val() == ''){
                txt += 'Please input your address. \n';
            }					
			if($('#city').val() == ''){
                txt += 'Please input your city. \n';
            }
			if($('#state').val() == ''){
                txt += 'Please input your state. \n';
            }
			if($('#zip').val() == ''){
                txt += 'Please input your zip. \n';
            }
			if($('#phone').val() == ''){
                txt += 'Please input your phone. \n';
            }
			if($('#email').val() == '' || !isValidEmail($('#email').val())){
                txt += 'Please input your email. \n';
            }
			if($('#qty1').val() == ''){
                txt += 'Please input qty 1. \n';
            }
			if($('#qty2').val() == ''){
                txt += 'Please input qty 2. \n';
            }
			if($('#qty3').val() == ''){
                txt += 'Please input qty 3. \n';
            }
			
			if(txt != '') {
                alert(txt);
                return false;
            }
            
            return true;				
    });	// end validate
	
});

