/**
 * @author Netcraft.co.il
 */
try
{
var dom = YAHOO.util.Dom;
var ev = YAHOO.util.Event;
var get = dom.get;
}
catch(e){}

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){
	try{
		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="img/blank.gif";
			}
		}
	}
	catch(e){
	
	}
}

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"

}

var oBookWin = null
function SelectBooks(iGuroRef, iUserID, strUGIUD){
	var blnOpenWin = false;
	//alert('iGuroRef = ' + iGuroRef)
	//alert('iUserID = ' + iUserID)
	//oWin = window.open('SelectBooks.asp','BookWin','height=600,width=400,top=200,left=200,status=no,toolbar=no,menubar=no,location=no,resizeable=no')
	//alert(oBookWin)

	if (oBookWin == null)
		blnOpenWin = true
	else if (oBookWin.closed)
		blnOpenWin = true
	
	if (blnOpenWin == true)
		oBookWin = window.open('','BookWin','height=600,width=400,top=200,left=200,status=no,toolbar=no,menubar=no,location=no,resizeable=no')

	document.Books_form.UGID.value = strUGIUD
	document.Books_form.GID.value = iGuroRef
	document.Books_form.UserID.value = iUserID
	document.Books_form.submit();
}


function getCookie(c_name){ //get the value of the version from the cookie
    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){ //Creates a cookie
    var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    GotoLandPage(value);
}

function checkCookie(){ //Checks for Cookie
    var BKIversion=getCookie('BKIversion');
    if (BKIversion!=null && BKIversion!=""){
        GotoLandPage(BKIversion);
    }
    else{
		if (navigator.cookieEnabled){ 
        setCookie('BKIversion',generateRandom(),365);
        }
        else{
			window.location.href="homepage1.asp"
		}
			
    }

}

function generateRandom(){ //creates a random number according to number of versions
    var rand_no = Math.random();
    rand_no = rand_no * 2;
    rand_no = Math.ceil(rand_no);
    return rand_no;
}

function GotoLandPage(num){ //Loads the main landing page
    switch(parseInt(num)){
        case 1:
        window.location.href="homepage1.asp"
        break;
        
        case 2: 
        window.location.href= "homepage2.asp"
        break; 
        
        default:
        window.location.href="homepage1.asp"
        break;
        
    }
}