// JavaScript Document

function loadImages()
{
  if (document.images)
   {	   
	 photo1  = new Image(120,32);
	 photo1.src = "images/photo1.jpg";
	 photo2  = new Image(120,32);
	 photo2.src = "images/photo2.jpg";
	 photo3  = new Image(120,32);
	 photo3.src = "images/photo3.jpg";	 
 	photo4  = new Image(120,32);
	 photo4.src = "images/photo4.jpg";	 
 	photo5  = new Image(120,32);
	 photo5.src = "images/photo5.jpg";	 	 
   }
}


//====================================
//Image swapper for frontpage
//var imageInterval	= setInterval("imageSwapper()",3000);
var imageArray		= new Array(5);
imageArray[0]		= "photo5.jpg";
imageArray[1]		= "photo2.jpg";
imageArray[2]		= "photo3.jpg";
imageArray[3]		= "photo1.jpg";
imageArray[4]		= "photo4.jpg";

currImage = 0;

function imageSwapper()
{
$('mainPic').src = "images/products/table1/" + imageArray[currImage];

var tempStr	= "thumb_"+currImage;

for(i=0;i<5;i++)
{
var tempStr1= "thumb_"+i;
$(tempStr1).className = "nothumb";
}


$(tempStr).className = "thumb";

//---------------------
//increment

	currImage++;
//---------------------
//reset
	if(currImage == 5)
	currImage = 0;
	
}

//===========================================
//product page image swapper
lastThumb = "undefined";
function swapProductImage(theImage,thumbID,prodID)
{	
	if(lastThumb != "undefined")
	$(lastThumb).className	= "nothumb";
	$(prodID).src			= theImage;
	$(thumbID).className	= "thumb2";
	lastThumb				= thumbID;
}


imageWindow = null;
function imagePop(width,height,title,imgsrc)
{
	
windowURL = "winpop.php?width="+width+"&height="+height+"&title="+title+"&imgsrc="+imgsrc;	
	
	if(imageWindow != null)
	{
	imageWindow.close();	
	imageWindow = window.open(windowURL,'_blank','width=50,height=50,scrollbars=no,status=no,titlebar=no');
	}	
	else
	imageWindow = window.open(windowURL,'_blank','width=50,height=50,scrollbars=no,status=no,titlebar=no');
}


theWindow = null;
function openWindow(windowURL)
{
	if(theWindow != null)
	{
	theWindow.close();	
	theWindow = window.open(windowURL,'_blank','width=150,height=177,scrollbars=no,status=no');
	}	
	else
	theWindow = window.open(windowURL,'_blank','width=150,height=177,scrollbars=no,status=no');
}

theWindow2 = null;
function openWindow2(windowURL)
{
	if(theWindow2 != null)
	{
	theWindow2.close();	
	theWindow2 = window.open(windowURL,'_blank','width=550,height=177,scrollbars=no,status=no');
	}	
	else
	theWindow2 = window.open(windowURL,'_blank','width=550,height=177,scrollbars=no,status=no');
}



function NoHTML(sText)
{
   var InvalidChars = "><";
   var HTMLcheck =true;
   var Char;

 
   for (i = 0; i < sText.length && HTMLcheck == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (InvalidChars.indexOf(Char) != -1) //if items are found 
         {
         HTMLcheck = false;
         }
      }
   return HTMLcheck;
}

function getURL()
{
test = location.href;

test= test.split("/");
str = "";
for(i=0;i<(test.length-1);i++)
{
	if(i == 0)
	str = test[i]+"/";
	else
	str = str + test[i] + "/";
}
return str;
}

function $(id) {
  return document.getElementById(id);
}


//==== Login form check function =====>
function logForm (form){

  if (form.login.value == ""){

    alert( "Please enter a vaild login." );
    logform.login.focus();
    return false;
  }
  
  if (form.pasword.value == ""){

    alert( "Please enter a vaild password." );
    logform.pasword.focus();
    return false;
  }
}

//==== Form check function =====>
function checkform (form){

  if (form.name.value == ""){

    alert( "Please enter your name." );
    form.name.focus();
    return false;
  }
  
  if (form.phone.value == ""){

    alert( "Please enter your phone number." );
    form.phone.focus();
    return false;
  }
  
  if (form.email.value != ""){

	temp = IsEmail(form.email.value);

	if(!temp)
	{
    alert( "Please enter a valid email." );
    form.email.focus();
    return false;		
	}
	
  }

  
  if (form.comments.value != ""){
	temp = NoHTML(form.comments.value);

	if(!temp)
	{
    alert( "< or > are invalid characters for comments." );
    form.comments.focus();
    return false;		
	}
	
  }
  
  
  populateKey(); 
   
  return true;
}

function populateKey()
{
$('key').value = getURL();	
}


//==== Email Check function =====>

function IsEmail(sText){

var whereisAT = sText.indexOf('@');

 if(whereisAT == -1){

    return false;
 }
   
 var whereisDOT = sText.indexOf('.');  

 if(whereisDOT == -1){

    return false;
 }
     
    return true;
}


//=======Prev/Next Links=============

function prevNextPage(direction)
{

//handles looping
if(lastPageSwapped == 1 && direction == "prev")
newIndex = 5;
else if(lastPageSwapped == 5 && direction == "next")
newIndex = 1;
else
{
	
	if(direction == "next")
	newIndex = lastPageSwapped+1;
	else if(direction == "prev")
	newIndex = lastPageSwapped-1;		
}

swapProdPage(newIndex);

}

//=======Product Page Swapper=========
lastPageSwapped = 1; //by default, page 1 is showing first
function swapProdPage(pageID)
//pre-condition: pageID is an integer which serves as the index of the page to swap
//post-condition: will hide existing page and reveal requested page
{
	
lastPageID		= "productpage_" + lastPageSwapped;
pageID1			= "productpage_" + pageID;

lastupdateLink	= "productpageLink_" + lastPageSwapped;
newupdateLink	= "productpageLink_" + pageID;

$(lastupdateLink).className	= "resLink";
$(newupdateLink).className		= "resLinkOver";

lastupdateLink	= "footer_productpageLink_" + lastPageSwapped;
newupdateLink	= "footer_productpageLink_" + pageID;

$(lastupdateLink).className	= "resLink";
$(newupdateLink).className		= "resLinkOver";


lastPageSwapped	= pageID;

$(lastPageID).style.display= 'none';

$(pageID1).style.display= 'block';
$('topofPage').focus();
window.scrollTo(0,0);
}

///==================setQuantity=================

function setQuantity(whichField,type)
{

pattern = /^\d+$/;
//input validation 
if($(whichField).value == "" ) 
$(whichField).value = 0;
else
{
if (pattern.test($(whichField).value)==false )
$(whichField).value = 0;
}


temp = $(whichField).value;

if(type == "plus")
temp++;
else
temp--;


if(temp < 0)
$(whichField).value = 0;
else
$(whichField).value = temp;



}


