/**
 * @author Netcraft.co.il
 */
var dom = YAHOO.util.Dom;
var ev = YAHOO.util.Event;
var get = dom.get;
var so1;
var video_links = [];

var Browser = {
	Version: function()
    {  
		var version = 999; // we assume a sane browser
		if (navigator.appVersion.indexOf("MSIE") != -1)  
		// bah, IE again, lets downgrade version number 
		version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		return version;
    }
}



//if (Browser.Version() < 7){

//	alert("The Work on the Web requires the installation of Internet Explorer 7.\nPlease download it from Microsoft center as in the internet page below");
//	window.location='http://www.microsoft.com/windows/downloads/ie/getitnow.mspx';
//} 


function fixPngTransparency(obj){
	if(document.body.filters){
		pattern=/.png/gi;
		filterFix="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+obj.src+"',sizingMethod='scale');";
		if(obj.src.search(pattern)!=-1){
			obj.style.width=obj.offsetWidth+"px";
			obj.style.height=obj.offsetHeight+"px";
			obj.style.filter=filterFix;
			obj.src="gfx/blank.gif";
		}
	}
}

function fixImages(){
        imageCollection=document.body.getElementsByTagName("IMG");
        for(i=0; i<imageCollection.length; i++){
               fixPngTransparency(imageCollection[i]);
        }
}


/*function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) ) 
   {
	 if (document.body.filters){
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
         }
      }
   }    
}*/

// inintialize video list on home page
function initVideos() {

    video_links = get('video_list').getElementsByTagName('a');
    
    ev.addListener(video_links, 'click', function(){
		swapVideo(this,true);
	});
    
}

//load first video in list
function initFirstVideo() {
	swapVideo(video_links[0],false);
}

// swap youtube videos
function swapVideo(videoLink,isAutoplay){

    for (i in video_links) {
        dom.removeClass(video_links[i].parentNode, 'selected');
    }
    
    dom.addClass(videoLink.parentNode, 'selected');
    
    var video = videoLink.href.replace('http://www.youtube.com/watch?v=', '');
	
	var autoplay = (isAutoplay) ? 1 : 0;

    var source = 'http://www.youtube.com/v/' + video + '&hl=en&autoplay='+autoplay;
    
    get('video').innerHTML = "";
    so1 = new SWFObject(source, "video_embed", "480", "410", "8", "#fff");
    so1.write('video');
    
}

// swap registration: new user/registered
function initRegRadios() {
	 var radios = YAHOO.util.Event.on(dom.getElementsByClassName('radio', 'input', 'user_form'), 'click', function(){
        if (dom.hasClass('user_form', 'new_user')) {
            dom.replaceClass('user_form', 'new_user', 'reg_user');
        }
        else {
            dom.replaceClass('user_form', 'reg_user', 'new_user');
        }
    });
}

//init fields with preset text on registration
function initCardNameFields() {
	var container = dom.getElementsByClassName('card_name','div','user_form')[0];
	var lastNameField = dom.getElementsByClassName('last','input',container);
	var firstNameField = dom.getElementsByClassName('first','input',container);
	
	ev.addListener(lastNameField, 'focus', function(){
		clearInputText('Last name',this)
	});
	
	ev.addListener(lastNameField, 'blur', function(){
		restoreInputText('Last name',this)
	});
	
	ev.addListener(firstNameField, 'focus', function(){
		clearInputText('First name',this)
	});
	
	ev.addListener(firstNameField, 'blur', function(){
		restoreInputText('First name',this)
	});
}

//clear preset text on inputs
function clearInputText(textString,element) {  
    if (element.value == textString) {
        element.value = '';
    } 
}

//restore preset text on inputs
function restoreInputText(textString,element)  {
    if (element.value == '') {
        element.value = textString;
    } 
}

//init card number fields
function initCardNumberFields() {
	var container = dom.getElementsByClassName('card_number','div','user_form')[0];
	var inputFields = container.getElementsByTagName('input');
	
	ev.addListener(inputFields, 'keyup', function(event){
		return autoTab(this, 4, event);
	});
}

//auto tab jumper
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input, len, e){
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0, 8, 9] : [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46];
    if (input.value.length >= len && !containsElement(filter, keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input) + 1) % input.form.length].focus();
    }
    
    function containsElement(arr, ele){
        var found = false, index = 0;
        while (!found && index < arr.length) 
            if (arr[index] == ele) 
                found = true;
            else 
                index++;
        return found;
    }
    
    function getIndex(input){
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1) 
            if (input.form[i] == input) 
                index = i;
            else 
                i++;
        return index;
    }
    return true;
}


function trim(sValue)
{
    var trm = ""+sValue;
    trm = trm.replace(/(^\s*)|(\s*$)/g, "");
    return trm;
}

function Is_Number(intNum){
	if(isNaN(intNum) || intNum < 0)
		return false
			
	return true
}
			
function Is_Email(strValue) {
	var objRegExp  = /(^[A-Za-z0-9]([A-Za-z0-9_\.\-]*)@([A-Za-z0-9_\.\-]*)([.][a-z]{3})$)|(^[A-Za-z0-9]([A-Za-z0-9_\.\-]*)@([A-Za-z0-9_\.\-]*)(\.[a-z]{2})(\.[a-z]{2})*$)/i;
    return objRegExp.test(strValue);
}

function checkFields(){
	oObj = document.getElementById("user_name")
	if (oObj.value == ""){
		alert("You must fill in a user name")
		oObj.focus();
		return false;
	}
						
	oObj = document.getElementById("user_name")
	if (Is_Email(oObj.value) == false){
		alert("Invalid input in username field.\n try again using an Email address. ")
		oObj.focus();
		return false;
	}

	oObj = document.getElementById("password")
	if (trim(oObj.value) == ""){
		alert("You must fill in a password")
		oObj.focus();
		return false;
	}
				
	return true
}
function openCiLink() //Flash onClick function
{
    window.location = "register.asp"

}

function ajaxObject(url, callbackFunction) {  
    var that=this;        
    this.updating = false;  
    this.abort = function() {    
        if (that.updating) {      
        that.updating=false;     
        that.AJAX.abort();      
        that.AJAX=null;   
        }
    }  
    this.update = function(passData,postMethod) {  
        if (that.updating) { return false; }    
        that.AJAX = null;         
    if (window.XMLHttpRequest) {           
        that.AJAX=new XMLHttpRequest();               
    }   else {  
        that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    } 
    if (that.AJAX==null) {
        return false;    
    }   else {      
        that.AJAX.onreadystatechange = function() {       
            if (that.AJAX.readyState==4) {                 
            that.updating=false;                    
            that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML); 
            that.AJAX=null;                                            
            }                                      
        }                                                      
    that.updating = new Date();                           
    if (/post/i.test(postMethod)) {   
        var uri=urlCall+'?'+that.updating.getTime();    
        that.AJAX.open("POST", uri, true);     
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
        that.AJAX.setRequestHeader("Content-Length", passData.length);     
        that.AJAX.send(passData);     
    }   else {      
            var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
            that.AJAX.open("GET", uri, true);                               
            that.AJAX.send(null);  
    }           
    return true;                  
  }  
} 
    var urlCall = url;        
    this.callback = callbackFunction || function () { };
}