/*******************************************/
/*  Copyright 2005 - Jon Dunfee, jdkd.com  */
/*  last updated: November 01, 2005        */
/*  Permission granted for unlimited use   */
/*  so far as the copyright notice above   */
/*  remains intact.                        */
/*******************************************/

/****************************************** Simple String Validation */
function isValid(string,vstring)  
{
         if(vstring == "alpha") { var regex = /^([a-z])+$/; }
    else if(vstring == "ALPHA") { var regex = /^([A-Z])+$/; }
    else if(vstring == "Alpha") { var regex = /^([a-zA-Z])+$/; }
    else if(vstring == "ANum")  { var regex = /^([a-zA-Z0-9])+$/; }
    else if(vstring == "num")   { var regex = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; }
    else if(vstring == "int")   { var regex = /^-?\d+$/; }
    else if(vstring == "$")     { var regex = /(^\$?-?\d*\.?\d{0,2}$)|(^\$?-?\d{1,3}(\,\d{3})+\.?\d{0,2}$)|((^\$\(?-?\d*\.?\d{0,2}\)$)|(^\$?\(-?\d{1,3}(\,\d{3})+\.?\d{0,2}\)$))/; } // CR04428143
    else if(vstring == "email") { var regex = /^([\w\.\-])+\@(([\w\-])+\.)+([a-zA-Z0-9]{2,4})+$/; }
    else if(vstring == "phone") { var regex = /^\(?\d{3}\)?(\-| |\.)?\d{3}(\-| |\.)?\d{4}$/; }
    else if(vstring == "ssn")   { var regex = /^\d{3}\-?\d{2}\-?\d{4}$/; }
    else if(vstring == "url")   { var regex = /^http:\/\/([\w\.\-])+\.+([\w\.\-])/; }
    else if(vstring == "zip")   { var regex = /^(\d{5})(-(\d{4}))?$/; }
    else var regex = vstring;
    return regex.test(string);
}

/****************************************** Image Manipulation */
function newImage(theImgLoc)
{
    if (document.images)
    {
        var theNewImg = new Image();
        theNewImg.src = theImgLoc;
        return theNewImg;
    }
}

function swapImg(theImg, theSrc)
{
	if(theImg)
	{
		if(theImg.src)
		{
			theImg.src = (theSrc.src)?theSrc.src:eval(theSrc+".src");
		}
		else
		{
            document.getElementById(theImg).src = (theSrc.src)?theSrc.src:eval(theSrc+".src");
		}
	}
}

/****************************************** Additional Image Manipulation */
function transImg(thisImg,img,dur,trans,div)
{
    var div = (div)?div:false;
    var navApp = navigator.appVersion;
    if(navApp.substring(navApp.indexOf("MSIE")+5,navApp.indexOf("MSIE")+8) >= 5.5 && trans)
    {
        var theImg = document.getElementById(thisImg);
        if(theImg.filters && trans < 24 && trans > 0)
        {
            theImg.filters.revealTrans.Transition=((trans)?trans:23);
            theImg.filters.revealTrans.Duration=dur;
            theImg.filters.revealTrans.stop();
            theImg.filters.revealTrans.apply();
            swapImg(thisImg,img,div);
            theImg.filters.revealTrans.play();
        }
        else if(theImg.filters && trans == 24)
        {
            theImg.filters.blendTrans.Duration=dur;
            theImg.filters.blendTrans.stop();
            theImg.filters.blendTrans.apply();
            swapImg(thisImg,img,div);
            theImg.filters.blendTrans.play();
        }
        else
        {
            swapImg(thisImg,img,div);
        }
    }
    else
    {
        swapImg(thisImg,img,div);
    }
}

/****************************************** DHTML Common Scripts */
function divStyle(theDiv)
{
    if (document.getElementById) { return document.getElementById(theDiv).style; }
    return eval("document."+((document.all)?"all."+theDiv+".style":theDiv));
}
function moveDiv(thisDiv,L,T,Z)
{
    if(L != "relative") {(document.all)?divStyle(thisDiv).pixelLeft = L:divStyle(thisDiv).left = parseInt(L) + "px";}
    if(T != "relative") {(document.all)?divStyle(thisDiv).pixelTop = T:divStyle(thisDiv).top = parseInt(T) + "px";}
    if(Z) { divStyle(thisDiv).zIndex = Z; }
}

function clipDiv(thisDiv,cT,cR,cB,cL)
{
    if (document.all || document.getElementById) { divStyle(thisDiv).clip = "rect("+cT+" "+cR+" "+cB+" "+cL+")"; }
    else
    {
        divStyle(thisDiv).clip.top = parseInt(cT) + "px";
        divStyle(thisDiv).clip.bottom = parseInt(cB) + "px";
        divStyle(thisDiv).clip.left = parseInt(cL) + "px";
        divStyle(thisDiv).clip.right = parseInt(cR) + "px";
    }
}

function showDiv()
{ 
    for(var sDi=0;sDi<showDiv.arguments.length;sDi++)
    {
        if (document.getElementById) { node = document.getElementById(showDiv.arguments[sDi]).style.visibility='visible'; }
        else divStyle(showDiv.arguments[sDi]).visibility = "visible";
    }
}

function hideDiv()
{
    for(var hDi=0;hDi<hideDiv.arguments.length;hDi++)
    {
        if (document.getElementById) { node = document.getElementById(hideDiv.arguments[hDi]).style.visibility='hidden'; }
        else divStyle(hideDiv.arguments[hDi]).visibility=(document.all)?"hidden":"hide";
    }
}

/****************************************** DHTML Additional Scripts */
function transStyle(dur,trans)
{
    var navApp = navigator.appVersion;
    if(navApp.substring(navApp.indexOf("MSIE")+5,navApp.indexOf("MSIE")+8) >= 5.5 && dur)
    {
        if(trans < 24)
        {
            return "filter:revealTrans(duration="+dur+",transition="+trans+");";
        }
        else
        {
            return "filter:blendTrans(duration="+dur+");";
        }
    }
    return "";
}

function transDiv(thisDiv,dur,trans)
{
    var navApp = navigator.appVersion;
    if(navApp.substring(navApp.indexOf("MSIE")+5,navApp.indexOf("MSIE")+8) >= 5.5 && trans)
    {
        var theDiv = document.getElementById(thisDiv);
        hideDiv(thisDiv);
        if(theDiv.filters && trans == 24)
        {
            theDiv.filters.blendTrans.Duration=dur;
            theDiv.filters.blendTrans.stop();
            theDiv.filters.blendTrans.apply();
            showDiv(thisDiv);
            theDiv.filters.blendTrans.play();        
        }
        else if (theDiv.filters && trans < 24 && trans > 0)
        {
            theDiv.filters.revealTrans.Transition=((trans)?trans:23);
            theDiv.filters.revealTrans.Duration=dur;
            theDiv.filters.revealTrans.stop();
            theDiv.filters.revealTrans.apply();
            showDiv(thisDiv);
            theDiv.filters.revealTrans.play();        
        }
        else
        {
            showDiv(thisDiv);
        }
    }
    else
    {
        showDiv(thisDiv);
    }
}

function fadeDiv(theDiv,speed,show,alpha)
{
    var alpha = (alpha)?alpha+10:10;
    hideDiv(theDiv);
    if(document.all)
    {
        document.getElementById(theDiv).style.filter = "alpha(opacity="+((show)?alpha:100-alpha)+")";
    }
    showDiv(theDiv);
    if(alpha < 100)
    {
        setTimeout("fadeDiv('"+theDiv+"',"+speed+","+show+","+alpha+")",speed);
    }
    else if(!show)
    {
        hideDiv(theDiv);
    }
}

/****************************************** Simplified Document.Write */
function w(theText)
{
    window.document.open("text/html");
    window.document.write(theText);
    window.document.close();
}

function wl(theText)
{
    window.document.open("text/html");
    window.document.writeln(theText);
    window.document.close();
}

/****************************************** Added Global Functions */
function cs(obj,cls)
{
	if(obj) obj.className = (cls)?cls:"";
}

/****************************************** Credit Card Validation */
function isValidCC(card,num,element,dashes)
{
    if(!num) return false;
    var regex = /^/; var ccform = "";
    var ccn = ""; var nums = "0123456789";
    for(var i=0;i<num.length;i++)
    {
        if(nums.indexOf(num.substring(i,i+1)) != -1) ccn += num.substring(i,i+1);
    }
    if(card == "AX") { regex = /^3[4,7]\d{13}$/; ccform = "xxxx-xxxxxx-xxxxx" }
    else if(card == "CB") { regex = /^389\d{11}$/; ccform = "xxxx-xxxxxx-xxxx" }
    else if(card == "DC") { regex = /(^38[0-8]\d{11})|(^3[0,6]\d{12})$/; ccform = "xxxx-xxxxxx-xxxx" }
    else if(card == "DI") { regex = /^6011\d{12}$/; ccform = "xxxx-xxxx-xxxx-xxxx" }
    else if(card == "JC") { regex = /^3((5[3-8][0-9][0-9][0-9])|(33[7-9][0-9][0-9])|(34[0-9][0-9][0-9])|(528[0-9][0-9])|(15[8,9][0-9][0-9])|(11[2-9][0-9][0-9])|(120[0-9][0-9])|(10[0-2][0-9][0-9])|(09[6-9][0-9][0-9])|(09[0-4][0-9][0-9])|(08[8-9][0-9][0-9]))\d{10}$/; ccform = "xxxx-xxxx-xxxx-xxxx" }
    else if(card == "MC") { regex = /^5[1-5]\d{14}$/; ccform = "xxxx-xxxx-xxxx-xxxx" }
    else if(card == "VI") { regex = /^4\d{12}|\d{15}$/; ccform = "visa" }
	else return false;
	if(!regex.test(ccn)) return false;
    var checksum = 0;
    for(var i=(2-(ccn.length % 2)); i<=ccn.length; i+=2) { checksum += parseInt(ccn.charAt(i-1)); }
    for(var i=(ccn.length % 2) + 1; i<ccn.length; i+=2)
    {
        var digit = parseInt(ccn.charAt(i-1)) * 2;
        checksum += (digit < 10)?digit:(digit-9);
    }
    if((checksum % 10) != 0) return false;
	if(element) element.value = ccn;
    if(element && dashes)
    {
        if(ccform == "visa") ccform = (ccn.length == 13)?"xxxx-xxxxx-xxxx":"xxxx-xxxx-xxxx-xxxx";
        var ccnform = ""; var count = 0;
        for(var i=0;i<ccform.length;i++)
        {
            if(ccform.charAt(i) == "x")
            {
                ccnform += ("" + ccn.charAt(count));
                count++;
            }
            else
            {
                ccnform += ("" + ccform.charAt(i));
            }
        }
        element.value = ccnform;
    }
    return true;
}
