// JavaScript Document
function isBlank(str)
{
	if(str.length == 0)
	{
		return true
	}
	else
	{
		return false
	}
}


function isCCExp(str)
{
	var status = true ;
	
	if(str.length == 0)
	{
		status = false
	}
	
	//Break into array
	var expArray = str.split('/')
	
	//If there are not 2 elements, then no, this is not a proper expiration date
	if(expArray.length != 2)
	{
		status = false
	}
	else
	{
		
		if(!isNumeric(expArray[0]) || expArray[0].length != 2)
		{
			status = false
		}
		
		if(!isNumeric(expArray[1])  || expArray[1].length != 2)
		{
			status = false
		}	
	}
	
	return status 
}



function isCCNum(str){
	
	var status = true;
	
	//Must be 15-16 characters and all numbers
	if(str.length > 16){
		status = false
	}
	
	if(str.length < 15){
		status = false	
	}
	
	if(!isNumeric(str)){
		status = false			  
	}
	
	return status;
}



function isPhoneNumber(str){
 
 	if(!isNumeric(str) || str.length != 10){
		return false
	}
	else
	{
		return true
	}

}//end function

function isNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (q = 0; q < strString.length && blnResult == true; q++)
      {
      strChar = strString.charAt(q);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
 }

function isZip(zipData){
	
	if(!isNumeric(zipData) || zipData.length != 5){
		return false;	
	}
	else
	{
		return true;	
	}
	
		
}//end function




//Determine the length of a string for min an d max values
function checkStrLength(str, minLength, maxLength)
{		
	//alert("checkStrLength")
	var status = true
	var length = str.length
	
	if(minLength != ''){
		//Check that there are aleast a min amount of characters
		if(length < minLength){
			//There is not enough characters
			status = false
		}//end inner if
	}//end outer if
	
	
	if(maxLength != ''){
		//Check that there are aleast a min amount of characters
		if(length > maxLength){
			//There are too many characters
			status = false
		}//end inner if
	}//end outer if
	

	
	//Send back the status of the function
	alert(status)
	return status

}//end function




//Check to see if a set of radio elements have been checked
function isRadioChecked(elementName, numElements){
		
		//alert('IsRadioChecked');
		
		var radioStatus = false
		
		for(i=0;i<numElements;i++){
				//alert(elementName+"_"+i)
				
				if(document.getElementById(elementName+""+i).checked == true){
						radioStatus = true
						i = numElements
				}//end if
				
		}//end loop
		
		
		return radioStatus

}//end function
				
	
	
//BatchId grouping
function batchIds(element, container){

	var mylist = $(container).value;
	
	var myarray = mylist.split(",");
	
	if(element.checked == true){
		myarray.push(element.value);
	}
	else{
		//alert("Remove from the array");
		for(i=0;i<myarray.length;i++){
			if(myarray[i] == element.value){
				myarray.splice(i,1);
			}//end if
		}//end loop
	}//end else
	
	$(container).value = myarray;

}//end function




//Graying out the screen
function grayOut(vis, options, message) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}
	
	
	
	
	
//Email formatted string validation
function isEmail(indirizzo) {
  if (window.RegExp) {
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
      return true;
    return false;
    }
  else {
    if(indirizzo.indexOf("@") >= 0)
      return true;
    return false;
     }
}




//Clear the contents on an element
function clearBox(element){
	element.value = ''
}//end function





function trim(str) {
	var trimmed = str.replace(/^\s+|\s+$/g, '') ;
	return trimmed
}




//Shortcut function to accession HTML DOM properties
function $(name){
	return document.getElementById(name)	
}

//Shortcut to a flash object
function $flash(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
	
	//alert(document[movieName])
}


function varDump(expression){
	
	//Loop through object properties and 
	for(i in expression){
		
		if(typeof(expression[i]) == 'object'){
			varDump(expression[i])
		}
		else
		{
			document.write(i +":->"+ expression[i] + "<BR>")
		}//end if-else
		
	}//end loop
	
	//Create spacer line
	document.write('<BR>')
}//end function




//Return the current time as an object
function getTimeObject(){
	
	var date = new Date()
	var obj = new Object()

	obj['year'] = date.getFullYear()
	obj['month'] =  date.getMonth() + 1
	obj['day'] =  date.getDate()
	obj['hour'] =  date.getHours() 
	obj['minute'] =  date.getMinutes()
	obj['second'] =  date.getSeconds()
	
	if(obj.hour == 0){
		obj['hour'] = '00'
	}

	//Cycle through object and look for single charagter strings
	for(i in obj){
		var term = new String(obj[i])
		if(term.length == 1){
			obj[i] = '0'+term
		}//end if
	}//end loop


	//Return the obj
	return obj

}

function highlight(element, color){	
	element.style.background = color
}

function setBackground(element, color){
	element.style.backgroundColor = color
}


function toggle(id){

	if($(id).style.display == 'none'){
		$(id).style.display = ''
		return true;
	}
	else
	{
		$(id).style.display = 'none';
		return false;
	}
		
}



function getUrl(url){
	if(trim(url) != ''){
		window.location.href = url	
	}
}




function stringToXML(xmlString){
try //Internet Explorer
  {
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(xmlString);
  var xml = xmlDoc
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    var xml = (new DOMParser()).parseFromString(xmlString, "text/xml");
	}
  catch(e) {alert(e.message)}
  }

	//Return the XML object
	return xml
}



function hasNumbers(t)
{
return /\d/.test(t);
}




function toggleGroup(param){
	//Break param into an array
	toggleArray = param.split(',')
	
	return toggleArray
}

function toggleElement(id){
	try{
		for(i=0; i< toggleArray.length; i++){
			if(id == toggleArray[i]){
				$(toggleArray[i]).style.display = ''	
			}
			else
			{
				$(toggleArray[i]).style.display = 'none'	
			}//end if/else
		}//end loop
		
	}
	catch(e){
		alert("Error"+e)
	}

}//end function




function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}


function setCookie(c_name, value, expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


function urlParam(url, name)
{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec(url);
	  if( results == null )
		return "";
	  else
		return results[1];
}	


	
	function onLoadIframe(e, page){
			
		try{
			//Check to see if the url param 'redirect' is needed. if no, the
			//run the iframe check, if yes, then set the iframe to the 
			//specified parameters

			var redirect = urlParam(window.location.href, 'redirect')
			
			//Set the height and width of the iframe
			if(redirect == 'false'){
				
								
				e.src = page + '#' + window.location.href

				var w = urlParam(window.location.href, 'w')
				var h = urlParam(window.location.href, 'h')
				
				//Expand the iFrame to the needed height and width
				e.width = w 
				e.height = h
			}
			else
			{
				e.src = page + '#' + window.location.href
			}
			
		}
		catch(e){
			//alert(e)
			if(typeof(e) == 'object'){
				alert('Error:'+e.description)
			}
			else
			{
				alert('Error:'+e)	
			}
				
		}
	}
	
	
	//var myInt
	var attempts = 0
	var iFrame
	var attemptsMax = 5
	var myInt
	var iframeUrlCheckSpeed = 500
	
	
	
	function checkUrlFormIFrameParameters(){
		
		try{
			//Increment the attempts
			attempts++
			
			//If the max attempts have happened, then kill the interval
			if(attempts >= attemptsMax){ 
				myInt = clearInterval(myInt)	
			}
			
			//Extract the fragment data and convert to an object
			var fragObj = urlFragmentToObject(window.location.href)
			
			//Set the iFrame dimensions
			if(fragObj != -1){
				
				//Set the iframe dimensions
				iFrame.height =  fragObj['height']
				iFrame.width = fragObj['width']
				
				//Kill the interval
				myInt = clearInterval(myInt)
			}
		
		}
		catch(e){
			alert(e)	
		}
		
		
	}//end function
	
	
	
	
	function urlFragmentToObject(url){
		
		//Parse the url
		if(url.indexOf('#') > 0){
			
			myInt = clearInterval(myInt)	
			
			//The pound symbol exist, parse it
			var array = url.split('#')
			var paramsString = array[1]
			var paramsObj = new Object()
			
			//Break the params
			var paramsArray = array[1].split('&')
			
			//Cycle through the array and add to object
			for(i=0; i<paramsArray.length; i++){
				var miniArray = paramsArray[i].split('=')
				paramsObj[miniArray[0]] = miniArray[1] 
			}
			//Return the object
			return paramsObj
		}
		else
		{
			return false
		}
		
	}
	
	
	function startCheckUrlTimer(e, page){
		myInt = setInterval('checkUrlFormIFrameParameters()' , iframeUrlCheckSpeed)
		iFrame = e
		onLoadIframe(e, page)
	}
	
	
	
	
function sendIframeDataToParent(){
	
	try{
		//The append variable
		var append = true
		
		//Get the url url
		var url = window.location.href
		
		//Split into an array [0] = url of this page, [1] = url of parent page
		var urlArray = url.split('#')
		
		//Check the parent for fragment data
		var fragData = urlFragmentToObject(urlArray[1])
		
		
		//Look for the word 'redirect' in the parentUrl
		if(urlArray[1].match('redirect') == 'redirect'){
			append = false
		}
		
	
		//The redict varaible will initally be missing, if so then redirect with dimension data
		if(append){
			//Get the max possible scroll dimensions of this page
			var h = document.body.scrollHeight;
			var w = document.body.scrollWidth;
			window.parent.location.href = urlArray[1] + '#redirect=false&height='+h+'&width='+w
		}//end if
	
	}//end TRY
	catch(e){

			//alert(e)
			if(typeof(e) == 'object'){
				alert('Interal iFrame Content Error:'+e.description)
			}
			else
			{
				alert('Interal iFrame Content Error:'+e)	
			}

	}
	
	
}





  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 dollarFormat(amount) 
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
