//set the site url
var full_url = window.location.protocol + "//" + window.location.host;

// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006-08 www.cryer.co.uk
function IncludeJavaScript(jsFile)
{
    document.write('<script type="text/javascript" src="'
        + jsFile + '"></scr' + 'ipt>');
}

IncludeJavaScript(full_url+'/js/change_css.js');

// hides an element
function hide(id) {
    document.getElementById(id).style.display='none';
}
// shows an element
function show(id) {
    document.getElementById(id).style.display='';
}
function collapse(id)
{
    var divid = document.getElementById(id);
    if (divid.style.display=="none") {
        divid.style.display="block";
    }
    else {
        divid.style.display="none";
    }
}

function displayAll(show,hide,layer,upper)
{
    if (document.getElementById(show).style.display=="inline")
    {
        document.getElementById(layer).style.display='inline';
        document.getElementById(hide).style.display='inline';
        document.getElementById(show).style.display='none';
        if(upper!="")
            document.getElementById(upper).style.display='none';
    }
    else {
        document.getElementById(layer).style.display='none';
        document.getElementById(hide).style.display='none';
        document.getElementById(show).style.display='inline';
        if(upper!="")
            document.getElementById(upper).style.display='inline';
    }
}

function displayAll2(show,hide,layer)
{
    if (document.getElementById(show).style.display=="inline")
    {
        document.getElementById(layer).style.display='inline';
        document.getElementById(hide).style.display='inline';
        document.getElementById(show).style.display='none';
    }
    else {
        document.getElementById(layer).style.display='none';
        document.getElementById(hide).style.display='none';
        document.getElementById(show).style.display='inline';
    }
}
function popup(page)
{
    window.open(page);
}
 
function redirect(page)
{
    window.location = page;
}
 
function showtab(tname,cname)
{
    i=1;
    while (document.getElementById("t_"+i))
    {
        document.getElementById("t_"+i).style.display='none';
        document.getElementById("c_"+i).className='personal-tab';
        i++;
    }
    document.getElementById(tname).style.display='block';
    document.getElementById(cname).className='tabs-selected';
}

function showsubtab1(subt)
{
    i=1;
    while (document.getElementById("s1_"+i))
    {
        document.getElementById("s1_"+i).style.display='none';
        i++;
    }
    document.getElementById(subt).style.display='block';
}
function showsubtab2(subt)
{
    i=1;
    while (document.getElementById("s2_"+i))
    {
        document.getElementById("s2_"+i).style.display='none';
        i++;
    }
    document.getElementById(subt).style.display='block';
}	
		
function ShowHelp(field) {
    document.getElementById(field).style.display='block'
}
function hideHelp(field) {
    document.getElementById(field).style.display='none'
}	

function showview(subt)
{
    i=1;
    while (document.getElementById("view"+i))
    {
        document.getElementById("view"+i).style.display='none';
        i++;
    }
    document.getElementById(subt).style.display='block';
}
	
/*function showview(subt) {

	if (document.getElementById(subt).style.display=='none') {
	    document.getElementById(subt).style.display='block';			
	}
	else {
		document.getElementById(subt).style.display='none';
	}
}*/

function checkall(count,group,allch) {
    for(var i = 1; i < count; i++)
    {
	
        if (document.getElementById(allch).checked==true)
        {
            document.getElementById(group+i).checked=true;
        }
        else
        {
            document.getElementById(group+i).checked=false;
        }
    }
}	

function changevalue(drop,val)
{
    document.getElementById(drop).innerHTML=val
}
//function hide(h)
//	{
//		document.getElementById(h).style.display='none'
//	}
//function show(h)
//	{
//		document.getElementById(h).style.display='block';
//		document.getElementById(h).style.position='absolute';
//	}	
function confirm_click(message,url) {
    var answer = confirm(message);
    if(answer){
        window.location = url;
    }else{
        return false;
    }
}
function alert_click(message,url) {
    var answer = alert(message);
    window.location = url;
}
// function to format the numbers
function number_format(number, decimals, dec_point, thousands_sep)
{
    var exponent = "";
    var numberstr = number.toString ();
    var eindex = numberstr.indexOf ("e");
    if (eindex > -1)
    {
        exponent = numberstr.substring (eindex);
        number = parseFloat (numberstr.substring (0, eindex));
    }
  
    if (decimals != null)
    {
        var temp = Math.pow (10, decimals);
        number = Math.round (number * temp) / temp;
    }
    var sign = number < 0 ? "-" : "";
    var integer = (number > 0 ?
        Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
    var fractional = number.toString ().substring (integer.length + sign.length);
    dec_point = dec_point != null ? dec_point : ".";
    fractional = decimals != null && decimals > 0 || fractional.length > 1 ?
    (dec_point + fractional.substring (1)) : "";
    if (decimals != null && decimals > 0)
    {
        for (i = fractional.length - 1, z = decimals; i < z; ++i)
            fractional += "0";
    }
  
    thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ?
    thousands_sep : null;
    if (thousands_sep != null && thousands_sep != "")
    {
        for (i = integer.length - 3; i > 0; i -= 3)
            integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
    }
  
    return sign + integer + fractional + exponent;
}
function removeSpaces(string) {
    return string.split(' ').join('');
}
// function definition to calculate round figure of a number upto 2 decimal points
function roundNumber(Number) {
    var rnum = Number;
    var rlength = 2; // The number of decimal places to round to
    if (rnum > 8191 && rnum < 10485) {
        rnum = rnum-5000;
        var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
        newnumber = newnumber+5000;
    }
    else {
        var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
    }
    return newnumber;
}
// function check for price values
	
function IsPrice(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 (i = 0; i < strString.length && blnResult == true; i++)
    {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
        }
    }
    return blnResult;
}

function removeSpaces(string) {
    return string.split(' ').join('');
}
// Form validator
// check to see if input is whitespace only or empty
function isEmpty(val)
{
    if (val.match(/^s+$/) || val == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

// check to see if input is number
function isNumber(val)
{
    if (isNaN(val))
    {
        return false;
    }
    else
    {
        return true;
    }
}

// check to see if input is alphabetic
function isAlphabetic(val)
{
    if (val.match(/^[a-zA-Z]+$/))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// check to see if input is alphanumeric
function isAlphaNumeric(val)
{
    if (val.match(/^[a-zA-Z0-9]+$/))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// check to see if input is alphanumeric
function isURL(val)
{
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(val)) {
        return false;
    }
    else
    {
        return true;
    }
}
// check to see if value is within range
function isWithinRange(val, min, max)
{
    if (val >= min && val <= max)
    {
        return true;
    }
    else
    {
        return false;
    }
}

// check to see if input is a valid email address
function isEmailAddress(val)
{
    if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// check to see if form value is checked
function isChecked(obj)
{
    if (obj.checked)
    {
        return true;
    }
    else
    {
        return false;
    }
}
function passwordStrength(password)
{
    var desc = new Array();
    desc[0] = "6 characters or more (get tricky!)";
    desc[1] = "Too Short";
    desc[2] = "Weak";
    desc[3] = "Good";
    desc[4] = "Strong";
    desc[5] = "Very Strong";
	
    var clss = new Array();
    clss[100] = "info";
    clss[0] = "info";
    clss[1] = "info";
    clss[2] = "weak";
    clss[3] = "good";
    clss[4] = "strong";
    clss[5] = "verystrong";
	
    var score   = 0;

    if (password.length > 0 )  {
        score++;
        var e = 1;
    }

    //if password bigger than 6 give 1 point
    if (password.length > 5) score++;

    //if password has both lower and uppercase characters give 1 point
    if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

    //if password has at least one number give 1 point
    if (password.match(/\d+/)) score++;

    //if password has at least one special caracther give 1 point
    if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	score++;

    //if password bigger than 12 give another 1 point
    if (password.length > 5) score++;
			
    if (password.length < 6) {
        score = 1;
        var e = 1;
    }
	
    if (password.length == 0) {
        score = 0;
        var e = 1;
    }
	
    $('password_info').className = 'roundBorderTR roundBorderBR '+clss[score];
    $('password_info').innerHTML = desc[score];
    return score;
}

// Error report for ajax
function reportError(request) {
    $F('tix_type_'+tixId) = "Error";
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}
function checkAll(field) {
    for (i = 0; i < field.length; i++)
        field[i].checked = true ;
}

function uncheckAll(field) {
    for (i = 0; i < field.length; i++)
        field[i].checked = false ;
}
function $N(element)
{
    if (typeof element == 'string')
        element = document.getElementsByName(element);
    return Element.extend(element);
}

function filterCode(e)
{
    var keynum
    var keychar
    var numcheck
    if(window.event)
    {
        keynum=e.keyCode;
    }
    else if(e.which)
    {
        keynum=e.which;
    }
    if(keynum>=12)
    {
        keychar=String.fromCharCode(keynum);
        var exp=new RegExp("[a-zA-Z0-9\-_]","g");
        check=/\w/;
        return exp.test(keychar);
    }
    else
    {
        return true;
    }
}

// Clear value
function resetValue (item) {
    $(item).value = '';
}

// copy to clipboard
Object.extend(Form.Element.Methods,
{
    // Uses a combination of:
    // http://www.jeffothy.com/weblog/clipboard-copy/
    // http://www.rodsdot.com/ee/cross_browser_clipboard_copy_with_pop_over_message.asp
    copyToClipboard: function(element, swf)
    {
        if (Object.isUndefined(swf)) swf = '_clipboard.swf';
        if (window.clipboardData)
            window.clipboardData.setData("Text", element.getValue());
        else
        {
            if(!$('flash_clipboard_container'))
                $(document.body).insert(new Element('div', {
                    id: 'flash_clipboard_container'
                }));
 
            var content = encodeURIComponent(element.getValue());
 
            if (!(typeof SWFObject == "undefined"))
            {
                var so = new SWFObject(swf, 'copy_contents', '0', '0', '4');
                so.addVariable('clipboard', content);
                so.write('flash_clipboard_container');
            }
            else
            {
                $('flash_clipboard_container').update(
                    new Element('embed',
                    {
                        width: 0,
                        height: 0,
                        flashvars: 'clipboard=' + content,
                        quality: 'high',
                        name: 'copy_contents',
                        id: 'copy_contents',
                        src: swf,
                        type: 'application/x-shockwave-flash'
                    })
                    );
            }
        }
    }
});
 
Element.addMethods();
// Tool tip 
document.observe('dom:loaded', function() {
    $$('a[rel]').each(function(element) {
        new Tip(element, $(element.rel), {
            radius: 4,
            border:4,
            hook: {
                target: 'bottomMiddle',
                tip: 'topLeft'
            },
            stem: 'topLeft'
        });
    });
});
// Lightview loaded
document.observe('lightview:opened', function() {
    changecss('.prototip','position','fixed');
});

// Lightview closed
document.observe('lightview:hidden', function() {
    changecss('.prototip','position','absolute');
});
// Checkbox toggle
function checkbox_toggle (chkbox, dsp) {
    if( $(chkbox).checked == true ) {
        $(dsp).style.display = 'none';
    } else {
        $(dsp).style.display = '';
    }
}

// Display message on top of page
function dspMessage (message, clss) {
    if(message){
        Event.observe(window,'load',function() {
            $('header_message').className = clss;    
            $('header_message').innerHTML = message;
            Effect.Appear('header_message', {
                duration:0
            });
            Effect.Fade('header_message', {
                delay: 3.0
            }); 
        });       
    }
} 
// Get states/provinces
function states(field, state, req){
    if($(field+'_country') != null){
        if(state != '') s = state;
        else s = field;
        // Get the states based on selection
        var url = full_url+'/ajax/ajaxEventRegister.php';
        var params = 	'country='+encodeURIComponent($(field+'_country').value)+
        '&state='+encodeURIComponent($(s+'_state').value)+
        '&req='+encodeURIComponent(req)+
        '&f='+encodeURIComponent(field)+
        '&process=getStates';
					
        // Reset ends
        display = field+'_states';
        var ajax = new Ajax.Updater(
        {
            success: display
        },
        url,
        {
            //   onCreate: function() {
            //       $('comments_loader').style.display = ''; // show a div with loading
            //       },
            //   onComplete: function() {
            //       $('comments_loader').style.display = 'none';
            //       },
            method: 'get',
            parameters: params,
            onFailure: reportError,
            evalScripts: true
        });
 
        if($(field+'_country').value=='US'){
            $(field+'_zip_label').innerHTML = 'Zip Code';
        } else {
            $(field+'_zip_label').innerHTML = 'Post Code';
        }
    }
}
