/* Text changer - light version.
Let your text's font size customizable.
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/text-changer
                v0.2 - May 18, 2006
*/
window.onload = function(){
	textChanger.init(); 
}

var textChanger = {
	cpanel : 'textchanger',  //set here the id of the element (div, p) within you want to insert the control panel
	element : 'pagewidth',    //set here the id of the element (div, p) within you want to change the text
	defaultFS : 1,         //set here the default font size in 'em'
	init: function() {
		var cpel = document.getElementById(textChanger.cpanel);
		var el = document.getElementById(textChanger.element);
		if (cpel == null || el == null) {alert('The elements with the \"'+textChanger.cpanel+'\" and/or \"'+textChanger.element+'\" ID do not exist in HTML source.');} else {
		var u = document.createElement('ul');
		cpel.appendChild(u);
		u.innerHTML = 
		'<li id="decrease"><a href="#" title="Decrease font size">A</a></li>'+
		'<li id="increase"><a href="#" title="Increase font size">A</a></li>'
		var sz = textChanger.getCookie();
		el.style.fontSize = sz ? sz + 'em' : textChanger.defaultFS + 'em';
		var incr = document.getElementById('increase');
		incr.onclick = function(){textChanger.changeSize(1); return false;};
		var decr = document.getElementById('decrease');
		decr.onclick = function(){textChanger.changeSize(-1); return false;};
		}
	} ,

	changeSize: function(val) {
		var el = document.getElementById(textChanger.element);
		var size = el.style.fontSize.substring(0,3);
		var fSize = parseFloat(size,10);
		if (val == 1) {
			fSize += 0.11;
			if (fSize > 2.0) fSize = 2.0;
		}
		if (val == -1) {
			fSize -= 0.11;
			if (fSize < 0.5) fSize = 0.5;
		}
		if (val == 0) {
			fSize = 1;
		}
		el.style.fontSize = fSize + 'em';
		textChanger.updateCookie(fSize);
		} ,
		
	updateCookie: function(vl) {
		var today = new Date();
		var exp = new Date(today.getTime() + (365*24*60*60*1000)); //the cookie will expire in one year  
		document.cookie = 'textChangerL=size=' + vl + ';' +'expires=' + exp.toGMTString() + ';' +'path=/';
	} ,

	getCookie: function() { 
		var cname = 'textChangerL=size=';
		var start = document.cookie.indexOf(cname);
		var len = start + cname.length;
		if ((!start) && (cname != document.cookie.substring(0,cname.length))) {return null;}
		if (start == -1) return null;
		var end = document.cookie.indexOf(";",len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));
	}
}






/*************************************************************************************************************/
/* new sub account form stuff
/* didn't want to create a new file for this stuff so that's why it's here
/*************************************************************************************************************/
function toggleProfileFormState() {
    var customerType = $('customerType').value;
    
    var isPet = customerType.search('Pet') >= 0;
    var isDependent = customerType.search('Dependent') >= 0;
    
    if (isPet) {
        $('name_label').innerHTML = 'Name&nbsp;<span class = "reqLabel">*</span>';
        
        // hide the last name element
        $('lastnameBlock').hide();
        
        // hide height, weight, email, area code, phone, and child resistant packaging elements
        $('emailBlock').hide();
        $('weightBlock').hide();
        $('heightBlock').hide();
        $('areaCodeBlock').hide();
        $('phoneBlock').hide();
        $('crpBlock').hide();
    }
    
    else if (isDependent) {
        // show the last name element
        $('name_label').innerHTML = 'First Name&nbsp;<span class = "reqLabel">*</span>';
        $('lastnameBlock').show();
        
        // hide email
        $('emailBlock').hide();
        
        // show height, weight, area code, phone, and child resistant packaging elements
        $('weightBlock').show();
        $('heightBlock').show();
        $('areaCodeBlock').show();
        $('phoneBlock').show();
        $('crpBlock').show();
    }
    
    else {
        // show the last name element
        $('name_label').innerHTML = 'First Name&nbsp;<span class = "reqLabel">*</span>';
        $('lastnameBlock').show();
        
        // show email, height, weight, area code, phone, and child resistant packaging elements
        $('emailBlock').show();
        $('weightBlock').show();
        $('heightBlock').show();
        $('areaCodeBlock').show();
        $('phoneBlock').show();
        $('crpBlock').show();
    }
}




/*************************************************************************************************************/
/* login email check
/* didn't want to create a new file for this stuff so that's why it's here
/*************************************************************************************************************/
function check_login_email() {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var email = $('username').value;
   
        
   if(reg.test(email) == false) {
        $('invalidEmailBlock').show();
        return false;
   }
   
   else
        return true;
}

function check_login_email_main() {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var email = $('username_main').value;
        
   if(reg.test(email) == false) {
        $('invalidEmailBlockMain').show();
        return false;
   }
   
   else
        return true;
}
    
    
function toggleAreaCode(areaCode) {
    var countryValue = $('country').value;
    var isIntl = countryValue != 'CAN' && countryValue != 'USA';
    
    //var areaCode = $('areacode').value;
    
    if (isIntl)
    {
       $('areaCodeBlock').hide();
       $('areacode').value = '000';
    }
    
    else
    {
       $('areaCodeBlock').show();
       $('areacode').value = areaCode == '000' ? '' : areaCode;
    }
}


function setSelectElement(id, val) {
    try {
        var pop = $(id);
        var type = pop.type;
    } catch (err) {
        return false;       
    }
    if(val.length > 0) {
        if(pop.type.indexOf('select') > -1) {
            var found = false;
            for (i=0;i < pop.options.length && !found; i++) {
                var selectValue = pop.options[i].value;
                if (!isNaN(val)) {//if(isFinite(val)) { 
                    selectValue = parseInt(selectValue);
                    val = parseInt(val);
                }
                
                if(pop.options[i].value.indexOf(val) > -1) {
                    pop.selectedIndex = i;
                    found = true;
                }
            }
        } else {
            throw("Attempting to setSelect a non-select element ("+pop.id+").");
        }
            
    } else {
        pop.selectedIndex = 0;
    }
    
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        pop.fireEvent('onchange',evt);
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent('change', true, false ); // event type,bubbling,cancelable
        pop.dispatchEvent(evt); 
    }
    //var evt = document.createEvent("HTMLEvents");
    //evt.initEvent('change',true,false);
    //pop.dispatchEvent(evt);   
    
}

    
function updateProvinceElement(e,id,prvValue) {
    var cnt = $(id + 'Container');
    cnt.innerHTML = '';

    var url = '/_fetch_provinces.php';
    var options = { method: 'get',
        parameters: $H({country: $(e).getValue(), id: id}),
        onSuccess: function(obj) {
            new Insertion.Top(cnt,obj.responseText);
            if($(id).type.indexOf('select') > -1) {
                setSelectElement(id,prvValue);
            } else {
                $(id).value = prvValue;
            }
        }.bind(this)
    }; 

    var ajax = new Ajax.Request(url,options);
}


function redirect(url) {
    location.href = url;
}
