﻿var imgCount = 0, iCurrImg, evt;
var imageContainer = "ctl00_mainPL_images";
var imagePrefix = "ssimg";

window.onload = function()
{				
	ApplySifrHeadings();
	
	if (window.attachEvent) // for IE 
	{	    
		sfHover();
	}
	
	if(document.getElementById(imageContainer))
	{
		var images = document.getElementById(imageContainer).getElementsByTagName("img");
		
		// Get the total number of images
		imgCount = images.length;
		
		// iCurrentImag is the image currently being displayed, as a default, set it to the first image
		iCurrImg = imgCount;
		
		if(images.length > 1)
		{			
			showAnimation();
		}
		else
		{
			clearTimeout(evt);
		}
	}
}

function ApplySifrHeadings()
{
	if(typeof sIFR == "function")
	{
		sIFR.bHideBrowserText = true;
		sIFR.setup();
		//sIFR.debug();
		sIFR();				
		sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/flash/GillSans.swf", sColor:"#000000", sWmode:"transparent"}));				
		sIFR.replaceElement(named({sSelector:"h2,#ctl00_mainPL_hppagecontent #ctl00_mainPL_maintext", sFlashSrc:"/flash/GillSans.swf", sColor:"#653559", sWmode:"transparent"}));		
	};	
}

function showAnimation()
{	
	var sImg1, sImg2;
	
	// if the current image is lower than the last image then we've gone too far, we need to loop back to the first image
	if(iCurrImg < 1)
	{
		iCurrImg = imgCount;
	}	
	
	// set the current image  (the one we're about to fade out)
	sImg1 = document.getElementById(imagePrefix + iCurrImg);
	
	
	// set the new image (the one we're about to fade in)
	if (document.getElementById(imagePrefix + (iCurrImg-1)))
	{
		sImg2 = document.getElementById(imagePrefix + (iCurrImg-1));
	}
	else
	{
		// if we get to the first image, go back to the first one
		sImg2 = document.getElementById(imagePrefix + imgCount);
	}
	
	// fade the images
	Effect.Fade(sImg1, { duration: 7.0});
	Effect.Appear(sImg2, { duration: 7.0});
		
	iCurrImg -= 1;		
			
	evt = setTimeout("showAnimation()", 7000);
}

function changeImg(img,url)
{
	clearTimeout(evt);
	
	var thisImg = document.getElementById(imagePrefix  + iCurrImg);

	// set the new image (the one we're about to fade in)
	if (document.getElementById(imagePrefix + (iCurrImg+1)))
	{
		sImg2 = document.getElementById(imagePrefix + (iCurrImg+1));
		iCurrImg++;
	}
	else
	{
	// if we get to the first image, go back to the first one
		sImg2 = document.getElementById(imagePrefix + 1);
		iCurrImg = 1;
	}
	

	
	if (thisImg.src.match(url + '$') == null)	// If this is a new image then fade the old one out and fade the new one in
	{
		sImg2.src = url;
		Effect.Fade(thisImg, { duration: 1.0});
		Effect.Appear(sImg2, { duration: 1.0});
	}
	
	thisImg = newImg;
}

function pauseAnimation()
{

}

function unpauseAnimation()
{

}

function checkanswer(sFormName, sAction)
{
    var theForm = document.forms[sFormName];
    var checked = false;
    
    for (i=0; i < theForm.length; i++)
	{
		if (theForm.elements[i].checked)
		{		
		    checked = true;
		    break;
        } 
	}
	
    if (checked)
    {
        checkrequired( sFormName, sAction);
    }
    else
    {
        alert("Please specify your answer to the question.");
    }
}
 
function checkrequired( sFormName)
{
	var pass=0, box=0, theForm = document.forms[sFormName];	
	
	for (i=0; i < theForm.length; i++)
	{
		var tempobj=theForm.elements[i];
		if (tempobj.name.substring(0,1) == "*")
		{
			if (((tempobj.type=="text"||tempobj.type=="textarea"||tempobj.type=="hidden")&&
			    tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
			    tempobj.selectedIndex==0))
	        {
				pass=1;
				break;
            }
        } 
	}	
	if (pass==1) {
	    tempobj.focus();
		shortFieldName=tempobj.name.substring(1,30).toUpperCase();
		alert("Please make sure the "+shortFieldName+" field is properly filled in.");
		return false;
	} else {	    
	    return true;
	}	
}

function checkrequired2( sFormName, action)
{
	var pass=0, box=0, theForm = document.forms[sFormName];	
	
	for (i=0; i < theForm.length; i++)
	{
		var tempobj=theForm.elements[i];
		if (tempobj.name.substring(0,1) == "*")
		{
			if (((tempobj.type=="text"||tempobj.type=="textarea"||tempobj.type=="hidden")&&
			    tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
			    tempobj.selectedIndex==0))
	        {
				pass=1;
				break;
            }
        } 
	}	
	if (pass==1) {
	    tempobj.focus();
		shortFieldName=tempobj.name.substring(1,30).toUpperCase();
		alert("Please make sure the "+shortFieldName+" field is properly filled in.");		
	} else {	    
	    //theForm.action = action;
	    //theForm.submit();
	}	
}

function emailCheck(thisControl)
{
	var emailStr = thisControl.value;
	
	if (emailStr != "") {
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null)
		{
			alert("Email address seems incorrect (check @ and .'s)");
			thisControl.select();
	  	    thisControl.focus();
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];
	
		if (user.match(userPat)==null)
		{
			alert("The username doesn't seem to be valid.");
			thisControl.select();
	  	    	thisControl.focus();
			return false;
		}
	
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null)
		{
			for (var i=1;i<=4;i++)
			{
				if (IPArray[i]>255)
				{
					alert("Destination IP address is invalid.");
					thisControl.select();
	  	            thisControl.focus();
			        return false;
				}
			}
			return true
		}
	
		var domainArray=domain.match(domainPat);
		if (domainArray==null)
		{
			alert("The domain name doesn't seem to be valid.");
			thisControl.select();
	  	    thisControl.focus();
			return false;
		}
	
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
		{
			alert("The address must end in a three-letter domain, or two letter country.");
			thisControl.select();
	  	    thisControl.focus();
			return false;
		}
	
		if (len<2)
		{			
			alert("This address is missing a hostname.");
			thisControl.select();
	  	    thisControl.focus();
			return false;
		}
		return true;
	} else {
		return false;
	}
}

sfHover = function()
{
    if (document.getElementById("ctl00_mainPL_options"))
    {	
	    var sfEls = document.getElementById("ctl00_mainPL_options").getElementsByTagName("li");
    	
	    if (sfEls)
        {
	        for (var i=0; i<sfEls.length; i++) {	    
		        sfEls[i].onmouseover=function() {
			        this.className+=" sfhover";
		        }
		        sfEls[i].onmouseout=function() {
			        this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		        }
	        }
	    }
    }
}

// function to only allow numeric key presses in a textbox
// this doesn't stop pasting of non numeric values
function numeric_only( e )
{
   // deal with unicode character sets
   var unicode = e.charCode ? e.charCode : e.keyCode;

   // if the key is backspace, tab, or numeric
   if( unicode == 8 || unicode == 9 || ( unicode >= 48 && unicode <= 57 ) )
   {
      // we allow the key press
      return true;
   }
   else
   {
      // otherwise we don't
      return false;
   }
}

function btnSaveOrderInfo()
{
   var theForm = document.forms['OrderInfo'];
   theForm.submit();
}
