﻿
var ForeColor="#000080";
var ForeColor2="#7c7c84";
var SolvSw=false;


function GetWinAlert(Param)
{
    document.getElementById("WinPhpAns").innerHTML="<img border='0' src='/Images/Error.gif'>&nbsp;"+Param;
}

function GetWinClearError()
{
    document.getElementById("WinPhpAns").innerHTML="&nbsp;";
}


function GetWinOK()
{
    document.location.reload();
}



function ErrorCell(row,col)
{
    var Ob="Cel_"+row+"-"+col
    Obj=document.getElementById(Ob)
    if(Obj.tagName=="INPUT")
    {
        Obj.style.color="red"
        Obj.style.borderColor="red"
        Obj.style.fontSize="9pt"
    }
}

function ClearErrors()
{
    var Ob="";
    for(i=1;i<=9;i++)
    for(j=1;j<=9;j++)
    {
        Ob="Cel_"+i+"-"+j
        Obj=document.getElementById(Ob)
		if (Obj!=null)
		{
			if(Obj.tagName=="INPUT")
			{
			    Obj.style.color=ForeColor2
			    Obj.style.borderColor=""
			    Obj.style.fontSize="10pt"
			}
		}
    }
    RemoveStars()
    document.getElementById("UserAns").innerHTML="&nbsp;"
}

function ClearCells()
{
    var ans
    var NeNumMsg="آيا مايل به حذف اطلاعات جدول هستيد ؟"
    ans=window.confirm(NeNumMsg);
    if(ans!=true) {return 0;}
    for(i=1;i<=9;i++)
    for(j=1;j<=9;j++)
    {
        Ob="Cel_"+i+"-"+j
        Obj=document.getElementById(Ob)
        if (Obj!=null)
        {
            if(Obj.tagName=="INPUT")
            {
                Obj.value="";
            }
        }
    }
    ClearErrors()
}


function CheckRows()
{
    var cSw=true;
    ClearErrors()
    for(i=1;i<=9;i++)
    {
        if(!CheckOneRow(i))
        {
            AddRowError(i);
            cSw=false;
        }
    }
    if (!cSw) ShowError("اشكال در سطر يا سطرهاي مشخص شده وجود دارد. ممكن است در آن سطر عددي تكراري باشد و يا سلولي خالي باشد ...")
    return cSw;
}

function CheckOneRow(row)
{
	var CheckSum=0;
	for(j=1;j<=9;j++)
	{
	    var Ob="Cel_"+row+"-"+j
	    Obj=document.getElementById(Ob)
	    if(Obj.tagName=="INPUT")
	    {
	    	CheckSum+=parseInt(Obj.value);
	    }else{
	        CheckSum+=parseInt(Obj.innerHTML);
	            }
	}
    return (CheckSum==45)
}


function CheckCols()
{
    var cSw=true;
    ClearErrors()
    for(i=1;i<=9;i++)
    {
        if(!CheckOneCol(i))
        {
            AddColError(i);
            cSw=false;
        }
    }
    if (!cSw) ShowError("اشكال در ستون يا ستون‌هاي مشخص شده وجود دارد. ممكن است در آن ستون عددي تكراري باشد و يا سلولي خالي باشد ...")
    return cSw;
}

function CheckOneCol(col)
{
    var CheckSum=0;
    for(j=1;j<=9;j++)
    {
        var Ob="Cel_"+j+"-"+col
        Obj=document.getElementById(Ob)
        if(Obj.tagName=="INPUT")
        {
        	CheckSum+=parseInt(Obj.value);
        }else{
            CheckSum+=parseInt(Obj.innerHTML);
        }
    }
    return (CheckSum==45)
}

function CheckDigit()
{
    var cSw=true;
    ClearErrors()
    for(i=1;i<=9;i++)
    for(j=1;j<=9;j++)
    {
        var Ob="Cel_"+i+"-"+j
        Obj=document.getElementById(Ob)
        if(Obj.tagName=="INPUT")
        {
            Cel=Obj.value;
            if(Cel >9 || Cel!= Obj.value || Cel<1)
            {
                ErrorCell(i,j)
                cSw=false;
            }
        }
    }
    if (!cSw) ShowError("اشكالي در سلول هاي مشخص شده وجود دارد . ممكن است عدد مناسبي در آن سلول قرار نگرفته و يا خالي باشد ...")
    return cSw;
}


function RemoveStars()
{
    X=document.getElementsByTagName('span')
    for(i=0;i<X.length;i++)
    {
        if(X[i].Mode=='Err') X[i].innerHTML ="";
    }
}

function AddRowError(row)
{
    ObjID=document.getElementById("Cel_"+row+"-0");
    ObjID.innerHTML="<span name='AlStar' Mode='Err' class='ErrStar'>*</span>"
}

function AddColError(col)
{
    ObjID=document.getElementById("Cel_0-"+col);
    ObjID.innerHTML="<span name='AlStar' Mode='Err' class='ErrStar'>*</span>"
}




function CheckTable()
{
    ClearErrors()
    if (!CheckDigit()) return 0;
    if (!CheckRows()) return 0;
    if (!CheckCols()) return 0;
    return true;
}



function SaveTable()
{
    if (!CheckTable()) return 0;
    ShowOK("تبريك ! شما موفق به حل جدول سودوكوي امروز شديد. ميتوانيد مشخصات خود را وارد نموده تا جزو برندگان امروز باشيد  ...");
    document.getElementById("GetWinBox").style.display=""
}


function ShowOK(Param)
{
    document.getElementById("UserAns").innerHTML="<img border='0' src='/Images/Icons/OK.gif'>&nbsp;"+Param;
    document.getElementById("UserAns").style.color="green"
    SolvSw=true;
}


function ShowError(Param)
{
    document.getElementById("UserAns").innerHTML="<img border='0'  src='/Images/Icons/Error.gif'>&nbsp;"+Param;
    document.getElementById("UserAns").style.color="red"
}


function CreateTable(Input)
{
    var RowMode=-1;
    var ColMode=-1;
    var Exp="";
    var PuzCel=new Array();

    if(Input=="-") return "No Table Data ...";

    for(i=0;i<9;i++)
    {PuzCel[i]=new Array();}

    var PuzRow=Input.split("|");
    if(PuzRow.length<9) return "Invalid Data<br>Number of Row : "+PuzRow.length+"<br>";


    for (i=0;i<9;i++)
    {
        if(PuzRow[i].length<9) return "Invalid Data<br>Number of Column : "+PuzRow[i].length+" in Row "+i+"<br>";
        for(j=0;j<9;j++)
        {
            PuzCel[i][j]=PuzRow[i].substr(j,1);
            if (PuzCel[i][j]=="0") PuzCel[i][j]="&nbsp;"
        }
    }


    Exp+="<style>";
    Exp+=".E_PuzCell_0{width:20px;height:20px;background-color:#DADDEF;color:"+ForeColor+";font-family:tahoma;font-size:8pt;text-align:center;font-weight:bold;padding-top:3px}";
    Exp+=".E_PuzCell_1{width:20px;height:20px;background-color:#ececf4;color:"+ForeColor+";font-family:tahoma;font-size:8pt;text-align:center;font-weight:bold;padding-top:3px}";
    Exp+=".E_PuzCell_2{width:20px;height:20px;background-color:#DADDEF;color:"+ForeColor2+";font-family:tahoma;font-size:9pt;text-align:center;font-weight:bold}";
    Exp+=".E_PuzCell_3{width:20px;height:20px;background-color:#ececf4;color:"+ForeColor2+";font-family:tahoma;font-size:9pt;text-align:center;font-weight:bold}";
    Exp+=".E_PuzTable{border-left:1px solid #eee; border-right:1px solid #fff; border-top:1px solid #C0C0C0; border-bottom:1px solid #808080;} ";
    Exp+=".PuzSp{height:10px;width:9px;font-size:1pt}";
    Exp+=".PuzSp0{height:10px;width:9px;font-size:1pt}";
    Exp+="background-color:#f5f5f5;font-size:8pt;text-align:center;font-family:tahoma;font-weight:bold}";
    Exp+="</style>";



    Exp+="<table border='0' cellspacing='0'  dir='ltr'>";

    Exp+="<tr>";
    for(col=0;col<=9;col++)
    {
        Exp+="<td ><span id='Cel_0-"+col+"' >&nbsp;</span></td>"
        if ((col)%3==0 && (col)!=0) Exp+="<td class='PuzSp0'>-</td>";
    }
    Exp+="</tr>";

    for (row=1;row<=9;row++)
    {
        if((row-1) % 3 ==0 && row>2) Exp+="<tr><td class='PuzSp0'></td><td colspan='11' class='PuzSp'></tr>";
        Exp+="<tr>";
        ColMode=-1;
        Exp+="<td ><span id='Cel_"+row+"-0"+"' >&nbsp;</span></td>";
        for(col=1;col<=9;col++)
        {
          	if (PuzCel[row-1][col-1]=="&nbsp;")
            {
                if (RowMode + ColMode==0){
                    Exp+="<td ><input type='text' id='Cel_"+row+"-"+col+"' name='"+row+"-"+col+"' size='1' class='E_PuzCell_2' def='"+PuzCel[row-1][col-1]+"' onkeydown='javascript:ClearErrors()' style='color:000'></td>"
                }else{
                    Exp+="<td ><input type='text' id='Cel_"+row+"-"+col+"' name='"+row+"-"+col+"' size='1' class='E_PuzCell_3' def='"+PuzCel[row-1][col-1]+"'  onkeydown='javascript:ClearErrors()'></td>"
                }
            }
            else
            {
                if (RowMode + ColMode==0){
                    Exp+="<td ><span id='Cel_"+row+"-"+col+"' name='"+row+"-"+col+"' size='1' class='E_PuzCell_0'  def='"+PuzCel[row-1][col-1]+"'  >"+PuzCel[row-1][col-1]+" </span></td>"
                }else{
                    Exp+="<td ><span id='Cel_"+row+"-"+col+"' name='"+row+"-"+col+"' size='1' class='E_PuzCell_1'  def='"+PuzCel[row-1][col-1]+"'  >"+PuzCel[row-1][col-1]+" </span></td>"
                }
            }
			if(col%3==0) ColMode=-ColMode;
			if (col%3==0 && row==1 && col!=9) Exp+="<td  rowspan='11' class='PuzSp'></td>";
        }
        Exp+="</tr>";
        if(row%3==0) RowMode=-RowMode;
    }
    Exp+="</table>"
    document.write( Exp);
}


function CreateAnsTable(Input)
{
    var RowMode=-1;
    var ColMode=-1;
    var Exp="";
    var PuzCel=new Array();

    if(Input=="-") return "-";

    for(i=0;i<9;i++) PuzCel[i]=new Array();

    var PuzRow=Input.split("|");
    if(PuzRow.length<9) return "Invalid Data<br>Number of Row : "+PuzRow.length+"<br>";
    for (i=0;i<9;i++)
    {
        if(PuzRow[i].length<9) return "Invalid Data<br>Number of Column : "+PuzRow[i].length+" in Row "+i+"<br>";
        for(j=0;j<9;j++)
        {
            PuzCel[i][j]=PuzRow[i].substr(j,1);
            if (PuzCel[i][j]=="0") PuzCel[i][j]="&nbsp;"
        }
    }


    Exp+="<style>";
    Exp+=".aPuzCell_0{width:12px;height:10px;color:#888;font-size:8pt;text-align:center}";
    Exp+=".aPuzCell_1{width:12px;height:10px;background-color:#DADDEF;color:#777;;font-size:8pt;text-align:center}";
    Exp+=".aPuzTable{border-left:1px solid #C0C0C0; border-right:1px solid #DADDEF; border-top:1px solid #C0C0C0; border-bottom:1px solid #DADDEF; ";
    Exp+="background-color:#EEEEEE;font-size:8pt;text-align:center;font-family:tahoma;font-weight:bold}";
    Exp+="</style>";

    Exp+="<table border='1' cellspacing='0' class='PuzTable' dir='ltr'>";

    for (row=1;row<=9;row++)
    {
        Exp+="<tr>";
        ColMode=-1;
        for(col=1;col<=9;col++)
        {
            if (RowMode + ColMode==0)
            {
                Exp+="<td class='aPuzCell_1'><span id='CL"+row+"_"+col+"'>"+PuzCel[row-1][col-1]+"</span></td>"
            }else{
                Exp+="<td class='aPuzCell_0'><span id='CL"+row+"_"+col+"'>"+PuzCel[row-1][col-1]+"</span></td>"
            }
            if(col%3==0) ColMode=-ColMode;
        }
        Exp+="</tr>";
        if(row%3==0) RowMode=-RowMode;
    }
        Exp+="</table>"
        document.write( Exp);
}


function RegisterWinner()
{
    if(!SolvSw)
    {
        alert(" شما هنوز موفق به حل جدول نشده ايد");
        return 0;
    }
    document.frmWinnerAdd.submit();

}

function WinnerAdd(Name,Email,Time)
{
    var WinCode="";
    var Wtl=Email+"  |  "+"زمان حل :  "+Time;
    WinCode+="&nbsp;&nbsp; • &nbsp;<a href='mailto:"+Email+"' title='"+Wtl+"' >"+Name+"</a>";
    WinCode+="<br>";
    document.getElementById("WinnerList").innerHTML+=WinCode;
}

function WinnerClear()
{
    document.getElementById("WinnerList").innerHTML="";
}
