﻿function resolveServerId(thisid)
{
    var str = '';
    var elem = document.getElementById('aspnetForm').elements;
    for(var i = 0; i < elem.length; i++)
    {
        var thisval = elem[i].id.toString();
        if(thisval.endsWith(thisid))
        {
           str = elem[i].id;
        }
    }
    return str;
}
function resolveServerSpanId(thisid)
{
    var str = '';
    var elem = document.getElementsByTagName('span');
    for(var i = 0; i < elem.length; i++)
    {
        var thisval = elem[i].id.toString();
        if(thisval.endsWith(thisid))
        {
           str = elem[i].id;
        }
    }
    return str;
}

String.prototype.endsWith = function(str)
{
    return (this.match(str+"$")==str)
}
function fillReglinkClicked()
{
    var va1 = document.getElementById('linkClicked');
    va1.value = "yes";
}
function checkRegBirthdayDDLs()
{
    var ddl1 = document.getElementById(resolveServerId('ddlRCDOBday'));
    var ddl2 = document.getElementById(resolveServerId('ddlRCDOBmonth'));
    var ddl3 = document.getElementById(resolveServerId('ddlRCDOByear'));
    var va1 = document.getElementById('valBirthday');
    var va2 = document.getElementById('linkClicked');
    if((ddl1.options.selectedIndex == 0) || (ddl2.options.selectedIndex == 0) || (ddl3.options.selectedIndex == 0))
    {
        if(va2.value != '')
        {        
            va1.style.display = "block";
        }
    }
    else
    {
        va1.style.display = "none";
    }
}
function switchUkashFlash(vart) {

    var whatis = document.getElementById('whatIs');
    var whereToGet = document.getElementById('whereToGet');
    var whereToSpend = document.getElementById('whereToSpend');

    switch (vart) {
        case 1:
            whatis.style.display = "block";
            whereToGet.style.display = "none";
            whereToSpend.style.display = "none";
            break;
        case 2:
            whatis.style.display = "none";
            whereToGet.style.display = "block";
            whereToSpend.style.display = "none";
            break;
        case 3:
            whatis.style.display = "none";
            whereToGet.style.display = "none";
            whereToSpend.style.display = "block";
            break;
    }
}
function findInstore(thisid, thisurl)
{
    var val = document.getElementById(thisid);
    if(val.value != '')
    {
        document.location = thisurl + val.value;
    }
}
function filterSectionGroup(optionid, thisurl)
{
    var val = document.getElementById(optionid);
    document.location = thisurl + val.value;
}
function askUkash(thisid, thisurl) {
    var val = document.getElementById(thisid);
    if (val.value != '') {
        document.location = thisurl + val.value;
    }
}
function askUkashT(event, thisid, thisurl) 
{
    if (event.which || event.keyCode) 
    {
        if ((event.which == 13) || (event.keyCode == 13)) {
            askUkash(thisid, thisurl); 
        } 
    };
//    var val = document.getElementById(thisid);
//    if (val.value != '') 
//    {
//        document.location = thisurl + val.value;
//    }
}

function chkSFDisclaimer(source, value)
{
    // get control that called this
    var ControlName = document.getElementById(resolveServerId('chkSFDisclaimer'));
    // get custom validator name
    var ControlValidatorName = source.id;

    if (ControlName.checked) {
        value.IsValid = true;
        return true;
    }
    else {
        value.IsValid = false;
        return false;
    }
}
function chkSFFDisclaimer()
{
    // get control that called this
    var ControlName = document.getElementById(resolveServerId('chkSFDisclaimer'));
    // get custom validator name
    var ControlValidatorName = document.getElementById(resolveServerSpanId('valSFDisclaimer1'));

    if (ControlName.checked) {
        ControlValidatorName.style.display = "none";
        return true;
    }
    else {
        ControlValidatorName.style.display = "block";
        return false;
    }
}
function chkCCSvoucherCode(source, value)
{
    var txt1 = document.getElementById(resolveServerId('txtUkashCCSVoucherCode1'));
    var txt2 = document.getElementById(resolveServerId('txtUkashCCSVoucherCode2'));
    
    var ControlValidatorName = document.getElementById(resolveServerSpanId('valUkashCCSVoucherCode'));
    if((txt1.value == '') || (txt2.value == ''))
    {
        ControlValidatorName.style.display = "block";
        value.IsValid = false;
        return false;
    }
    else
    {
        value.IsValid = true;
        return true;
        ControlValidatorName.style.display = "none";
    }
}
function chkCCSvoucherValue(source, value)
{
    var ddl1 = document.getElementById(resolveServerId('ddlUkashVoucherValue'));
    var txt2 = document.getElementById(resolveServerId('txtVoucherValue'));
    
    var ControlValidatorName = document.getElementById(resolveServerSpanId('valVoucherValue'));
    if((ddl1.value == '*') || (txt2.value == ''))
    {
        ControlValidatorName.style.display = "block";
        value.IsValid = false;
        return false;
    }
    else
    {
        value.IsValid = true;
        return true;
        ControlValidatorName.style.display = "none";
    }
}
function validatePassStrengthRcc(source, value)
{
    if (document.getElementById(resolveServerId('txtRCPassword')).value == "")
        return;
        
    var strength = passBlur('txtRCPassword');
    var ControlValidatorName = document.getElementById(resolveServerSpanId('valRCPasswordStrength'));
    
    if((strength == 'strong') || (strength == 'excellent'))
    {
        ControlValidatorName.style.display = "none";
        value.IsValid = true;
        return true;
    }
    else
    {
        value.IsValid = false;
        return false;
        ControlValidatorName.style.display = "block";
    }
    
}
function passBlur(pass)
{
    var strength = checkPassStrength(pass);
    var simg = document.getElementById('strengthIndicator')
    
    if(strength.toString() == '0')
    {
        simg.setAttribute("class", "");
        simg.setAttribute("className", "");
    }
    if(strength.toString() == 'weak')
    {
        simg.setAttribute("class", "weak");
        simg.setAttribute("className", "weak");        
    }
    if(strength.toString() == 'medium')
    {
        simg.setAttribute("class", "medium");
        simg.setAttribute("className", "medium");        
    }
    if(strength.toString() == 'strong')
    {
        simg.setAttribute("class", "strong");
        simg.setAttribute("className", "strong");        
    }
    if(strength.toString() == 'excellent')
    {
        simg.setAttribute("class", "excellent");
        simg.setAttribute("className", "excellent");        
    }   
    return strength; 
}
function checkPassStrength(pass)
{
    var password = document.getElementById(resolveServerId(pass)).value;
    /*function to calculate "strength" of a password
    * expects password, returns strength
    */
    //calculate strength out of length
    var strength = 0;
    var length = password.length;
    if (length > 16) 
    {
        strength = 18;
    } 
    else 
    {
        if (length > 8) 
        {
            strength = 12
        }
        else 
        {
            if (length > 5) 
            {
                strength = 6;
            } 
            else 
            {
                if (length > 0) 
                {
                    strength = 3;
                }
                else 
                {
                    return 0; //no password means no strength, :-)
                }
            }
        }    
    }
    //Now consider the letters
    if (password.match(/[a-z]/i)) 
    {
        strength += 5; //at least one letter, 5 points
        if (password.match(/[a-z][A-Z]|[A-Z][a-z]/)) 
        {
            strength += 2; //mixed-case adds 2 points
        }
    } //no letter, no points
    //Now check for numbers
    count = password.match(/[0-9]/g);
    if (count) 
    {
        strength += 5; //at least one number, 5 points
        if (count > 2) 
        {
            strength += 2; //three or more numbers, 2 extra points;
        }
    } //no number, no points
    //Now check for special characters
    count = password.match(/[!,@,#,$,%,^,*,?,_,~,-]/g)
    if (count) 
    {
        strength += 5; //at least one special characters, 5 points
        if (count > 1) 
        {
            strenth += 2; //two or more special characters, 2 extra points
        }
    } //no special characters, no points
    
    
    
    var result = "";
    
    if (strength < 10)
    {
        result = "weak"
    }
    else if (strength < 15)
    {
       result = "medium"
    }
    else if (strength < 25)
    {
       result = "strong"
    }
    else
    {
       result = "excellent"
    }
    return result;
}
function chkReCaptcha()
{
    var captchaTxt = document.getElementById('recaptcha_response_field');
    var captchaChTxt = document.getElementById('recaptcha_challenge_field');
    
    alert(captchaTxt.value);
    alert(captchaChTxt.value);
}

function faqDropChanged(urlink, select) {
    var selectid = document.getElementById(select);
    document.location = urlink + "?node=" + selectid.value + "#id" + selectid.value;
}

function findInstore(thisid, thisurl) {
    var val = document.getElementById(thisid);
    if (val.value != '') {
        document.location = thisurl + val.value;
    }
}
function findInstoreT(event, thisid, thisurl) {
    if (event.which || event.keyCode) {
        if ((event.which == 13) || (event.keyCode == 13)) {
            findInstore(thisid, thisurl);
        }
    };
//    var val = document.getElementById(thisid);
//    if (val.value != '') {
//        document.location = thisurl + val.value;
//    }
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
	//	alert(date.getTime());
		//date.setTime(date.getTime()+(120*60*1000));
	//	alert(date.getTime());
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function checkEnterKey(event, buttonId)
{
    if (event.which || event.keyCode)
    {
        if ((event.which == 13) || (event.keyCode == 13)) 
        {
            __doPostBack(buttonId,'')
            return false;
        }
    } 
    else 
    {
        return true;
    }
}

function checkEnterKeyWithOptions(event, buttonId)
{
    fillReglinkClicked();
    checkRegBirthdayDDLs();
    if (event.which || event.keyCode)
    {
        if ((event.which == 13) || (event.keyCode == 13)) 
        {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(buttonId, "", true, "Rcc", "", false, true))
            return false;
        }
    } 
    else 
    {
        return true;
    }
}

function isMaxLength(obj)
{
    var mlength = 200;
    
    if (obj.getAttribute && obj.value.length > mlength)
    {
        obj.value = obj.value.substring(0,mlength);
    }
}

jQuery.fn.populate = function() {
    this.each(function(){
        this.value = jQuery(this).siblings('label').html();
        jQuery(this).siblings('label').hide();
    });	
    return this.focus(function() {

    if( this.value == jQuery(this).siblings('label').html() ) {
        this.value = "";
    }
    }).blur(function() {
        if( !this.value.length ) {
            this.value = jQuery(this).siblings('label').html();
    }
    });


};

$(function(){
    $('.mapSearch').populate();
    
    /* home infoBox heights */
    $('.infoBox.first').each(function(){
        var h = $(this).innerHeight();
        var altH = $(this).next('.infoBox').innerHeight();
        if(altH > h){
            $(this).css('height', altH + 'px');
            $(this).next('.infoBox').css('height', altH + 'px');
        } else {
            $(this).css('height', h + 'px');
            $(this).next('.infoBox').css('height', h + 'px');
        }
    });
    
});
